00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <stdlib.h>
00036 #include <stdio.h>
00037 #include <string.h>
00038 #include <gtk/gtk.h>
00039
00040 #include "ink_rw.h"
00041 #include "ScribbleLog.h"
00042 #include "ScribbleUtils.h"
00043
00044
00045 #define MAX_INT_CHAR_LEN 16
00046
00047 typedef enum{
00048 WRITEINT_NULL=0,
00049 WRITEINT_2SPACES,
00050 WRITEINT_LINEEND,
00051 } WRITEINT_MODE;
00052
00053
00054 static unsigned char read_ink (FILE * file, PtrInk ink);
00055 static unsigned char write_ink (PtrInk ink, FILE * file);
00056
00057 static long read_stroke (FILE * file, PtrStroke stroke);
00058 static unsigned char write_stroke (FILE * file, PtrStroke stroke);
00059
00060 static unsigned char read_integer (int *integer, FILE * file);
00061
00062 static unsigned char write_integer (int integer, FILE * file,WRITEINT_MODE iMode);
00063
00064
00065 static unsigned char write_rgb(GdkColor *pGdkColor, FILE * file);
00066
00067 static unsigned char read_rgb(GdkColor *pGdkColor, FILE * file);
00068
00069
00070
00071
00072 PtrInk file_read_ink(const char *inkfile)
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 }
00107
00108
00109
00110
00111 unsigned char read_ink (FILE * file, PtrInk ink)
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
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
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 }
00163
00164
00165
00166
00167
00168
00169 long read_stroke (FILE * file, PtrStroke stroke)
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
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);
00205 }
00206
00207
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
00228 ink_add_point(stroke, point);
00229 }
00230 return ftell (file);
00231 }
00232
00233
00235
00236
00237 unsigned char file_write_ink (PtrInk ink, const char *inkfile)
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 }
00257
00258
00259 unsigned char write_ink (PtrInk ink, FILE * file)
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 }
00299
00300
00301 unsigned char write_stroke (FILE * file, PtrStroke stroke)
00302 {
00303 if (file == NULL || stroke == NULL || stroke->nPoints == 0)
00304 {
00305 return 0;
00306 }
00307
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
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 }
00345
00346
00347
00348 unsigned char read_integer (int *integer, FILE * file)
00349 {
00350 unsigned long lRead = 0;
00351 unsigned char integer_str[MAX_INT_CHAR_LEN];
00352 int i = 0;
00353 int bPrev0Dor20=0;
00354
00355 if (integer == NULL || file == NULL)
00356 return 0;
00357
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 }
00384
00385 unsigned char
00386 write_uchars (FILE * file,const unsigned char* ustr,unsigned int iLen)
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 }
00395
00396 unsigned char write_pure_integer (int integer, FILE * file)
00397 {
00398 unsigned char integer_str[MAX_INT_CHAR_LEN];
00399 sprintf (integer_str, "%d", integer);
00400 return write_uchars(file, integer_str, strlen(integer_str) );
00401 }
00402
00403
00404 unsigned char write_line (FILE * file)
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 }
00411
00412
00413 unsigned char write_2spaces (FILE * file)
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 }
00420
00421
00422 unsigned char
00423 write_integer(int integer, FILE * file,const WRITEINT_MODE iMode)
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
00434 return write_line(file);
00435 }
00436
00437
00438 unsigned char write_rgb(GdkColor *pGdkColor, FILE * file)
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 }
00460
00461
00462 unsigned char read_rgb(GdkColor *pGdkColor, FILE * file)
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 }