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 / expense.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-23  |  5.5 KB  |  258 lines

  1. /* expense.c:  Translate Pilot expense tracker data formats
  2.  *
  3.  * Copyright (c) 1997, 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-socket.h"
  14. #include "pi-dlp.h"
  15. #include "pi-expense.h"
  16.  
  17. void free_Expense(struct Expense * a) {
  18.   if (a->note)
  19.     free(a->note);
  20.   if (a->amount)
  21.     free(a->amount);
  22.   if (a->city)
  23.     free(a->city);
  24.   if (a->vendor)
  25.     free(a->vendor);
  26.   if (a->attendees)
  27.     free(a->attendees);
  28. }
  29.  
  30. void unpack_Expense(struct Expense * a, unsigned char * buffer, int len)
  31. {
  32.   unsigned long d;
  33.                                                                                                                                                                                                             
  34.   d = (unsigned short int)get_short(buffer);
  35.   a->date.tm_year = (d >> 9) + 4;
  36.   a->date.tm_mon = ((d >> 5) & 15) - 1;
  37.   a->date.tm_mday = d & 31;
  38.   a->date.tm_hour = 0;
  39.   a->date.tm_min = 0;
  40.   a->date.tm_sec = 0;
  41.   a->date.tm_isdst = -1;
  42.   mktime(&a->date);
  43.   
  44.   a->type = get_byte(buffer+2);
  45.   a->payment = get_byte(buffer+3);
  46.   a->currency = get_byte(buffer+4);
  47.   
  48.   buffer += 6;
  49.   
  50.   if (*buffer) {
  51.     a->amount = strdup(buffer);
  52.     buffer += strlen(a->amount);
  53.   } else {
  54.     a->amount = 0;
  55.   }
  56.   buffer++;
  57.  
  58.   if (*buffer) {
  59.     a->vendor = strdup(buffer);
  60.     buffer += strlen(a->vendor);
  61.   } else {
  62.     a->vendor = 0;
  63.   }
  64.   buffer++;
  65.  
  66.   if (*buffer) {
  67.     a->city = strdup(buffer);
  68.     buffer += strlen(a->city);
  69.   } else {
  70.     a->city = 0;
  71.   }
  72.   buffer++;
  73.  
  74.  
  75.   if (*buffer) {
  76.     a->attendees = strdup(buffer);
  77.     buffer += strlen(a->attendees);
  78.   } else {
  79.     a->attendees = 0;
  80.   }
  81.   buffer++;
  82.  
  83.   if (*buffer) {
  84.     a->note = strdup(buffer);
  85.     buffer += strlen(a->note);
  86.   } else {
  87.     a->note = 0;
  88.   }
  89.   buffer++;
  90. }
  91.  
  92. void pack_Expense(struct Expense * a, unsigned char * record, int * len)
  93. {
  94.   unsigned char * buf = record;
  95.   
  96.   set_short(buf, ((a->date.tm_year - 4) << 9) |
  97.                    ((a->date.tm_mon  + 1) << 5) |
  98.                    a->date.tm_mday);
  99.   buf += 2;
  100.   set_byte(buf, a->type);
  101.   set_byte(buf+1, a->payment);
  102.   set_byte(buf+2, a->currency);
  103.   set_byte(buf+3, 0); /*gapfil*/ 
  104.   buf += 4;
  105.   
  106.   if (a->amount) {
  107.     strcpy(buf, a->amount);
  108.     buf += strlen(buf);
  109.   } else {
  110.     set_byte(buf, 0);
  111.   }
  112.   buf++;
  113.  
  114.   if (a->vendor) {
  115.     strcpy(buf, a->vendor);
  116.     buf += strlen(buf);
  117.   } else {
  118.     set_byte(buf, 0);
  119.   }
  120.   buf++;
  121.   
  122.   if (a->city) {
  123.     strcpy(buf, a->city);
  124.     buf += strlen(buf);
  125.   } else {
  126.     set_byte(buf, 0);
  127.   }
  128.   buf++;
  129.   
  130.   if (a->attendees) {
  131.     strcpy(buf, a->attendees);
  132.     buf += strlen(buf);
  133.   } else {
  134.     set_byte(buf, 0);
  135.   }
  136.   buf++;
  137.   
  138.   if (a->note) {
  139.     strcpy(buf, a->note);
  140.     buf += strlen(buf);
  141.   } else {
  142.     set_byte(buf, 0);
  143.   }
  144.   buf++;
  145.   
  146.   if (len)
  147.     *len = (buf-record);
  148. }
  149.  
  150. void unpack_ExpenseAppInfo(struct ExpenseAppInfo * ai, unsigned char * record, int len)
  151. {
  152.   int i;
  153.   ai->renamedcategories = get_short(record);
  154.   record+=2;
  155.   for(i=0;i<16;i++) {
  156.     memcpy(ai->CategoryName[i], record, 16);
  157.     record += 16;
  158.   }
  159.   memcpy(ai->CategoryID, record, 16);
  160.   record += 16;
  161.   ai->lastUniqueID = get_byte(record);
  162.   record += 4;
  163.   ai->sortOrder = get_byte(record);
  164.   record += 2;
  165.   for(i=0;i<4;i++) {
  166.     memcpy(ai->currencies[i].name, record, 16);
  167.     record+=16;
  168.     memcpy(ai->currencies[i].symbol, record, 4);
  169.     record+=4;
  170.     memcpy(ai->currencies[i].rate, record, 8);
  171.     record+=8;
  172.   }
  173. }
  174.  
  175. void pack_ExpenseAppInfo(struct ExpenseAppInfo * ai, unsigned char * record, int * len)
  176. {
  177.   unsigned char * start = record;
  178.   int i;
  179.   set_short(record, ai->renamedcategories);
  180.   record += 2;
  181.   for(i=0;i<16;i++) {
  182.     memcpy(record, ai->CategoryName[i], 16);
  183.     record += 16;
  184.   }
  185.   memcpy(record, ai->CategoryID, 16);
  186.   record += 16;
  187.   set_long(record, 0); /* gapfil */
  188.   set_byte(record, ai->lastUniqueID);
  189.   record += 4;
  190.   set_byte(record, ai->sortOrder);
  191.   record += 2;
  192.   for(i=0;i<4;i++) {
  193.     memcpy(record, ai->currencies[i].name, 16);
  194.     record+=16;
  195.     memcpy(record, ai->currencies[i].symbol, 4);
  196.     record+=4;
  197.     memcpy(record, ai->currencies[i].rate, 8);
  198.     record+=8;
  199.   }
  200.   if (len)
  201.     *len = (record-start);
  202. }
  203.  
  204. void unpack_ExpensePrefs(struct ExpensePrefs * p, unsigned char * record, int len)
  205. {
  206.   int i;
  207.   p->currentCategory = get_short(record);
  208.   record += 2;
  209.   p->defaultCategory = get_short(record);
  210.   record += 2;
  211.   p->noteFont = get_byte(record);
  212.   record++;
  213.   p->showAllCategories = get_byte(record);
  214.   record++;
  215.   p->showCurrency = get_byte(record);
  216.   record++;
  217.   p->saveBackup = get_byte(record);
  218.   record++;
  219.   p->allowQuickFill = get_byte(record);
  220.   record++;
  221.   p->unitOfDistance = get_byte(record);
  222.   record += 2;
  223.   for(i=0;i<7;i++) {
  224.     p->currencies[i] = get_byte(record);
  225.     record++;
  226.   }
  227. }
  228.  
  229. void pack_ExpensePrefs(struct ExpensePrefs * p, unsigned char * record, int * len)
  230. {
  231.   int i;
  232.   unsigned char * start = record;
  233.   set_short(record, p->currentCategory);
  234.   record += 2;
  235.   set_short(record, p->defaultCategory);
  236.   record += 2;
  237.   set_byte(record, p->noteFont);
  238.   record++;
  239.   set_short(record, p->showAllCategories);
  240.   record++;
  241.   set_byte(record, p->showCurrency);
  242.   record++;
  243.   set_byte(record, p->saveBackup);
  244.   record++;
  245.   set_byte(record, p->allowQuickFill);
  246.   record++;
  247.   set_byte(record, p->unitOfDistance);
  248.   record++;
  249.   set_byte(record, 0); /* gapfil ?? */
  250.   record++;
  251.   for(i=0;i<7;i++) {
  252.     set_byte(record, p->currencies[i]);
  253.     record++;
  254.   }
  255.   if (len)
  256.     *len = record-start;
  257. }
  258.