File: task.c

package info (click to toggle)
gtimer 2.0.0-1.2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 1,380 kB
  • ctags: 849
  • sloc: ansic: 8,122; sh: 821; perl: 651; makefile: 86
file content (824 lines) | stat: -rw-r--r-- 19,349 bytes parent folder | download | duplicates (2)
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
/*
 * Routines for dealing with tasks
 *
 * Copyright:
 *	(C) 1999 Craig Knudsen, cknudsen@cknudsen.com
 *	See accompanying file "COPYING".
 * 
 *	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., 59 Temple Place,
 *	Suite 330, Boston, MA  02111-1307, USA
 *
 * History:
 *	17-Apr-2005	Add support for subtracting a particular offset
 *			off of timers.  (Russ Allbery)
 *	09-Mar-2000	Added functions to allow for restoring to
 *			a previous state: taskMark(), taskMarkAll(),
 *			taskRestore(), taskRestoreAll()
 *	25-Mar-1999	Lots of WIN32 patches made by
 *			Thomas Epperly <Thomas.Epperly@aspentech.com>
 *	18-Mar-1999	Internationalization
 *	11-Nov-1998	Added support for options
 *	23-Apr-1998	Added new function: taskClearAll
 *	22-Mar-1998	Added an extra argument to TaskGetAnnotationEntries
 *			for time offset.
 *	15-Mar-1998	Debugged all malloc/free and found one place
 *			where free() was not called.
 *	15-Mar-1998	Fixed bug that caused SIGSEGV.  Only appeared
 *			if one more tasks (but not the last one) were
 *			deleted.
 *	15-Mar-1998	Added new function: taskAddAnnotation
 *	11-Mar-1998	Changed functions to K&R style
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef WIN32
#include <io.h>
#else
#include <dirent.h>
#endif
#include <errno.h>
#include <malloc.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "task.h"

#ifdef GTIMER_MEMDEBUG
#include "memdebug/memdebug.h"
#endif

// PV: Internationalization
#include "gtimeri18n.h"

static int num_tasks = 0;
static Task **tasks = NULL;
static int max_task = -1;
static int last_number = -1;


#ifdef WIN32
static int valid_name ( filename )
char *filename;
{
  while ( *filename && isdigit ( *filename ) )
    filename++;
  return ( ! strcmp ( filename, ".task" ) );
}
#endif


/*
** Add a task.
*/
void taskAdd ( task )
Task *task;
{
  int loop;
  int new_max_task = 0;

  new_max_task = max_task;

  if ( task->number == -1 ) {
    for ( loop = 0; loop < max_task && task->number < 0; loop++ ) {
      if ( tasks[loop] == NULL )
        task->number = loop;
    }
    if ( task->number < 0 )
      task->number = ++max_task;
    new_max_task = max_task;
  } else if ( task->number > max_task ) {
    new_max_task = task->number;
  }

  if ( tasks == NULL ) {
    tasks = (Task **) malloc ( sizeof ( Task * ) * ( new_max_task + 1 ) );
    for ( loop = 0; loop < new_max_task; loop++ )
      tasks[loop] = NULL;
  } else
    tasks = (Task **) realloc ( tasks,
      sizeof ( Task * ) * ( new_max_task + 1 ) );

  for ( loop = max_task + 1; loop <= new_max_task; loop++ )
    tasks[loop] = NULL;

  max_task = new_max_task;

  tasks[task->number] = task;
  num_tasks++;
}



/*
** Create a new task
*/
Task *taskCreate ( name )
char *name;
{
  Task *task;

  task = (Task *) malloc ( sizeof ( Task ) );
  memset ( task, '\0', sizeof ( Task ) );
  task->name = (char *) malloc ( strlen ( name ) + 1 );
  strcpy ( task->name, name );
  time ( &task->created );
  task->number = -1; /* not yet assigned */

  return ( task );
}


/*
** Mark the current time in a task.  We can then use taskRestore()
** to restore the state of the task to what it was when this function
** was called.
*/
void taskMark ( task, offset )
Task *task;
int offset;
{
  int i;
  int seconds;

  for ( i = 0; i < task->num_entries; i++ ) {
    task->entries[i]->marked_seconds = task->entries[i]->seconds;
  }
  if ( offset > 0 && task->num_entries > 0 ) {
    seconds = task->entries[task->num_entries - 1]->marked_seconds;
    seconds -= offset;
    if ( seconds < 0 )
      seconds = 0;
    task->entries[task->num_entries - 1]->marked_seconds = seconds;
  }
}


/*
** Mark the current time in all tasks so that we can use taskRestoreAll()
** to restore the state of all tasks to where they are right now.
** NOTE: this is intended to be used only with the timing elements.
** Other stuff like annotations doesn't apply here, but annotations shouldn't
** be added during idle time anyhow...
*/
void taskMarkAll ( offset )
int offset;
{
  int loop;

  for ( loop = 0; loop <= max_task; loop++ ) {
    if ( tasks[loop] ) {
      taskMark ( tasks[loop], offset );
    }
  }
}


/*
** Restore a task to its previous state when taskMark() was called.
** This is intended to allow the application to bookmark the state
** of a task and then return to it (as far as timing data goes).
*/
void taskRestore ( task )
Task *task;
{
  int i;

  for ( i = 0; i < task->num_entries; i++ ) {
    task->entries[i]->seconds = task->entries[i]->marked_seconds;
  }
}



/*
** Restore all tasks to where they were when taskRestoreAll()
** was called.
*/
void taskRestoreAll ()
{
  int loop;

  for ( loop = 0; loop <= max_task; loop++ ) {
    if ( tasks[loop] ) {
      taskRestore ( tasks[loop] );
    }
  }
}



/*
** Delete a task.
*/
int taskDelete ( task, taskdir )
Task *task;
char *taskdir;
{
  char *path;

  path = (char *) malloc ( strlen ( taskdir ) + 10 );
  sprintf ( path, "%s/%d.task", taskdir, task->number );
  unlink ( path );
  sprintf ( path, "%s/%d.ann", taskdir, task->number );
  unlink ( path );
  free ( path );

  tasks[task->number] = NULL;
  num_tasks--;
  taskFree ( task );

  return ( 0 );
}



/*
** Return the number of tasks currently loaded.
*/
int taskCount () {
  return ( num_tasks );
}



/*
** Get a task by number.
*/
Task *taskGet ( number )
int number;
{
  return ( tasks[number] );
}


/*
** Get first task.
*/
Task *taskGetFirst () {
  last_number = -1;
  return ( taskGetNext() );
}


/*
** Get next task.
*/
Task *taskGetNext () {
  int loop;

  if ( ! num_tasks )
    return ( NULL );

  for ( loop = last_number + 1; loop <= max_task; loop++ ) {
    if ( tasks[loop] ) {
      last_number = loop;
      return ( tasks[loop] );
    }
  }
 
  return ( NULL );
}



/*
** Clear all tasks out of memory.
*/
void taskClearAll ()
{
  int loop;

  for ( loop = 0; loop <= max_task; loop++ ) {
    if ( tasks[loop] ) {
      taskFree ( tasks[loop] );
      tasks[loop] = 0;
    }
  }
  num_tasks = 0;
  last_number = max_task = -1;
}


/*
** Save a task to it's task file.
*/
int taskSave ( task, taskdir )
Task *task;
char *taskdir;
{
  char *path;
  FILE *fp;
  int loop;

  path = (char *) malloc ( strlen ( taskdir ) + 10 );
  sprintf ( path, "%s/%d.task", taskdir, task->number );

  fp = fopen ( path, "w" );
  if ( !fp ) {
    free ( path );
    return ( TASK_ERROR_SYSTEM_ERROR );
  }

  fprintf ( fp, "Format: 1.2\n" );
  fprintf ( fp, "Name: %s\n", task->name );
  fprintf ( fp, "Created: %u\n", (unsigned int)task->created );
  fprintf ( fp, "Options: %u\n", task->options );
  fprintf ( fp, "Project: %d\n", task->project_id );
  fprintf ( fp, "Data:\n" );

  for ( loop = 0; loop < task->num_entries; loop++ ) {
    if ( task->entries[loop]->seconds )
      fprintf ( fp, "%04d%02d%02d %d\n",
        task->entries[loop]->year, task->entries[loop]->mon,
        task->entries[loop]->mday, task->entries[loop]->seconds );
  }

  fclose ( fp );
  free ( path );

  return ( 0 );
}


/*
** Save all tasks
*/
int taskSaveAll ( taskdir )
char *taskdir;
{
  int loop;
  int ret;

  for ( loop = 0; loop <= max_task; loop++ ) {
    if ( tasks[loop] ) {
      ret = taskSave ( tasks[loop], taskdir );
      if ( ret )
        return ( ret );
    }
  }
  return ( 0 );
}



/*
** Free all resources of a task.
*/
void taskFree ( task )
Task *task;
{
  int loop;
  free ( task->name );
  for ( loop = 0; loop < task->num_entries; loop++ )
    free ( task->entries[loop] );
  if ( task->entries )
    free ( task->entries );
  for ( loop = 0; loop < task->num_annotations; loop++ ) {
    free ( task->annotations[loop]->text );
    free ( task->annotations[loop] );
  }
  if ( task->annotations )
    free ( task->annotations );
  free ( task );
}



/*
** Load a task from file.
*/
int taskLoad ( path, task )
char *path;
Task **task;
{
  FILE *fp;
  int fd;
  Task *newtask;
  char line[512], *ptr, *ptr2, temp[10], *annfile, *anntext;
  int len, created, number, options, project_id = -1;
  TaskTimeEntry *entry;
  TaskAnnotation *a;
  struct stat buf;

  fp = fopen ( path, "r" );
  if ( !fp )
    return ( TASK_ERROR_SYSTEM_ERROR );

  for ( ptr = path + strlen ( path ) - 1; *ptr != '/' && ptr != path; ptr-- );
  if ( *ptr == '/' )
    ptr++;
  sscanf ( ptr, "%d.task", &number );

  fgets ( line, 512, fp );
  len = strlen ( line );
  if ( line[len-1] == '\n' )
    line[len-1] = '\0';
  if ( strcmp ( line, "Format: 1.0" ) && strcmp ( line, "Format: 1.1" ) &&
    strcmp ( line, "Format: 1.2" ) ) {
    fclose ( fp );
    return ( TASK_ERROR_BAD_FILE );
  }

  newtask = (Task *) malloc ( sizeof ( Task ) );
  memset ( newtask, '\0', sizeof ( Task ) );
  newtask->number = number;
  newtask->project_id = -1; /* no project */

  while ( fgets ( line, 512, fp ) ) {
    len = strlen ( line );
    if ( line[len-1] == '\n' )
      line[len-1] = '\0';
    if ( strncmp ( line, "Name:", 5 ) == 0 ) {
      ptr = line + 5;
      if ( *ptr == ' ' )
        ptr++;
      newtask->name = (char *) malloc ( strlen ( ptr ) + 1 );
      strcpy ( newtask->name, ptr );
    } else if ( strncmp ( line, "Created:", 8 ) == 0 ) {
      sscanf ( line + 8, "%d", &created );
      newtask->created = (time_t) created;
    } else if ( strncmp ( line, "Project:", 8 ) == 0 ) {
      sscanf ( line + 8, "%d", &project_id );
      newtask->project_id = project_id;
    } else if ( strncmp ( line, "Options:", 8 ) == 0 ) {
      sscanf ( line + 8, "%d", &options );
      newtask->options = (unsigned int) options;
    } else if ( strcmp ( line, "Data:" ) == 0 ) {
      while ( fgets ( line, 512, fp ) ) {
        entry = (TaskTimeEntry *) malloc ( sizeof ( TaskTimeEntry ) );
        memset ( entry, '\0', sizeof ( TaskTimeEntry ) );
        strncpy ( temp, line, 4 );
        temp[4] = '\0';
        entry->year = atoi ( temp );
        strncpy ( temp, line + 4, 2 );
        temp[2] = '\0';
        entry->mon = atoi ( temp );
        strncpy ( temp, line + 6, 2 );
        temp[2] = '\0';
        entry->mday = atoi ( temp );
        entry->seconds = atoi ( line + 9 );
        entry->marked_seconds = entry->seconds;
        if ( ! newtask->entries )
          newtask->entries = (TaskTimeEntry **) malloc ( 
            sizeof ( TaskTimeEntry * ) );
        else
          newtask->entries = (TaskTimeEntry **) realloc ( newtask->entries,
            sizeof ( TaskTimeEntry * ) * ( newtask->num_entries + 1 ) );
        newtask->entries[newtask->num_entries] = entry;
        newtask->num_entries++;
      }
      break;
    } else {
      fclose ( fp );
      taskFree ( newtask );
      return ( TASK_ERROR_BAD_FILE );
    }
  }
  fclose ( fp );

  /* now load annotations */
  annfile = (char *) malloc ( strlen ( path ) + 1 );
  strcpy ( annfile, path );
  ptr = annfile + strlen ( annfile ) - 5;
  if ( strcmp ( ptr, ".task" ) == 0 ) {
    strcpy ( ptr, ".ann" );
    if ( stat ( annfile, &buf ) == 0 ) {
      fd = open ( annfile, O_RDONLY );
      anntext = (char *) malloc ( buf.st_size + 1 );
      read ( fd, anntext, buf.st_size );
      anntext[buf.st_size] = '\0';
      close ( fd );
      ptr = strtok ( anntext, "\n" );
      while ( ptr ) {
        ptr2 = ptr;
        while ( isdigit ( *ptr2 ) )
          ptr2++;
        if ( *ptr2 == ' ' ) {
          *ptr2 = '\0';
          a = (TaskAnnotation *) malloc ( sizeof ( TaskAnnotation ) );
          memset ( a, '\0', sizeof ( TaskAnnotation ) );
          a->text_time = atoi ( ptr );
          ptr2++;
          a->text = (char *) malloc ( strlen ( ptr2 ) + 1 );
          strcpy ( a->text, ptr2 );
          for ( ptr2 = a->text; *ptr2 != '\0'; ptr2++ )
            if ( *ptr2 == '\r' )
              *ptr2 = '\n';
          if ( newtask->annotations == NULL ) {
            newtask->annotations = (TaskAnnotation **) malloc (
              sizeof ( TaskAnnotation * ) );
          } else {
            newtask->annotations = (TaskAnnotation **) realloc (
              newtask->annotations,
              ( newtask->num_annotations + 1 ) * sizeof ( TaskAnnotation * ) );
          }
          newtask->annotations[newtask->num_annotations] = a;
          newtask->num_annotations++;
        }
        ptr = strtok ( NULL, "\n" );
      }
      free ( anntext );
    }
  }
  free ( annfile );

  taskAdd ( newtask );

  *task = newtask;

  return ( 0 );
}



int taskLoadAll ( taskdir )
char *taskdir;
{
#ifdef WIN32
   char *pattern;
   Task *task;
   long handle;
   struct _finddata_t fdata;
   pattern = malloc ( strlen ( taskdir ) + 8 );
   (void) strcat ( strcpy ( pattern, taskdir ), "/*.task" );
   if ( ( handle = _findfirst ( pattern, &fdata ) ) != -1 ) {
     char *path = malloc ( strlen ( taskdir ) + _MAX_FNAME + 2 );
     char *start = path + strlen ( taskdir ) + 1;
     (void) strcat ( strcpy ( path, taskdir ), "/" );
     do {
       (void) strcpy ( start, fdata.name );
       if ( valid_name ( fdata.name ) && !_access ( path, 4 ) ){
         taskLoad ( path, &task );
       }
     } while ( !_findnext ( handle, &fdata ) );
     _findclose ( handle );
     free ( path );
   }
   else {
     free ( pattern );
     return ( TASK_ERROR_SYSTEM_ERROR );
   }
   free ( pattern );
#else
  DIR *dir;
  struct dirent *entry;
  struct stat buf;
  char *path, *ptr;
  Task *task;

  dir = opendir ( taskdir );
  if ( ! dir )
    return ( TASK_ERROR_SYSTEM_ERROR );
  while ( ( entry = readdir ( dir ) ) ) {
    path = (char *) malloc ( strlen ( taskdir ) + strlen ( entry->d_name )
      + 2 );
    sprintf ( path, "%s/%s", taskdir, entry->d_name );
    if ( stat ( path, &buf ) == 0 ) {
      for ( ptr = entry->d_name; isdigit ( *ptr ); ptr++ ) ;
      if ( strcmp ( ptr, ".task" ) == 0 && S_ISREG ( buf.st_mode ) ) {
        /* NOTE: add catching of errors here... */
        taskLoad ( path, &task );
      }
    }
    free ( path );
  }

#endif
  return ( 0 );
}


char *taskErrorString ( task_error )
int task_error;
{
  static char msg[256];

  switch ( task_error ) {
    case TASK_ERROR_SYSTEM_ERROR:
      sprintf ( msg, gettext("System Error (%d)"), errno );
      return ( msg );
    case TASK_ERROR_BAD_FILE:
      return ( gettext("Invalid task data file format") );
    default:
      sprintf ( msg,  gettext("Unknown error (%d)"), task_error );
      return ( msg );
  }
}




TaskTimeEntry *taskGetTimeEntry ( task, year, month, day )
Task *task;
int year, month, day;
{
  int loop;
  struct tm *tm;
  time_t now;

  if ( year < 100 ) {
    time ( &now );
    tm = localtime ( &now );
    year += 1900 + ( tm->tm_year % 100 );
  }
  
  for ( loop = 0; loop < task->num_entries; loop++ ) {
    if ( task->entries[loop]->year == year &&
      task->entries[loop]->mon == month &&
      task->entries[loop]->mday == day )
      return ( task->entries[loop] );
  }

  return ( NULL );
}




TaskTimeEntry *taskNewTimeEntry ( task, year, month, day )
Task *task;
int year, month, day;
{
  struct tm *tm;
  time_t now;
  TaskTimeEntry *ret;

  if ( year < 100 ) {
    time ( &now );
    tm = localtime ( &now );
    year += 1900 + ( tm->tm_year % 100 );
  }

  ret = (TaskTimeEntry *) malloc ( sizeof ( TaskTimeEntry ) );

  ret->year = year;
  ret->mon = month;
  ret->mday = day;
  ret->seconds = 0;
  ret->marked_seconds = 0;

  if ( task->entries )
    task->entries = (TaskTimeEntry **) realloc ( task->entries,
      ( task->num_entries + 1 ) * ( sizeof ( TaskTimeEntry * ) ) );
  else
    task->entries = (TaskTimeEntry **) malloc ( ( task->num_entries + 1 )
      * ( sizeof ( TaskTimeEntry * ) ) );
  task->entries[task->num_entries] = ret;
  task->num_entries++;

  return ( ret );
}


/*
** Get the options for the specified task.
*/
unsigned int taskGetOptions ( task )
Task *task;
{
  return task->options;
}


/*
** Determine if the specified option is enabled.
*/
unsigned int taskOptionEnabled ( task, option )
Task *task;
unsigned int option;
{
  if ( option & task->options )
    return 1;
  else
    return 0;
}


void taskSetOption ( task, option )
Task *task;
unsigned int option;
{
  task->options |= option;
}

void taskUnsetOption ( task, option )
Task *task;
unsigned int option;
{
  if ( taskOptionEnabled ( task, option ) )
    task->options -= option;
}

/*
** Add a task annotation to a task.  Can be more than one line of
** text.  We will convert '\n' into '\r' before saving and then
** back when loading.
** Note: It gets saved immediately (not when taskSave is called)
*/
void taskAddAnnotation ( task, taskdir, text )
Task *task;
char *taskdir;
char *text;
{
  char *ptr, *path, *newtext;
  TaskAnnotation *a;
  FILE *fp;

  a = (TaskAnnotation *) malloc ( sizeof ( TaskAnnotation ) );
  memset ( a, '\0', sizeof ( TaskAnnotation ) );
  time ( &a->text_time );
  a->text = (char *) malloc ( strlen ( text ) + 1 );
  strcpy ( a->text, text );

  if ( task->annotations == NULL ) {
    task->annotations = (TaskAnnotation **) malloc (
      sizeof ( TaskAnnotation * ) );
  } else {
    task->annotations = (TaskAnnotation **) realloc ( task->annotations,
      ( task->num_annotations + 1 ) * sizeof ( TaskAnnotation * ) );
  }
  task->annotations[task->num_annotations] = a;
  task->num_annotations++;

  /* now save to file */
  path = (char *) malloc ( strlen ( taskdir ) + 10 );
  sprintf ( path, "%s/%d.ann", taskdir, task->number );
  fp = fopen ( path, "a+" );
  if ( fp ) {
    newtext = (char *) malloc ( strlen ( text ) + 1 );
    strcpy ( newtext, text );
    for ( ptr = newtext; *ptr != '\0'; ptr++ )
      if ( *ptr == '\n' )
        *ptr = '\r';
    fprintf ( fp, "%d %s\n", (int)a->text_time, newtext );
    free ( newtext );
    fclose ( fp );
  }
  free ( path );
}



/*
** Get all annotations for the specified task on the specified day.
** NOTE: Caller must free return value.
** ??? Maybe we should take the midnight offset into consideration here ???
*/
TaskAnnotation **TaskGetAnnotationEntries ( task, year, month, day,
  time_offset, num_ret )
Task *task;
int year, month, day;
int time_offset;
int *num_ret;
{
  TaskAnnotation **ret = NULL;
  int loop = 0;
  struct tm *tm;
  int num = 0;
  time_t then;

  for ( loop = 0; loop < task->num_annotations; loop++ ) {
    then = task->annotations[loop]->text_time - time_offset;
    tm = localtime ( &then );
    if ( tm->tm_year + 1900 == year &&
      tm->tm_mon + 1 == month &&
      tm->tm_mday == day ) {
      if ( num ) {
        ret = (TaskAnnotation **) realloc ( ret,
          ( num + 1 ) * sizeof ( TaskAnnotation * ) );
      } else {
        ret = (TaskAnnotation **) malloc ( sizeof ( TaskAnnotation * ) );
      }
      ret[num] = task->annotations[loop];
      num++;
    }
  }

  *num_ret = num;
  return ( ret );
}