Hi,

This is my proposed API for exporting to MSWord format. It is in its 
infancy, but if I get these calls working properly, we'll have decent MSWord 
export capabilities. It probably needs some (i.e. a lot of) tweaking, esp. 
around the fields stuff. There are a lot of rough edges and missing 
features. This is only the beginning though. I plan to add more powerful 
export features in the future once these basic properties are handled.

There's also a small sample program to demonstrate proposed usage of the API 
(in case you couldn't figure it out :). Probably neither file will compile, 
so don't try to.

Anything marked "Initially unsupported" means that version 0.1 probably 
won't have the calls implemented, but the functions will be in place and 
probably evaluate to "noops" with an associated wvTrace("Function 
wvExporter_foo isn't implemented yet."). This means we can add functionality 
pre-emptively to the ie_exp_MSWord97 class and one day it will all magically 
work.

I plan for the exporting API to be "use-driven" i.e. the types of calls a WP 
such as AW or KWord needs will be found in the API. This is why I'm posting 
this message here instead of the wvWare-devel list. Basically, this means 
that you won't have to deal with all of those nasty word structures such as 
FIB, CHP, FKP, etc...
Thoughts, suggestions, comments, and help are all appreciated :)

Dom
_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.

Share information about yourself, create your own public profile at 
http://profiles.msn.com.
/*
* text2word by Dom Lachowicz
*/

#include <stdio.h>
#include <wvexporter.h>

int main(int argc, char *argv[])
{
        FILE *in, *out;
        wvExporter *wv;
        char line[BUFSIZ];

        if(argc != 3) {
                fprintf(stderr, "Usage: text2word in_txtfile out_docfile\n");
                exit(1);
        }

        /* open file for reading */
        in = fopen(argv[1], "r");
        if(in == NULL) {
                fprintf(stderr, "Error opening %s for reading\n", argv[1]);
                exit(1);
        }

        /* open a new word document */
        wv = wvExporter_create(argv[2]);
        if(wv == NULL) {
                fprintf(stderr, "Error opening %s for export\n", argv[2]);
                exit(1);
        }

        /* TODO: dump some information into
         * the summary stream
         */
        /* wvExporter_summaryPut(wv, , ); */

        /*********************************************
         * be a dummie and assume that everything is
         * really one section and one paragraph. we are
         * dealing with a text document here so there's
         * really only 1 section and all of the paragraphs
         * and all of the characters really have the same
         * style so we can get away with this
         ********************************************/
        wvExporter_sectionBegin(wv);
        wvExporter_paraBegin(wv);

        while(fgets(in, line, BUFSIZ) != NULL) {
                int nlen = strlen(line);
                /* detect hard-line break */
                if(line[nlen-1] == '\n') {
                        line[nlen-1] = 0;
                        wvExporter_textRun(wv, line);
                        wvExporter_hardBreak(wv);
                } else
                        wvExporter_textRun(wv, line);
        }

        /* end the paragraph */
        wvExporter_paraEnd(wv);

        /* end the section */
        wvExporter_sectionEnd(wv);

        /* close the input and output streams */
        fclose(in);
        wvExporter_close(wv);

        return 0;
}
/**
* Word Document Exporting API
* Proposed Draft v0.1
* Sept. 22, 2000
*
* (c) Dom Lachowicz
*/

/*
* This draft is under constant revision.
* Expect it to change often and unannounced.
*/

typedef struct wvExporterTAG {
        /**
         * Consider everything here private.
         * Yes, that means you!
         */
        wvStream *main;
        wvStream *table0;
        wvStream *data;
        wvStream *summaryInformation;
        /* 0 or more object streams that we won't use right now */

        /* we'll need a *lot* of accounting information here */

        FIB m_fib;
        /* others.. */
} wvExporter;

/*********************************************************************/

/**
* Creates a MSWord 97 exporter object
*
*/
wvExporter *wvExporter_create(const char *filename);

/**
* Closes and saves the MSWord97 document
*/
void wvExporter_close(wvExporter *wv);

/*********************************************************************/

/*
* I think this covers all known types
*/

/**
* Puts the string value associated with 'key' into
* the Summary Stream
*/
void wvExporter_summaryPutString(wvExporter *wv, U32 key,
                                 const char *value);

/**
* Puts the long value associated with 'key'
* into the Summary Stream
*/
void wvExporter_summaryPutLong(wvExporter *wv, U32 key,
                               long value);

/**
* Puts the value associated with 'key'
* into the Summary Stream. Converts 'value' into
* the appropriate type (string or long) accordingly.
*/
void wvExporter_summaryPut(wvExporter *wv, U32 key,
                           const char *value);

/**
* Puts the time value associated with 'key' into
* the Summary Stream.
* Possibly -=Initially unsupported=-
*/
void wvExporter_summaryPutDate(wvExporter *wv, U32 key,
                               time_t *value);

/*********************************************************************/

/*
* No font or paragraph properties will be
* initially supported
*/

/**
* Sets foreground color
* -=Initially unsupported=-
*/
void wvExporter_setForeColor(wvExporter *wv, U32 color);

/**
* Sets background color
* -=Initially unsupported=-
*/
void wvExporter_setBackColor(wvExporter *wv, U32 color);

/**
* Sets the font face
* See listing for common valid values
* -=Initially unsupported=-
*/
void wvExporter_setFontFace(wvExporter *wv, const char *face);

/**
* Sets the font size *in half points*
* -=Initially unsupported=-
*/
void wvExporter_setFontSize(wvExporter *wv, double half_points);

/**
* Set the font weight
* -=Initially unsupported=-
*/
void wvExporter_setFontWeight(wvExporter *wv, U32 fw);

/**
* Wrapper around all other font calls
* -=Initially unsupported=-
*/
void wvExporter_setFont(wvExporter *wv, const char *face,
                        double half_points, U32 fw);

/*********************************************************************/

/*
* Def: section - a contiguous sequence of paragraphs within
*                the text stream. Equivalent to chapters in
*                book. The boundaries of sections mark where
*                the layout rules for a document (# columns,
*                etc...) are changed.
*/

/**
* Begins a section
* For now, please count on there only
* being 1 section per document
*/
void wvExporter_sectionBegin(wvExporter *wv);

/**
* Ends a section
*/
void wvExporter_sectionEnd(wvExporter *wv);

/*
* Def: paragraph - contiguous sequence of characters within
*                  the text stream, delimited by a paragraph
*                  mark (ASCII(13)).
*/

/**
* Begins a paragraph
*/
void wvExporter_paraBegin(wvExporter *wv);

/**
* Ends a paragraph
*/
void wvExporter_paraEnd(wvExporter *wv);

/*
* Def: text run - contiguous sequence of characters within
*                 the text stream with the same character
*                 formatting properties.
*/

/**
* Write a run of text. Can cross paragraph boundaries.
* Don't expect that to work though. Always close
* your paragraphs! This function can be called more than once
* on the same "run of text" - i.e. you can put this call
* into a loop. C++ example:
*  while(!myRun.isEmpty()) {
*     wvExporter_textRun(wv, myRun.getElement());
*     myRun.advanceNextElement();
*  }
*/
void wvExporter_textRun(wvExporter *wv, const char *text);

/**
* Page break (ASCII(12))
*/
void wvExporter_pageBreak(wvExporter *wv);

/**
* Column break (ASCII(14))
*/
void wvExporter_columnBreak(wvExporter *wv);

/**
* "Hard" break - line break that's not a paragraph end
* ASCII(11)
*/
void wvExporter_hardBreak(wvExporter *wv);

/**
* Exports a picture to the word document
* -=Initially Unsupported=-
*/
void wvExporter_expPicture(wvExporter *wv, U32 type,
                           const unsigned char *bytes);

/**
* Begin field
* As of Word97, 91 different field types
* -=Initially Unsupported=-
*/
void wvExporter_beginField(wvExporter *wv, int field_code);

/**
* End field
* -=Initially Unsupported=-
*/
void wvExporter_endField(wvExporter *wv);

Reply via email to