belahcene wrote:
> Hi,
> I use fltk (C++ graphical interface) to create an electronics design package. 
> I created a list of objects and placed them on a window. My problem is how to 
> save the file, I think to create a list in memory for the objects placed on 
> the sheet and store them in a file.
> I think (not sure of course) that xfig for example uses this procedure ?

        Sure sounds like a simple matter of making your class(s) that
        handle the components have Save() and Load() methods.

        This general technique has always worked for me:

// A/C TRANSFORMER CLASS
class Transformer {
    int x, y;           // position on board
    int num_pins;       // number of pins

    // ..

    // SAVE TO FILE
    int Save(FILE *fp) {
        if ( fprintf(fp, "xfmr %d %d %d\n", x, y, num_pins) < 0 ) return(-1);
        return(0);
    }
    // LOAD FROM FILE
    int Load(const char *s, string& errmsg) {
        if ( sscanf(s, "xfmr %d %d %d", &x, &y, &num_pins) == 3 ) return(0);
        errmsg = "bad command or argument: ";
        errmsg += s;
        return(-1);
    }
};

        ..and your 'save' and 'load' functions might look like:

// SAVE ALL COMPONTENTS
int Save(const char* filename) {
    int err = 0;
    // OPEN FILE
    FILE *fp = fopen(filename, "w");
    if ( fp == NULL ) {
        Fail(filename, 0, strerror(errno));
        return(-1);
    }
    // SAVE TRANSFORMERS
    fprintf(fp, "# TRANSFORMERS\n");
    for ( int t=0; t<transformers.size(); t++ ) {
        if ( transformers[t].Save(fp) < 0 ) {
            err = Fail(filename, 0, strerror(errno));
            goto save_fail;
        }
    }
    // SAVE RESISTORS
    fprintf(fp, "# RESISTORS\n");
    // ..ETC..
save_fail:
    if ( fclose(fp) < 0 )
        Fail(filename, 0, strerror(errno));
    return(err);
}

// LOAD ALL COMPONTENTS
#define ISIT(x)         if ( strncmp(s, x, strlen(x)) == 0 )
int Load(const char* filename) {
    // OPEN FILE
    FILE *fp = fopen(filename, "r");
    if ( fp == NULL ) {
        Fail(filename, 0, strerror(errno));
        return(-1);
    }

    // LOAD COMPONENTS
    int err = 0;
    int line = 0;
    char s[1024];
    string errmsg;
    while ( fgets(s, sizeof(s), fp) ) {
        ++line;
        if ( s[0] == '#' ) continue;    // skip comments
        ISIT("xfmr") {
            Transformer *x = new Transformer();
            if ( x->Load(s, errmsg) < 0 ) {
                err = Fail(filename, line, errmsg);
                break;
            }
            // ..Add to transformers array..
            continue;
        }
        ISIT("res") {
            // ..ETC..
        }
        errmsg = "Unknown command: ";
        errmsg += s;
        Fail(filename, line, s);
        break;
    }
    fclose(fp);
    return(err);
}
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to