home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / MSGDP206.SZH / TEXTFILE.C < prev    next >
C/C++ Source or Header  |  1990-07-31  |  6KB  |  315 lines

  1. /*
  2.  
  3. Title:  MsgEd
  4.  
  5. File:   textfile.c
  6.  
  7. Author: Jim Nutt
  8.  
  9. Copr:    released into the PUBLIC DOMAIN 30 Jul 1990 by jim nutt 
  10.  
  11. Description:
  12.  
  13.     handles import and export of textfiles
  14.  
  15. Revision History:
  16.  
  17.     0.00    4 July 1988
  18.  
  19. Support Files:
  20.  
  21.     msged.h
  22.  
  23. */
  24.  
  25. #include "msged.h"
  26. #include "date.h"
  27. #include "menu.h"
  28. #ifdef __MSC__
  29. #include <sys/types.h>
  30. #endif
  31. #include <sys\stat.h>
  32.  
  33. #define TEXTLEN 2048
  34.  
  35. char * _pascal makequote(void);
  36. int  _pascal wrap(LINE *cl, int x, int y);
  37. FILE * _pascal repapp(char *path);
  38.  
  39. void _pascal import(LINE * l)
  40.  
  41. {
  42.     char   line[TEXTLEN];
  43.     FILE   *fp;
  44.     static char fn[PATHLEN];
  45.     LINE   *n;
  46.  
  47.     memset(line, 0, sizeof(line));
  48.  
  49.     gotoxy(9, 1);
  50.     clreol();
  51.     set_color(co_info);
  52.     bputs("File to import? ");
  53.     bgets(fn, PATHLEN,PATHLEN);
  54.     set_color(co_normal);
  55.  
  56.     if ((fp = fopen(fn, "rt")) != NULL) {
  57.         while (fgets(line, TEXTLEN, fp) != NULL) {
  58.             if (l->text != NULL) {
  59.                 if ((n = (LINE *) calloc(1, sizeof(LINE))) == NULL) {
  60.                     gotoxy(9, 1);
  61.                     clreol();
  62.                     bputs("Not enough memory, press any key");
  63.                     getkey();
  64.                     showheader(message);
  65.                     return;
  66.                 }
  67.                 n->next = l->next;
  68.                 if (n->next != NULL)
  69.                     n->next->prev = n;
  70.                 n->prev = l;
  71.                 l->next = n;
  72.                 l = n;
  73.             }
  74.             else
  75.                 n = l;
  76.  
  77.             n->text = strdup(line);
  78.             if (strlen(n->text) > (size_t) rm) {
  79.                 l = n->next;
  80.                 wrap(n,maxx,maxy);
  81.                 if (l == NULL)
  82.                     while (n->next)
  83.                         n = n->next;
  84.                 else
  85.                     n = l->prev;
  86.  
  87.                 l = n;
  88.             }
  89.             memset(line, 0, sizeof(line));
  90.         }
  91.         fclose(fp);
  92.     }
  93.  
  94.     showheader(message);
  95. }
  96.  
  97. void _pascal export(LINE * f)
  98. {
  99.     FILE   *fp;
  100.     char   fn[PATHLEN];
  101.  
  102.     gotoxy(9, 1);
  103.     clreol();
  104.     set_color(co_info);
  105.     bputs("File name to write to? ");
  106.     memset(fn, 0, sizeof(fn));
  107.     strcpy(fn, outfile);
  108.     bgets(fn, sizeof(fn),sizeof(fn));
  109.     free(outfile);
  110.     outfile = strdup(fn);
  111.     set_color(co_normal);
  112.  
  113.     if (*fn == '+')
  114.         fp = fopen(fn+1,"at");
  115.     else
  116.         fp = fopen(fn,"wt");
  117.  
  118.     if (fp != NULL) {
  119.  
  120.         fputc('\n', fp);
  121.  
  122.         for (; f != NULL; f = f->next)
  123.             if ((f->text != NULL) && ((*(f->text) != '\01') || shownotes)) {
  124.                 fputs(f->text, fp);
  125.                 if (strchr(f->text, '\n') == NULL)
  126.                     fputc('\n', fp);
  127.             }
  128.  
  129.         fclose(fp);
  130.     }
  131.  
  132.     showheader(message);
  133. }
  134.  
  135. void _pascal writetxt()
  136.  
  137. {
  138.     LINE   *f = message->text;
  139.     FILE   *fp;
  140.     char    fn[PATHLEN];
  141.     static char *modes[] = {"Text", "Quote", "Msged", NULL};
  142.     static char *ovr[] = {"Replace", "Append", NULL}; 
  143.     char   *s;
  144.     char   *l;
  145.     static int mode = 0;
  146.  
  147.     gotoxy(9, 1);
  148.     clreol();
  149.     set_color(co_info);
  150.     bputs("File name to write to? ");
  151.     memset(fn, 0, sizeof(fn));
  152.     strcpy(fn, outfile);
  153.     bgets(fn, sizeof(fn),sizeof(fn));
  154.     free(outfile);
  155.     outfile = strdup(fn);
  156.     set_color(co_normal);
  157.  
  158.     if ((s = strchr(fn,',')) != NULL)
  159.         *s++ = '\0';
  160.  
  161.     if (s && (*s == 't')) mode = 0;
  162.     if (s && (*s == 'q')) mode = 1;
  163.     if ((s && (*s == 'm')) || (s == NULL)) mode = 2;
  164.  
  165.     if (*fn == '?') {
  166.         box(60,1,70,5,1);
  167.         mode = menu(61,2,69,4,modes,co_hilite,co_normal,mode);
  168.         clrwnd(60,1,70,5);
  169.         if (mode == -1) {
  170.             mode = 0;
  171.             return;
  172.         }
  173.     }
  174.  
  175.     if (*fn == '+')
  176.         fp = fopen(fn+1,"at");
  177.  
  178.     else if (*fn == '?') {
  179.         if ((fp = fopen(fn+1,"rt")) == NULL)
  180.             fp = fopen(fn+1,"wt");
  181.         else if (isatty(fileno(fp))) {
  182.             fclose(fp);
  183.             fp = fopen(fn+1,"wt");
  184.             mode = 0;
  185.         }
  186.         else {    
  187.             int i;
  188.             box(60,1,70,4,1);
  189.             i = menu(61,2,69,3,ovr,co_hilite,co_normal,0);
  190.             if (i == -1) {
  191.                 clrwnd(60,1,70,4);
  192.                 return;
  193.             }
  194.             fclose(fp);
  195.             if (i) 
  196.                 fp = fopen(fn+1,"at");
  197.             else
  198.                 fp = fopen(fn+1,"wt");
  199.         }
  200.     }
  201.     else
  202.         fp = fopen(fn,"wt");
  203.  
  204.     if (fp == NULL)
  205.         return;
  206.  
  207.     if (mode == 0) {
  208.         fprintf(fp, "\nDate:   %s\n", atime(message->timestamp));
  209.         fprintf(fp, "From:   %s", message->isfrom);
  210.         if (arealist[area].netmail)
  211.             fprintf(fp," of %s",show_address(message->from));
  212.         fputc('\n', fp);
  213.         fprintf(fp, "To:     %s", message->isto);
  214.         if (arealist[area].netmail)
  215.             fprintf(fp," of %s",show_address(message->to));
  216.         fputc('\n', fp);
  217.         if (message->attrib.attached)
  218.             fprintf(fp, "Files:  %s", message->subj);
  219.         else
  220.             fprintf(fp, "Subj:   %s", message->subj);
  221.         fputc('\n', fp);
  222.         fputs("Attr:   ", fp);
  223.  
  224.         if (message->attrib.private)
  225.             fputs("privileged ", fp);
  226.         if (message->attrib.crash)
  227.             fputs("crash ", fp);
  228.         if (message->attrib.recvd)
  229.             fputs("recvd ", fp);
  230.         if (message->attrib.sent)
  231.             fputs("sent ", fp);
  232.         if (message->attrib.attached)
  233.             fputs("f/a ", fp);
  234.         if (message->attrib.killsent)
  235.             fputs("kill/sent ", fp);
  236.         if (message->attrib.freq)
  237.             fputs("freq ", fp);
  238.         if (message->attrib.rreq)
  239.             fputs("rreq ", fp);
  240.         if (message->attrib.areq)
  241.             fputs("areq ", fp);
  242.         if (message->attrib.ureq)
  243.             fputs("ureq ", fp);
  244.         fputc('\n', fp);
  245.         fprintf(fp,"%-30s -------------------------------\n",arealist[area].description);
  246.     }
  247.     else if (mode == 1) {
  248.         l = makequote();
  249.         fputs(l,fp);
  250.         fputc('\n',fp);
  251.         f = message->text;
  252.     }
  253.  
  254.     for (; f != NULL; f = f->next) {
  255.         if ((f->text != NULL) && ((*(f->text) != '\01') || shownotes)) {
  256.             fputs(f->text, fp);
  257.             if ((strchr(f->text, '\n') == NULL) && ((mode == 0) || (mode == 1)))
  258.                 fputc('\n', fp);
  259.         }
  260.     }
  261.  
  262.     if (isatty(fileno(fp)))
  263.         fputc(12, fp);
  264.  
  265.     fclose(fp);
  266. }
  267.  
  268. FILE * _pascal repapp(char *path)
  269.  
  270. {
  271.     FILE *fp;
  272.     int ch;
  273.  
  274.  
  275.     if ((fp = fopen(path,"rt")) == NULL)
  276.         return(fp = fopen(path,"wt"));
  277.  
  278.     if (isatty(fileno(fp))) {
  279.         fclose(fp);
  280.         return(fp = fopen(path,"wt"));
  281.     }
  282.     fclose(fp);
  283.  
  284.     gotoxy(9,1);
  285.     clreol();
  286.     set_color(co_hilite);
  287.     bputc('r');
  288.     set_color(co_info);
  289.     bputs("eplace or ");
  290.     set_color(co_hilite);
  291.     bputc('a');
  292.     set_color(co_info);
  293.         bputs("ppend? ");
  294.         video_update();
  295.  
  296.     ch = getkey() & 0x7f;
  297.     ch = tolower(ch);
  298.     if (ch == 0x1b)
  299.         return(NULL);
  300.  
  301.     while ((ch != 'a') && (ch != 'r')) {
  302.         ch = 0x7f & getkey();
  303.         ch = tolower(ch);
  304.         if (ch == 0x1b)
  305.             return(NULL);
  306.     }
  307.  
  308.     if (ch == 'a')
  309.         fp = fopen(path,"at");
  310.     else
  311.         fp = fopen(path,"wt");
  312.  
  313.     return(fp);
  314. }
  315.