home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 August / PCO0897.ISO / filesbbs / os2 / plnk065.arj / PLNK065.ZIP / pilot-link.0.6.5 / libsock / todo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-23  |  4.1 KB  |  143 lines

  1. /* todo.c:  Translate Pilot ToDo application data formats
  2.  *
  3.  * Copyright (c) 1996, Kenneth Albanowski
  4.  *
  5.  * This is free software, licensed under the GNU Public License V2.
  6.  * See the file COPYING for details.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "pi-source.h"
  13. #include "pi-dlp.h"
  14. #include "pi-todo.h"
  15.  
  16. void free_ToDo(struct ToDo * a) {
  17.   if(a->description)
  18.     free(a->description);
  19.   if(a->note)
  20.     free(a->note);
  21. }
  22.  
  23. void unpack_ToDo(struct ToDo * a, unsigned char * buffer, int len) {
  24.   unsigned long d;
  25.  
  26.   /* Note: There are possible timezone conversion problems related to the
  27.            use of the due member of a struct ToDo. As it is kept in local
  28.            (wall) time in struct tm's, the timezone of the Pilot is
  29.            irrelevant, _assuming_ that any UNIX program keeping time in
  30.            time_t's converts them to the correct local time. If the Pilot is
  31.            in a different timezone than the UNIX box, it may not be simple
  32.            to deduce that correct (desired) timezone.
  33.                                                                                
  34.            The easiest solution is to keep apointments in struct tm's, and
  35.            out of time_t's. Of course, this might not actually be a help if
  36.            you are constantly darting across timezones and trying to keep
  37.            appointments.
  38.                                                                     -- KJA
  39.            */
  40.                                                                                                                                                                                                           
  41.                                                                                                                                                                                                             
  42.   d = (unsigned short int)get_short(buffer);
  43.   if (d != 0xffff) {
  44.     a->due.tm_year = (d >> 9) + 4;
  45.     a->due.tm_mon = ((d >> 5) & 15) - 1;
  46.     a->due.tm_mday = d & 31;
  47.     a->due.tm_hour = 0;
  48.     a->due.tm_min = 0;
  49.     a->due.tm_sec = 0;
  50.     a->due.tm_isdst = -1;
  51.     mktime(&a->due);
  52.     a->indefinite = 0;
  53.   } else {
  54.     a->indefinite = 1; /* a->due is invalid */
  55.   }
  56.  
  57.   a->priority = get_byte(buffer+2);
  58.   if(a->priority & 0x80) {
  59.     a->complete = 1;
  60.     a->priority &= 0x7f;
  61.   } else {
  62.     a->complete = 0;
  63.   }
  64.   
  65.   a->description = strdup((char*)buffer+3);
  66.   a->note = strdup((char*)buffer+3+strlen((char*)buffer+3)+1);
  67. }
  68.  
  69. void pack_ToDo(struct ToDo *a, unsigned char * buf, int * len) {
  70.   int pos;
  71.  
  72.   if (a->indefinite) {
  73.     buf[0] = 0xff;
  74.     buf[1] = 0xff;
  75.   } else {
  76.     set_short(buf, ((a->due.tm_year - 4) << 9) |
  77.                    ((a->due.tm_mon  + 1) << 5) |
  78.                    a->due.tm_mday);
  79.   }
  80.   buf[2] = a->priority;
  81.   if(a->complete) {
  82.     buf[2] |= 0x80;
  83.   }
  84.   
  85.   pos = 3;
  86.   if(a->description) {
  87.     strcpy((char*)buf+pos, a->description);
  88.     pos += strlen(a->description)+1;
  89.   } else {
  90.     buf[pos++] = 0;
  91.   }
  92.   
  93.   if(a->note) {
  94.     strcpy((char*)buf+pos, a->note);
  95.     pos += strlen(a->note)+1;
  96.   } else {
  97.     buf[pos++] = 0;
  98.   }
  99.   
  100.   if (len)
  101.     *len = pos;
  102. }
  103.  
  104.                   
  105. void unpack_ToDoAppInfo(struct ToDoAppInfo * ai, unsigned char * record, int len) {
  106.   int i;
  107.   ai->renamedcategories = get_short(record);
  108.   record+=2;
  109.   for(i=0;i<16;i++) {
  110.     memcpy(ai->CategoryName[i], record, 16);
  111.     record += 16;
  112.   }
  113.   memcpy(ai->CategoryID, record, 16);
  114.   record += 16;
  115.   ai->lastUniqueID = get_byte(record);
  116.   record += 4;
  117.   ai->dirty = get_short(record);
  118.   record += 2;
  119.   ai->sortByPriority = get_byte(record);
  120.   record += 2;
  121. }
  122.  
  123. void pack_ToDoAppInfo(struct ToDoAppInfo * ai, unsigned char * record, int * len) {
  124.   int i;
  125.   set_short(record, ai->renamedcategories);
  126.   record += 2;
  127.   for(i=0;i<16;i++) {
  128.     memcpy(record, ai->CategoryName[i], 16);
  129.     record += 16;
  130.   }
  131.   memcpy(record, ai->CategoryID, 16);
  132.   record += 16;
  133.   set_byte(record, ai->lastUniqueID);
  134.   record ++;
  135.   set_byte(record, 0); /* gapfil */
  136.   set_short(record+1, 0); /* gapfil */
  137.   set_short(record+3, ai->dirty);
  138.   set_byte(record+5, ai->sortByPriority);
  139.   set_byte(record+6, 0); /* gapfil */
  140.   
  141.   *len = 2 + (16*16) + 16 + 2 + 2 + 2 + 2;
  142. }
  143.