File: pilot-read-notepad.c

package info (click to toggle)
pilot-link 0.12.5-dfsg-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,860 kB
  • ctags: 5,811
  • sloc: ansic: 53,143; sh: 10,459; java: 2,584; perl: 2,247; python: 1,044; makefile: 991; yacc: 662; cpp: 551; xml: 39
file content (548 lines) | stat: -rw-r--r-- 11,515 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*
 * $Id: pilot-read-notepad.c,v 1.42 2007/02/04 23:06:02 desrod Exp $ 
 *
 * pilot-read-notepad.c:  Translate Palm NotePad database into generic
 *                        picture format
 *
 * Copyright (c) 2002, Angus Ainslie
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "pi-source.h"
#include "pi-notepad.h"
#include "pi-file.h"
#include "pi-header.h"
#include "pi-userland.h"

#ifdef HAVE_PNG
#include "png.h"
#endif

const char *progname;

#ifdef HAVE_PNG
void write_png( FILE *f, struct NotePad *n );
#endif




/***********************************************************************
 *
 * Function:    fmt_date
 *
 * Summary:
 *
 * Parameters:  None
 *
 * Return:      Nothing
 *
 ***********************************************************************/
static const char *fmt_date ( noteDate_t d )
{

   static char buf[24];
   sprintf (buf, "%d-%02d-%02d %02d:%02d:%02d",
	    d.year, d.month, d.day, d.hour, d.min, d.sec);
   return buf;

}


/***********************************************************************
 *
 * Function:    write_ppm
 *
 * Summary:
 *
 * Parameters:  None
 *
 * Return:      Nothing
 *
 ***********************************************************************/
void write_ppm( FILE *f, struct NotePad *n )
{
   int i,j,k,datapoints = 0;
   unsigned long black = 0;
   unsigned long white = 0xFFFFFFFF;

   fprintf( f, "P6\n# " );

   if( n->name != NULL )
     fprintf( f, "%s (created on %s)\n", n->name, fmt_date( n->createDate ));
   else
     fprintf( f, "%s\n", fmt_date( n->createDate ));

   /* NotePad says that the width is only 152 but it encodes 160 bits */
   /* of data! - AA */
   fprintf( f, "%ld %ld\n255\n",
	    n->body.width+8, n->body.height );

   if( n->body.dataType == NOTEPAD_DATA_BITS )
     for( i=0; i<n->body.dataLen/2; i++ )
       {
	  datapoints += n->data[i].repeat;

	  for( j=0; j<n->data[i].repeat; j++ )
	    {

	       for( k=0; k<8; k++ )
		 {
		    if( n->data[i].data & 1<<(7-k) )
		      fwrite( &black, 3, 1, f );
		    else
		      fwrite( &white, 3, 1, f );
		 }
	    }
       }
   else
     for( i=0; i<n->body.dataLen/2; i++ )
       {
	  for( k=0; k<8; k++ )
	    {
	       if( n->data[i].repeat & 1<<(7-k) )
		 fwrite( &black, 3, 1, f );
	       else
		 fwrite( &white, 3, 1, f );
	    }
	  for( k=0; k<8; k++ )
	    {
	       if( n->data[i].data & 1<<(7-k) )
		 fwrite( &black, 3, 1, f );
	       else
		 fwrite( &white, 3, 1, f );
	    }
       }

}


/***********************************************************************
 *
 * Function:    write_png
 *
 * Summary:
 *
 * Parameters:  None
 *
 * Return:      Nothing
 *
 ***********************************************************************/
#ifdef HAVE_PNG
void write_png( FILE *f, struct NotePad *n )
{
   int i,j,k = 0, width;
   png_structp png_ptr;
   png_infop info_ptr;
   png_bytep row;

   width = n->body.width + 8;

   png_ptr = png_create_write_struct
     ( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

   if(!png_ptr)
     return;

   info_ptr = png_create_info_struct(png_ptr);
   if( !info_ptr )
     {
	png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
	return;
     }

   if( setjmp( png_jmpbuf( png_ptr )))
     {
	png_destroy_write_struct( &png_ptr, &info_ptr );
	fclose( f );
	return;
     }

   png_init_io( png_ptr, f );

   png_set_IHDR( png_ptr, info_ptr, width, n->body.height,
		1, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
		PNG_COMPRESSION_TYPE_DEFAULT,  PNG_FILTER_TYPE_DEFAULT );

   png_write_info( png_ptr, info_ptr );

   row = (png_bytep)malloc( width/8 * sizeof( png_byte ));

   if( NULL == row )
     return;

   if( n->body.dataType == NOTEPAD_DATA_BITS )
     for( i=0, k=0; i<n->body.dataLen/2; i++ )
       for( j=0; j<n->data[i].repeat; j++ )
	 {
	    row[k] = n->data[i].data ^ 0xFF;

	    if( ++k >= width/8 )
	      {
		 png_write_row( png_ptr, row );
		 k = 0;
		 png_write_flush(png_ptr);
	      }
	 }
   else
     for( i=0, k=0; i<n->body.dataLen/2; i++ )
       {
	  row[k] = n->data[i].repeat ^ 0xFF;
	  row[k+1] = n->data[i].data ^ 0xFF;
	  k += 2;
	  if( k >= width/8 )
	    {
	       png_write_row( png_ptr, row );
	       k = 0;
	       png_write_flush(png_ptr);
	    }
       }

   png_write_end(png_ptr, info_ptr);

   free( row );

   row = NULL;

   png_destroy_write_struct( &png_ptr, &info_ptr );

}
#endif


/***********************************************************************
 *
 * Function:    write_png_v2
 *
 * Summary:
 *
 * Parameters:  None
 *
 * Return:      Nothing
 *
 ***********************************************************************/
void write_png_v2( FILE *f, struct NotePad *n )
{
   if( n->body.dataType != NOTEPAD_DATA_PNG )
     {
	fprintf( stderr, "Bad data Type" );
	return;
     }

   fwrite( n->data, n->body.dataLen, 1, f );
   fflush( f );
}


/***********************************************************************
 *
 * Function:    print_note_info
 *
 * Summary:
 *
 * Parameters:  None
 *
 * Return:      Nothing
 *
 ***********************************************************************/
void print_note_info( struct NotePad n, struct NotePadAppInfo nai, int category )
{
   if( n.flags & NOTEPAD_FLAG_NAME )
     printf( "Name: %s\n", n.name );

   printf( "Category: %s\n", nai.category.name[category] );
   printf( "Created: %s\n", fmt_date( n.createDate ));
   printf( "Changed: %s\n", fmt_date( n.changeDate ));

   if( n.flags & NOTEPAD_FLAG_ALARM )
     printf( "Alarm set for: %s\n", fmt_date( n.alarmDate ));
   else
     printf( "Alarm set for: none\n" );

   printf( "Picture: " );

   if( n.flags & NOTEPAD_FLAG_BODY )
     printf( "yes\n" );
   else
     printf( "no\n" );

   printf( "Picture version: %ld\n", n.body.dataType );

}

/***********************************************************************
 *
 * Function:    output_picture
 *
 * Summary:
 *
 * Parameters:  None
 *
 * Return:      Nothing
 *
 ***********************************************************************/
void output_picture( int type, struct NotePad n )
{
   char fname[FILENAME_MAX];
   FILE *f;
   char extension[8];
   static int i = 1;

   if( n.body.dataType == NOTEPAD_DATA_PNG )
     type = NOTE_OUT_PNG;

   if( n.flags & NOTEPAD_FLAG_NAME )
     {
	if( type == NOTE_OUT_PNG )
	  sprintf( extension, ".png" );
	else if( type == NOTE_OUT_PPM )
	  sprintf( extension, ".ppm" );

	sprintf( fname, "%s", n.name );
     }
   else
     {
	if( type == NOTE_OUT_PNG )
	  sprintf( extension, "_np.png" );
	else if( type == NOTE_OUT_PPM )
	  sprintf( extension, "_np.ppm" );

	sprintf( fname, "%4.4d", i++ );
     }

	if (plu_protect_files( fname, extension, sizeof(fname) ) < 1) {
		goto cleanup;
	}

   printf ("Generating %s...\n", fname);

   f = fopen (fname, "wb");
   if( f )
     {
	switch( n.body.dataType )
	  {
	   case NOTEPAD_DATA_PNG:
	     fprintf( stderr, "Notepad data version 2 defaulting to png output.\n" );
	     write_png_v2( f, &n );
	     break;

	   case NOTEPAD_DATA_BITS:
	   case NOTEPAD_DATA_UNCOMPRESSED:
	     switch( type )
	       {
		case NOTE_OUT_PPM:
		  write_ppm( f, &n );
		  break;

		case NOTE_OUT_PNG:
#ifdef HAVE_PNG
		  write_png( f, &n );
#else
		  fprintf( stderr, "read-notepad was built without png support\n" );
#endif
		  break;
	       }
	     break;

	   default:
	     fprintf( stderr, "Picture version is unknown - unable to convert\n" );
	  }

	fclose (f);

     }
	else {
		fprintf (stderr, "Can't write to %s\n", fname);
	}

cleanup:
	free_NotePad( &n );
}


int main(int argc, const char *argv[])
{
   int	c,	/* switch */
     db,
     i,
     sd	= -1,
     action 	= NOTEPAD_ACTION_OUTPUT;

   int type = NOTE_OUT_PPM;

   const char
	*typename	= 
#ifdef HAVE_PNG
	"png"
#else
	"ppm"
#endif
	;

   struct 	PilotUser User;
   struct 	NotePadAppInfo nai;
   pi_buffer_t *buffer;

   poptContext pc;

   struct poptOption options[] = {
   	USERLAND_RESERVED_OPTIONS
        {"list", 'l', POPT_ARG_VAL, &action, NOTEPAD_ACTION_LIST, "List Notes on device", NULL},
        {"type", 't', POPT_ARG_STRING, &typename, 0, "Specify picture output type, either \"ppm\" or \"png\"", "type"},
        POPT_TABLEEND
   };

	pc = poptGetContext("read-notepad", argc, argv, options, 0);
	poptSetOtherOptionHelp(pc,"\n\n"
		"   Copy or list your NotePad database.\n\n");


	if (argc<2) {
		poptPrintUsage(pc,stderr,0);
		return 1;
	}
	while ((c = poptGetNextOpt(pc)) >= 0) {
		fprintf(stderr,"   ERROR: Unhandled option %d.\n",c);
		return 1;
	}
	if (c < -1) {
		plu_badoption(pc,c);
	}

	if( !strncmp( "png", typename, 3 ))
	{
#ifdef HAVE_PNG
		type = NOTE_OUT_PNG;
#else
		fprintf( stderr, "   WARNING: read-notepad was built without png support,\n"
			"            using ppm instead.\n" );
		type = NOTE_OUT_PPM;
#endif
	}
	else if( !strncmp( "ppm", typename, 3 ))
	{
		type = NOTE_OUT_PPM;
	}
	else
	{
		fprintf( stderr, "   WARNING: Unknown output type defaulting to ppm\n" );
		type = NOTE_OUT_PPM;
	}

	sd = plu_connect();

   if( sd < 0 )
     goto error;

   if (dlp_ReadUserInfo(sd, &User) < 0)
     goto error_close;

   /* Open the NotePad database, store access handle in db */
   if( dlp_OpenDB(sd, 0, 0x80 | 0x40, "npadDB", &db ) < 0)
     {
	fprintf(stderr,"   ERROR: Unable to open NotePadDB on Palm.\n");
	dlp_AddSyncLogEntry(sd, "Unable to open NotePadDB.\n");
	exit(EXIT_FAILURE);
     }

   buffer = pi_buffer_new (0xffff);

   dlp_ReadAppBlock(sd, db, 0, 0xffff, buffer);
   unpack_NotePadAppInfo( &nai, buffer->data, buffer->used);

   for (i = 0;; i++)
     {
	int 	attr,
		category,
		len = 0;

	struct 	NotePad n;

	if( sd )
	  {
	     len = dlp_ReadRecordByIndex(sd, db, i, buffer, 0,
				       &attr, &category);

	     if (len < 0)
	       break;
	  }
	else
		pi_buffer_clear(buffer);

	/* Skip deleted records */
	if ((attr & dlpRecAttrDeleted) || (attr & dlpRecAttrArchived))
	  continue;

	unpack_NotePad( &n, buffer->data, buffer->used);

	switch( action )
	  {
	   case NOTEPAD_ACTION_LIST:
	     print_note_info( n, nai, category );
	     printf( "\n" );
	     break;

	   case NOTEPAD_ACTION_OUTPUT:
	     if (!plu_quiet) {
	        print_note_info( n, nai, category );
	     }
	     output_picture( type, n );
	     if (!plu_quiet) {
	        printf( "\n" );
	     }
	     break;
	  }
     }


   if( sd ) {
	/* Close the database */
	dlp_CloseDB( sd, db );
	dlp_AddSyncLogEntry( sd, "Successfully read NotePad from Palm.\n\n"
			    "Thank you for using pilot-link.");
	dlp_EndOfSync( sd, 0 );
	pi_close(sd);
     }

   pi_buffer_free (buffer);

   return 0;

error_close:
	pi_close(sd);

error:
	return -1;

}

/* vi: set ts=8 sw=4 sts=4 noexpandtab: cin */
/* ex: set tabstop=4 expandtab: */
/* Local Variables: */
/* indent-tabs-mode: t */
/* c-basic-offset: 8 */
/* End: */