Example #1
0
/*! Writes the data from \a pattern to a file with the given \a fileName.
 *  Returns \c true if successful, otherwise returns \c false. */
int writeExp(EmbPattern* pattern, const char* fileName)
{
#ifdef ARDUINO /* ARDUINO TODO: This is temporary. Remove when complete. */
return 0; /* ARDUINO TODO: This is temporary. Remove when complete. */
#else /* ARDUINO TODO: This is temporary. Remove when complete. */

    EmbFile* file = 0;
    EmbStitchList* stitches = 0;
    double dx = 0.0, dy = 0.0;
    double xx = 0.0, yy = 0.0;
    int flags = 0;
    unsigned char b[4];

    if(!pattern) { embLog_error("format-exp.c writeExp(), pattern argument is null\n"); return 0; }
    if(!fileName) { embLog_error("format-exp.c writeExp(), fileName argument is null\n"); return 0; }

    if(!embStitchList_count(pattern->stitchList))
    {
        embLog_error("format-exp.c writeExp(), pattern contains no stitches\n");
        return 0;
    }

    /* Check for an END stitch and add one if it is not present */
    if(pattern->lastStitch->stitch.flags != END)
        embPattern_addStitchRel(pattern, 0, 0, END, 1);

    file = embFile_open(fileName, "wb");
    if(!file)
    {
        embLog_error("format-exp.c writeExp(), cannot open %s for writing\n", fileName);
        return 0;
    }

    /* write stitches */
    stitches = pattern->stitchList;
    while(stitches)
    {
        dx = stitches->stitch.xx * 10.0 - xx;
        dy = stitches->stitch.yy * 10.0 - yy;
        xx = stitches->stitch.xx * 10.0;
        yy = stitches->stitch.yy * 10.0;
        flags = stitches->stitch.flags;
        expEncode(b, (char)roundDouble(dx), (char)roundDouble(dy), flags);
        if((b[0] == 128) && ((b[1] == 1) || (b[1] == 2) || (b[1] == 4)))
        {
            embFile_printf(file, "%c%c%c%c", b[0], b[1], b[2], b[3]);
        }
        else
        {
            embFile_printf(file, "%c%c", b[0], b[1]);
        }
        stitches = stitches->next;
    }
    embFile_printf(file, "\x1a");
    embFile_close(file);
    return 1;
#endif /* ARDUINO TODO: This is temporary. Remove when complete. */
}
int writeExp(EmbPattern* pattern, const char* fileName)
{
    FILE* file;
    EmbStitchList *stitches;
    double dx = 0.0, dy = 0.0;
    double xx = 0.0, yy = 0.0;
    int flags = 0;
    int i = 0;
    unsigned char b[4];

    file = fopen(fileName, "wb");
    if(file == 0)
    {
        /*TODO: set status here "Error opening EXP file for write:" */
        return 0;
    }

    /* write stitches */
    stitches = pattern->stitchList;
    while(stitches)
    {
        dx = stitches->stitch.xx * 10.0 - xx;
        dy = stitches->stitch.yy * 10.0 - yy;
        xx = stitches->stitch.xx * 10.0;
        yy = stitches->stitch.yy * 10.0;
        flags = stitches->stitch.flags;
        expEncode(b, roundDouble(dx), roundDouble(dy), flags);
        if((b[0] == 128) && ((b[1] == 1) || (b[1] == 2) || (b[1] == 4)))
        {
            fprintf(file, "%c%c%c%c", b[0], b[1], b[2], b[3]);
        }
        else
        {
            fprintf(file, "%c%c", b[0], b[1]);
        }
        stitches = stitches->next;
    }
    fprintf(file, "\x1a");
    fclose(file);
    return 1;
}