scribble/src/ink_rw_file.c File Reference

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include "ink_rw.h"
#include "ScribbleLog.h"
#include "ScribbleUtils.h"

Go to the source code of this file.

Defines

#define MAX_INT_CHAR_LEN   16

Enumerations

enum  WRITEINT_MODE { WRITEINT_NULL = 0, WRITEINT_2SPACES, WRITEINT_LINEEND }

Functions

static unsigned char read_ink (FILE *file, PtrInk ink)
static unsigned char write_ink (PtrInk ink, FILE *file)
static long read_stroke (FILE *file, PtrStroke stroke)
static unsigned char write_stroke (FILE *file, PtrStroke stroke)
static unsigned char read_integer (int *integer, FILE *file)
static unsigned char write_integer (int integer, FILE *file, WRITEINT_MODE iMode)
static unsigned char write_rgb (GdkColor *pGdkColor, FILE *file)
static unsigned char read_rgb (GdkColor *pGdkColor, FILE *file)
PtrInk file_read_ink (const char *inkfile)
unsigned char file_write_ink (PtrInk ink, const char *inkfile)
unsigned char write_uchars (FILE *file, const unsigned char *ustr, unsigned int iLen)
unsigned char write_pure_integer (int integer, FILE *file)
unsigned char write_line (FILE *file)
unsigned char write_2spaces (FILE *file)


Define Documentation

#define MAX_INT_CHAR_LEN   16

Copyright (C) 2005-2008 iRex Technologies B.V. All rights reserved.

Definition at line 45 of file ink_rw_file.c.


Enumeration Type Documentation

Enumerator:
WRITEINT_NULL 
WRITEINT_2SPACES 
WRITEINT_LINEEND 

Definition at line 47 of file ink_rw_file.c.

00047             {
00048         WRITEINT_NULL=0,//only write int
00049         WRITEINT_2SPACES,//end with 2 spaces char
00050         WRITEINT_LINEEND,//end with line end
00051 } WRITEINT_MODE;


Function Documentation

PtrInk file_read_ink ( const char *  inkfile  ) 

Copyright (C) 2005-2008 iRex Technologies B.V. All rights reserved.

Definition at line 72 of file ink_rw_file.c.

00073 {
00074     FILE *file = NULL;
00075     PtrInk ink = NULL;
00076     unsigned char ret;
00077     if (inkfile == NULL || strlen (inkfile) == 0)
00078     {
00079         SB_INKERRPRINTF("failed:incorrect arguments\n");
00080         return NULL;
00081     }
00082     file = fopen (inkfile, "rb");
00083     if (file == NULL)
00084     {
00085         SB_INKERRPRINTF("fopen failed \n");
00086         return NULL;
00087     }
00088 
00089     ink = construct_ink ();
00090     if (ink == NULL)
00091     {
00092         fclose (file);
00093         return NULL;
00094     }
00095     ret = read_ink (file, ink);
00096     if (ret == 0)
00097     {
00098         destroy_ink (ink);
00099         fclose (file);
00100         return NULL;
00101     }
00102 
00103     SB_INKPRINTF("succeeded,return ink=%p\n", ink);
00104     fclose (file);
00105     return ink;
00106 }

Here is the call graph for this function:

unsigned char file_write_ink ( PtrInk  ink,
const char *  inkfile 
)

Definition at line 237 of file ink_rw_file.c.

00238 {
00239     FILE *file = NULL;
00240     unsigned char ret;
00241 
00242     if (ink == NULL || ink->nStrokes == 0 || inkfile == NULL
00243         || strlen (inkfile) == 0)
00244     {
00245         return 0;
00246     }
00247     file = fopen (inkfile, "wb+");
00248     if (file == NULL)
00249     {
00250         SB_INKERRPRINTF("fopen failed \n");
00251         return 0;
00252     }
00253     ret = write_ink (ink, file);
00254     fclose (file);
00255     return ret;
00256 }

Here is the call graph for this function:

unsigned char read_ink ( FILE *  file,
PtrInk  ink 
) [static]

Definition at line 111 of file ink_rw_file.c.

00112 {
00113     long lPos, fileLen;
00114     PtrStroke stroke = NULL;
00115     unsigned char integer_str[MAX_INT_CHAR_LEN];
00116 
00117     if (file == NULL || ink == NULL)
00118     {
00119         SB_INKERRPRINTF("failed:incorrect arguments\n");
00120         return 0;
00121     }
00122 
00123   //to get the file length
00124     if ( 0!= fseek (file, 0, SEEK_END))
00125     {
00126         SB_INKERRPRINTF("fseek failed \n");
00127         return 0;
00128     }
00129     fileLen = ftell (file);
00130     if ( 0!=fseek (file, 0, SEEK_SET))
00131     {
00132         SB_INKPRINTF("fseek failed \n");
00133         return 0;
00134     }
00135 
00136     if ( !read_integer (&(ink->nStrokes), file) )
00137     {
00138         SB_INKERRPRINTF("failed getting ink->nStrokes\n");
00139         return 0;
00140     }
00141     if (ink->nStrokes == 0)
00142     {
00143         SB_INKERRPRINTF("return:ink->nStrokes=0 \n");
00144         return 0;
00145     }
00146     SB_INKPRINTF("nStrokes=%d\n",ink->nStrokes);
00147     sprintf (integer_str, "%d", ink->nStrokes);
00148     lPos = sizeof (char) * (strlen (integer_str) + 2);
00149     while (lPos != -1 && lPos != fileLen)
00150     {
00151       //SB_INKPRINTF("lPos=%d\n", lPos);
00152         stroke = construct_stroke ();
00153         if (stroke == NULL)
00154         {
00155             SB_INKERRPRINTF("return: construct_stroke failed \n");
00156             return 0;
00157         }
00158         lPos = read_stroke (file, stroke);
00159         ink_add_stroke (ink, stroke);
00160     }
00161     return 1;
00162 }

Here is the call graph for this function:

unsigned char read_integer ( int *  integer,
FILE *  file 
) [static]

Definition at line 348 of file ink_rw_file.c.

00349 {
00350     unsigned long lRead = 0;
00351     unsigned char integer_str[MAX_INT_CHAR_LEN];// 0xFFFFFFFF=4294967295 
00352     int i = 0;
00353     int bPrev0Dor20=0;//previous char is 0x0D or 0x20?,special
00354 
00355     if (integer == NULL || file == NULL)
00356         return 0;
00357   //two legal mode:[0x20 0x20],[0x0D 0x0A]
00358     while (1)
00359     {
00360         if (i >= MAX_INT_CHAR_LEN) return 0;
00361 
00362         lRead = fread (&(integer_str[i]), sizeof (char), 1, file);
00363         if(!lRead) return 0;
00364 
00365         if (bPrev0Dor20)
00366         {
00367             if(  (integer_str[i-1] == 0x20 && integer_str[i] == 0x20)
00368                   ||(integer_str[i-1] == 0x0D && integer_str[i] == 0x0A) )
00369             {
00370                 integer_str[i-1] = '\0';
00371                 *integer = atoi (integer_str);
00372                 return 1;
00373             }
00374             return 0;
00375         }
00376         if(integer_str[i] == 0x0D || integer_str[i] == 0x20)
00377         {
00378             bPrev0Dor20=1;
00379         }
00380         i++;
00381     }
00382     return 0;
00383 }

unsigned char read_rgb ( GdkColor *  pGdkColor,
FILE *  file 
) [static]

Definition at line 462 of file ink_rw_file.c.

00463 {
00464     int iColor;
00465     if ( !read_integer(&iColor, file) )
00466     {
00467         SB_INKERRPRINTF("failed\n");
00468         return 0;
00469     }
00470     pGdkColor->red=(guint16)iColor;
00471     if ( !read_integer(&iColor, file) )
00472     {
00473         SB_INKERRPRINTF("failed\n");
00474         return 0;
00475     }
00476     pGdkColor->green=(guint16)iColor;
00477     if ( !read_integer(&iColor, file) )
00478     {
00479         SB_INKERRPRINTF("failed\n");
00480         return 0;
00481     }
00482     pGdkColor->blue=(guint16)iColor;
00483     SB_INKPRINTF("rgb(%d,%d,%d) \n", 
00484                     pGdkColor->red, pGdkColor->green,pGdkColor->blue);
00485     return 1;
00486 }

Here is the call graph for this function:

long read_stroke ( FILE *  file,
PtrStroke  stroke 
) [static]

Definition at line 169 of file ink_rw_file.c.

00170 {
00171     int i;
00172     PtrInkPoint point;
00173 
00174     if (file == NULL || stroke == NULL)
00175     {
00176         SB_INKERRPRINTF("failed:incorrect arguments\n");
00177         return -1;
00178     }
00179 
00180     if ( !read_integer(&(stroke->iPenSize), file) )
00181     {
00182         SB_INKPRINTF("failed getting stroke->iPenSize\n");
00183         return -1;
00184     }
00185   //read rgb
00186     if ( !read_rgb(&stroke->gdkColor,file) )
00187     {
00188         SB_INKERRPRINTF("failed write rgb\n");
00189         return 0;
00190     }
00191 
00192     if ( !read_integer(&(stroke->nPoints), file) )
00193     {
00194         SB_INKERRPRINTF("failed getting stroke->nPoints\n");
00195         return -1;
00196     }
00197 
00198     SB_INKPRINTF("npoints=%d,ipensize=%d\n",
00199             stroke->nPoints,stroke->iPenSize);
00200 
00201     if ( stroke->nPoints == 0)
00202     {
00203         SB_INKPRINTF("returned because of stroke->nPoints=0\n");
00204         return ftell (file);//lPos
00205     }
00206 
00207     //stroke->nPoints will be changed in iteration,so we should keep it.
00208     int nTotalPoints=stroke->nPoints;
00209     for (i = 0; i < nTotalPoints; i++)
00210     {
00211         point = construct_point ();
00212         if ( NULL==point )
00213         { 
00214             SB_INKERRPRINTF("construct point error\n");
00215             return -1;
00216         }
00217         if ( !read_integer (&(point->x), file) )
00218         {
00219             SB_INKERRPRINTF("failed getting (point%d->x)\n",i);
00220             return -1;
00221         }
00222         if ( !read_integer (&(point->y), file) )
00223         {
00224             SB_INKERRPRINTF("failed getting (point%d->y)\n",i);
00225             return -1;
00226         }
00227             //SB_INKPRINTF("point(%d,%d) \n", point->x, point->y);
00228         ink_add_point(stroke, point);
00229     }
00230     return ftell (file);//current file pointer position
00231 }

Here is the call graph for this function:

unsigned char write_2spaces ( FILE *  file  ) 

Definition at line 413 of file ink_rw_file.c.

00414 {
00415     unsigned char sep[2];
00416     sep[0] = 0x20;
00417     sep[1] = 0x20;
00418     return write_uchars(file, sep, sizeof(sep)/sizeof(sep[0]) );
00419 }

Here is the call graph for this function:

unsigned char write_ink ( PtrInk  ink,
FILE *  file 
) [static]

Definition at line 259 of file ink_rw_file.c.

00260 {
00261     PtrStroke stroke;
00262 
00263     if (ink == NULL || file == NULL )
00264     {
00265         SB_INKPRINTF("failed:incorrect arguments\n");
00266         return 0;
00267     }
00268     if (ink->nStrokes == 0)
00269     {
00270         SB_INKPRINTF("return:ink->nStrokes=0 \n");
00271         return 0;
00272     }
00273     if ( 0!= fseek (file, 0, SEEK_SET) )
00274     {
00275         SB_INKERRPRINTF("fseek failed \n");
00276         return 0;
00277     }
00278     if ( !write_integer (ink->nStrokes, file,WRITEINT_LINEEND) )
00279     {
00280         SB_INKERRPRINTF("failed writing ink->nStrokes=%d \n",
00281                         ink->nStrokes);
00282         return 0;
00283     }
00284 
00285     SB_INKPRINTF("ink=%p, ink->nStrokes=%d \n", ink, ink->nStrokes);
00286     stroke = ink->firstStroke;
00287     while (stroke)
00288     {
00289         if (0 ==write_stroke (file, stroke) )
00290         {
00291             SB_INKERRPRINTF("write_stroke failed \n");
00292             return 0;
00293         }
00294         stroke = stroke->nextStroke;
00295     }
00296     SB_INKPRINTF("succeeded \n");
00297     return 1;
00298 }

Here is the call graph for this function:

unsigned char write_integer ( int  integer,
FILE *  file,
WRITEINT_MODE  iMode 
) [static]

Definition at line 423 of file ink_rw_file.c.

00424 {
00425     unsigned char bRet= write_pure_integer(integer,file);
00426 
00427     if( WRITEINT_NULL==iMode ) return bRet;
00428 
00429     if( WRITEINT_2SPACES==iMode )
00430     {
00431         return write_2spaces(file);
00432     }
00433   //WRITEINT_LINEEND
00434     return write_line(file);
00435 }

Here is the call graph for this function:

unsigned char write_line ( FILE *  file  ) 

Definition at line 404 of file ink_rw_file.c.

00405 {
00406     unsigned char sep[2];
00407     sep[0] = 0x0D;
00408     sep[1] = 0x0A;
00409     return write_uchars(file, sep, sizeof(sep)/sizeof(sep[0]) );
00410 }

Here is the call graph for this function:

unsigned char write_pure_integer ( int  integer,
FILE *  file 
)

Definition at line 396 of file ink_rw_file.c.

00397 {
00398     unsigned char integer_str[MAX_INT_CHAR_LEN];// 0xFFFFFFFF=4294967295 
00399     sprintf (integer_str, "%d", integer);
00400     return write_uchars(file, integer_str, strlen(integer_str) );
00401 }

Here is the call graph for this function:

unsigned char write_rgb ( GdkColor *  pGdkColor,
FILE *  file 
) [static]

Definition at line 438 of file ink_rw_file.c.

00439 {
00440     if ( !write_integer(pGdkColor->red, file, WRITEINT_2SPACES) )
00441     {
00442         SB_INKERRPRINTF("failed, pGdkColor->red= %d \n", pGdkColor->red);
00443         return 0;
00444     }
00445     if ( !write_integer(pGdkColor->green, file, WRITEINT_2SPACES) )
00446     {
00447         SB_INKERRPRINTF("failed, pGdkColor->green= %d \n", 
00448                         pGdkColor->green);
00449         return 0;
00450     }
00451     if ( !write_integer(pGdkColor->blue, file, WRITEINT_LINEEND) )
00452     {
00453         SB_INKERRPRINTF("failed, pGdkColor->blue= %d \n", pGdkColor->blue);
00454         return 0;
00455     }
00456     SB_INKPRINTF("rgb(%d,%d,%d) \n", pGdkColor->red, 
00457                     pGdkColor->green,pGdkColor->blue);
00458     return 1;
00459 }

Here is the call graph for this function:

unsigned char write_stroke ( FILE *  file,
PtrStroke  stroke 
) [static]

Definition at line 301 of file ink_rw_file.c.

00302 {
00303     if (file == NULL || stroke == NULL || stroke->nPoints == 0)
00304     {
00305         return 0;
00306     }
00307   //write pen size
00308     if ( !write_integer(stroke->iPenSize,file,WRITEINT_LINEEND) )
00309     {
00310         SB_INKERRPRINTF("write failed, iPenSize= %d \n",stroke->iPenSize);
00311         return 0;
00312     }
00313   //write rgb
00314     if ( !write_rgb(&stroke->gdkColor,file) )
00315     {
00316         SB_INKERRPRINTF("failed write rgb\n");
00317         return 0;
00318     }
00319 
00320     if ( !write_integer(stroke->nPoints,file,WRITEINT_LINEEND) )
00321     {
00322         SB_INKERRPRINTF("write failed, nPoints= %d \n",stroke->nPoints);
00323         return 0;
00324     }
00325 
00326     SB_INKPRINTF("stroke->nPoints=%d \n", stroke->nPoints);
00327     PtrInkPoint point = stroke->firstPoint;
00328     while (point)
00329     {
00330         if ( !write_integer(point->x, file, WRITEINT_2SPACES) )
00331         {
00332             SB_INKERRPRINTF("fwrite failed, point->x= %d \n", point->x);
00333             return 0;
00334         }
00335         if ( !write_integer(point->y, file, WRITEINT_LINEEND) )
00336         {
00337             SB_INKERRPRINTF("fwrite failed, point->y= %d \n", point->y);
00338             return 0;
00339         }
00340         SB_INKPRINTF("point(%d,%d) \n", point->x, point->y);
00341         point = point->nextPoint;
00342     }
00343     return 1;
00344 }

Here is the call graph for this function:

unsigned char write_uchars ( FILE *  file,
const unsigned char *  ustr,
unsigned int  iLen 
)

Definition at line 386 of file ink_rw_file.c.

00387 {
00388     unsigned long lWrite ;
00389     if (file == NULL) return 0;
00390 
00391     lWrite = fwrite (ustr, sizeof (unsigned char), iLen, file);
00392 
00393     return (lWrite == iLen ?1:0);
00394 }


Generated on Sun Dec 14 17:14:37 2008 by  doxygen 1.5.6