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

  1. /* install-todolist.c:  Pilot todo list installer
  2.  *
  3.  * Copyright 1996, Robert A. Kaplan
  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 "pi-source.h"
  12. #include "pi-socket.h"
  13. #include "pi-dlp.h"
  14. #include "pi-todo.h"
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18.   struct pi_sockaddr addr;
  19.   int db;
  20.   int sd;
  21.   int i;
  22.   int ToDo_size;
  23.   unsigned char ToDo_buf[0xffff];
  24.   struct ToDo todo;
  25.   FILE *f;
  26.   struct PilotUser U;
  27.   int ret;
  28.   int filelen;
  29.   char note_text[] = "";
  30.   char *cPtr;
  31.   char *begPtr;
  32.   char *file_text;
  33.   int cLen;
  34.   int count;
  35.  
  36. #ifndef NOTHAPPENING
  37.   if (argc < 3) {
  38.     fprintf(stderr,"usage:%s %s file [file] ...\n",argv[0],TTYPrompt);
  39.     exit(2);
  40.   }
  41.   if (!(sd = pi_socket(PI_AF_SLP, PI_SOCK_STREAM, PI_PF_PADP))) {
  42.     perror("pi_socket");
  43.     exit(1);
  44.   }
  45.     
  46.   addr.pi_family = PI_AF_SLP;
  47.   addr.pi_port = 3;
  48.   strcpy(addr.pi_device,argv[1]);
  49.   
  50.   ret = pi_bind(sd, &addr, sizeof(addr));
  51.   if(ret == -1) {
  52.     perror("pi_bind");
  53.     exit(1);
  54.   }
  55.  
  56.   ret = pi_listen(sd,1);
  57.   if(ret == -1) {
  58.     perror("pi_listen");
  59.     exit(1);
  60.   }
  61.  
  62.   sd = pi_accept(sd, 0, 0);
  63.   if(sd == -1) {
  64.     perror("pi_accept");
  65.     exit(1);
  66.   }
  67.  
  68.   /* Ask the pilot who it is. */
  69.   dlp_ReadUserInfo(sd,&U);
  70.   
  71.   /* Tell user (via Pilot) that we are starting things up */
  72.   dlp_OpenConduit(sd);
  73.   
  74.   /* Open the ToDo Pad's database, store access handle in db */
  75.   if(dlp_OpenDB(sd, 0, 0x80|0x40, "ToDoDB", &db) < 0) {
  76.     puts("Unable to open ToDoDB");
  77.     dlp_AddSyncLogEntry(sd, "Unable to open ToDoDB.\n");
  78.     exit(1);
  79.   }
  80. #endif
  81.   for (i=2; i<argc; i++) {
  82.  
  83.     f = fopen(argv[i], "r");
  84.     if (f == NULL) {
  85.       perror("fopen");
  86.       exit(1);
  87.     }
  88.  
  89.     fseek(f, 0, SEEK_END);
  90.     filelen = ftell(f);
  91.     fseek(f, 0, SEEK_SET);
  92.  
  93.     file_text = (char*)malloc(filelen+1);
  94.     if (file_text == NULL) {
  95.       perror("malloc()");
  96.       exit(1);
  97.     }
  98.  
  99.     fread(file_text, filelen, 1, f);
  100.  
  101.     /*printf("file: %s\n",file_text);*/
  102.  
  103.     cPtr = file_text;
  104.     begPtr = cPtr;
  105.     cLen = 0;
  106.     count = 0;
  107.     while(count < filelen) {
  108.       count++;
  109.        /*printf("c:%c.\n",*cPtr); */
  110.       if (*cPtr == '\n') {
  111.     todo.description = begPtr;
  112.     /* replace cr with terminator */
  113.     *cPtr = '\0';
  114.  
  115.     todo.priority = 4;
  116.     todo.complete = 1;
  117.     todo.indefinite = 1;
  118.     /*now = time(0);
  119.     todo.due = *localtime(&now);*/
  120.     todo.note = note_text;
  121.     pack_ToDo(&todo, ToDo_buf,&ToDo_size);
  122. printf("desc: %s\n",todo.description); 
  123. /* printf("todobuf: %s\n",ToDo_buf);       */
  124.     dlp_WriteRecord(sd, db, 0, 0, 0, ToDo_buf, ToDo_size, 0);
  125.     cPtr++;
  126.     begPtr = cPtr;
  127.     cLen = 0;
  128.       } else {
  129.     cLen++;
  130.     cPtr++;
  131.       }
  132.     }
  133.   }
  134.  
  135.   /* Close the database */
  136.   dlp_CloseDB(sd, db);
  137.  
  138.   /* Tell the user who it is, with a different PC id. */
  139.   U.lastSyncPC = 0x00010000;
  140.   U.succSyncDate = time(NULL);
  141.   U.lastSyncDate = U.succSyncDate;
  142.   dlp_WriteUserInfo(sd,&U);
  143.  
  144.   dlp_AddSyncLogEntry(sd, "Wrote ToDo to Pilot.\n");
  145.   
  146.   /* All of the following code is now unnecessary, but harmless */
  147.   
  148.   dlp_EndOfSync(sd,0);
  149.   pi_close(sd);
  150.   exit(0);
  151. }
  152.