home *** CD-ROM | disk | FTP | other *** search
/ PDA Software Library / pdasoftwarelib.iso / PSION / COMMS / PSIONMAI / PMFULLSO / SUNMAIL / META.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-04  |  7.1 KB  |  290 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "project.h"
  4. /* start of meta file operations code */
  5. /* the meta file consists of line terminated data in the following format */
  6. /* the full path component, E.g. LOC::M\MAIL\ */
  7. /* mu current email adress, e.g. tim.graves@uk.sun.com the following repeated */
  8. /* datafile name, E.g. in1.msg or out1.msg */
  9. /* to, a list of recieved addresses, e.g. tim.graves@uk.sun.com MUST contain at least one address */
  10. /* from, the origionator of the email */
  11. /* subject, obvious */
  12. /* cc, the cc list */
  13. /* bcc, the bcc list (empty on reciept )*/
  14. /* read, N if the email is unread, R if read */
  15. /*at the end of the repeats */
  16. /* END */
  17. /* No other items */
  18. /* */
  19. /* read in the data descriptions */
  20. int readmeta(fname, itemlist)
  21. char * fname ;
  22. struct ll * itemlist;
  23. {
  24.     FILE * fd ;
  25.     int ret ;
  26.     if (mtadbg == TRUE)
  27.         printf("CALL: readmeta (fname = %s, itemlist = OMITTED)", fname) ;
  28.     if ((fd = fopen(fname, "r")) == NULL)
  29.     {
  30.         printf("FILE ERROR:  readmeta, error in opening %s", fname) ;
  31.         return (FILEERR) ;
  32.     }
  33.     /* load the one off data */
  34.     ret = getf(fd, itemlist->mydata->mypath) ;
  35.     if ( ret == FALSE)
  36.     {
  37.         printf("FILE ERROR: readmeta, error in reading my path from %s", fname) ;
  38.         return(FILEERR) ;
  39.     }
  40.     ret = getf(fd, itemlist->mydata->myaddr) ;
  41.     if (ret == FALSE)
  42.     {
  43.         printf("FILE ERROR: readmeta, error in reading my address from %s", fname) ;
  44.         return(FILEERR) ;
  45.     }
  46.     while((ret = readhdr(fd, itemlist))== TRUE)
  47.         ;
  48.     fclose(fd) ;
  49.     if (ret == FILEERR)
  50.     {
  51.         printf("FILE ERROR: readmeta, error in reading meta record from %s", fname) ;
  52.         return(FILEERR) ;
  53.     }
  54.     return(TRUE) ;
  55. }
  56.  
  57. /* write the data descriptions out */
  58. wrtmeta(fname, itemlist)
  59. char * fname;
  60. struct ll * itemlist ;
  61. {
  62.     FILE * fd ;
  63.     int ret ;
  64.         
  65.     if (mtadbg == TRUE)
  66.         printf("CALL: wrtmeta (fname = %s, itemlist = OMITTED)", fname) ;
  67.     ret = unlink(fname) ; /* delete the origional file */
  68.     if (ret < 0)
  69.     {
  70.         printf("FILE ERROR: wrtmeta, error in deleting %s prior to output", fname) ;
  71.         return(FILEERR) ;
  72.     }
  73.     if ((fd = fopen(fname, "w")) == NULL)
  74.     {
  75.         printf("FILE ERROR: wrtmeta, error in opening %s for output", fname) ;
  76.         return(FILEERR) ;
  77.     }
  78.     /* write the one off data */
  79.     ret = putf(fd, itemlist->mydata->mypath) ;
  80.     if (ret == FILEERR)
  81.     {
  82.         printf("FILE ERROR: wrtmeta, error in writting my path to %s", fname) ;
  83.         fclose(fd) ;
  84.         return(FILEERR) ;
  85.     }
  86.     ret = putf(fd, itemlist->mydata->myaddr) ;
  87.     if (ret == FILEERR)
  88.     {
  89.         printf("FILE ERROR: wrtmeta, error in writting my address to %s", fname) ;
  90.         fclose(fd) ;
  91.         return(FILEERR) ;
  92.     }
  93.     while(itemlist != NULL)
  94.     {
  95.         ret = wrthdr(fd, itemlist) ;
  96.         if (ret == FILEERR)
  97.             break ;
  98.         itemlist = itemlist->next ;
  99.     }
  100.     if (ret == FILEERR)
  101.     {
  102.         printf("FILE ERROR: wrtmeta, error in writting record to %s", fname) ;
  103.         fclose(fd) ;
  104.         return(FILEERR) ;
  105.     }
  106.     ret = putf(fd, "END") ;
  107.     if (ret == FILEERR)
  108.     {
  109.         printf("FILE ERROR: wrtmeta, error in writting terminator (END) to %s", fname) ;
  110.         fclose(fd) ;
  111.         return(FILEERR) ;
  112.     }
  113.     fclose(fd) ;
  114.     return(TRUE) ;
  115. }
  116.  
  117. /* read the from, subject and return path etc from the data file */
  118. readhdr(fd, itemlist)
  119. FILE * fd;
  120. struct ll * itemlist;
  121. {
  122.     struct ll * newll ;
  123.     char datafile[20] ;
  124.     char to[100] ;
  125.     char from[100] ;
  126.     char subject[100];
  127.     char cc[100];
  128.     char bcc[100];
  129.     char cread[100] ;
  130.     int read, ret ;
  131.     if (mtadbg == TRUE)
  132.         printf("CALL: readhdr(fd = OMITTED, itemlist = OMITTED)");
  133.     /* read in an entry */
  134.     ret = getf(fd, datafile) ;
  135.     if (ret == FILEERR)
  136.     {
  137.         printf("FILE ERROR: readhdr, error in reading datafile") ;
  138.         return(FILEERR) ;
  139.     }
  140.     if (strcmp(datafile, "END") == 0)
  141.         return(FALSE) ;
  142.     ret = getf(fd, to) ;
  143.     if (ret == FILEERR)
  144.     {
  145.         printf("FILE ERROR: readhdr, error in reading to") ;
  146.         return(FILEERR) ;
  147.     }
  148.     ret = getf(fd, from) ;
  149.     if (ret == FILEERR)
  150.     {
  151.         printf("FILE ERROR: readhdr, error in reading from") ;
  152.         return(FILEERR) ;
  153.     }
  154.     ret = getf(fd, subject) ;
  155.     if (ret == FILEERR)
  156.     {
  157.         printf("FILE ERROR: readhdr, error in reading subject") ;
  158.         return(FILEERR) ;
  159.     }
  160.     ret = getf(fd, cc) ;
  161.     if (ret == FILEERR)
  162.     {
  163.         printf("FILE ERROR: readhdr, error in reading cc") ;
  164.         return(FILEERR) ;
  165.     }
  166.     ret = getf(fd, bcc) ;
  167.     if (ret == FILEERR)
  168.     {
  169.         printf("FILE ERROR: readhdr, error in reading bcc") ;
  170.         return(FILEERR) ;
  171.     }
  172.     ret = getf(fd, cread) ;
  173.     if (ret == FILEERR)
  174.     {
  175.         printf("FILE ERROR: readhdr, error in reading read") ;
  176.         return(FILEERR) ;
  177.     }    
  178.     if (cread[0] == 'R')
  179.         read = RD ;
  180.     else
  181.         read = NEW ;
  182.     
  183.     /* get a new ll structure */
  184.     newll = llnew ();
  185.     /* fill in the structure */
  186.     llfill(newll, datafile, to, from, subject, cc, bcc, ACTIVE, read, itemlist->count + 1) ;
  187.     /* add the new item into the list */
  188.     lladd(newll, itemlist) ;
  189.     return(TRUE) ;
  190. }
  191. /* write a subject and destination etc to the data file */
  192. wrthdr(fd,item)
  193. FILE * fd;
  194. struct ll * item;
  195. {
  196.     int ret ;
  197.     if (mtadbg == TRUE)
  198.         printf("CALL: wrthdr(fd = OMITTED, item = OMITTED)");
  199.     /* only outut the record if its active */
  200.     if (item->status != ACTIVE)
  201.         return(TRUE) ;
  202.     ret = putf(fd, item->datafile);
  203.     if ( ret == FILEERR)
  204.     {
  205.         printf("FILE ERROR: wrthdr, error in writting datafile (%s)", item->datafile) ;
  206.         return(FILEERR) ;
  207.     }
  208.     ret = putf(fd, item->to) ;
  209.     if ( ret == FILEERR)
  210.     {
  211.         printf("FILE ERROR: wrthdr, error in writting to (%s)", item->to) ;
  212.         return(FILEERR) ;
  213.     }
  214.     ret = putf(fd, item->from) ;
  215.     if ( ret == FILEERR)
  216.     {
  217.         printf("FILE ERROR: wrthdr, error in writting from (%s)", item->from) ;
  218.         return(FILEERR) ;
  219.     }
  220.     ret = putf(fd, item->subject) ;
  221.     if ( ret == FILEERR)
  222.     {
  223.         printf("FILE ERROR: wrthdr, error in writting subject (%s)", item->subject) ;
  224.         return(FILEERR) ;
  225.     }
  226.     ret = putf(fd, item->cc) ;
  227.     if ( ret == FILEERR)
  228.     {
  229.         printf("FILE ERROR: wrthdr, error in writting cc (%s)", item->cc) ;
  230.         return(FILEERR) ;
  231.     }
  232.     ret = putf(fd, item->bcc) ;
  233.     if ( ret == FILEERR)
  234.     {
  235.         printf("FILE ERROR: wrthdr, error in writting bcc (%s)", item->bcc) ;
  236.         return(FILEERR) ;
  237.     }
  238.     if (item->read == RD)
  239.         ret = putf(fd, "R") ;
  240.     else
  241.         ret = putf(fd, "N") ;
  242.     if ( ret == FILEERR)
  243.     {
  244.         printf("FILE ERROR: wrthdr, error in writting read (%s)", item->read == RD ? "R" : "N") ;
  245.         return(FILEERR) ;
  246.     }
  247.     return(TRUE) ;
  248. }
  249. /* end of meta file operations code */
  250.  
  251. /* start of ancillary file and keyboard IO code */
  252. /* read a line of text from file handle */
  253. getf(fd, line)
  254. FILE * fd;
  255. char * line;
  256. {
  257.     char *ret ;
  258.     if (ancdbg == TRUE)
  259.         printf("CALL: getf(fd = OMITTED, line = OMITTED)");
  260.     if (fgets(line, 1024, fd) == NULL)
  261.         return(FILEERR) ;
  262.     /* remove the newline */
  263.     if ((int) strlen(line) > 0)
  264.         if (line[strlen(line)-1] == '\n')
  265.             line[strlen(line) -1] = '\0' ;
  266.     if (ancdbg == TRUE)
  267.     {
  268.         printf("line is :%s:", line) ;
  269.     }
  270.  
  271.     return(TRUE) ;
  272. }
  273. /* write a line of text to file handle */
  274. putf(fd, line)
  275. FILE * fd;
  276. char * line;
  277. {
  278.     int ret ;
  279.     if (ancdbg == TRUE)
  280.         printf("CALL: putf (fd = OMITTED, line = %s)", line) ;
  281.     ret = fprintf(fd, "%s\n", line) ;
  282.     if (ret < 0)
  283.         return(FILEERR) ;
  284.     if (ancdbg == TRUE)
  285.     {
  286.         printf("ret is %d, line is :%s:", ret, line) ;
  287.     }
  288.     return(TRUE) ;
  289. }
  290.