home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / UTIL / WWIVE / MYWIVE.ZIP / TEDIT.C < prev    next >
C/C++ Source or Header  |  1993-05-06  |  7KB  |  333 lines

  1. #include "vars.h"
  2. #pragma hdrstop
  3. #include <dir.h>
  4. #define utoa(s,v,r) ultoa((unsigned long)(s),v,r)
  5.  
  6. struct line *read_file(char *fn, int *numlines)
  7. {
  8.   char b[1024],s[160],s1[160];
  9.   int f,i,n,nb,cb,sp,oor;
  10.   struct line *l,*l1,*topline;
  11.  
  12.   *numlines=-1;
  13.   oor=0;
  14.   topline=NULL;
  15.   sprintf(s,"%s%s",syscfg.gfilesdir,fn);
  16.   f=open(s,O_RDWR | O_BINARY);
  17.   if (f<0) {
  18.     nl();
  19.     pl("File not found.");
  20.     nl();
  21.     return(NULL);
  22.   }
  23.   nb=read(f,(void *)b,1024);
  24.   cb=0;
  25.   sp=0;
  26.   *numlines=0;
  27.   while ((nb) && (!oor)) {
  28.     if (b[cb]==13) {
  29.       s[sp]=0;
  30.       sp=0;
  31.       if (farcoreleft()<10240) {
  32.         nl();
  33.         pl("Ran out of memory.  Not all of file read.");
  34.         nl();
  35.         oor=1;
  36.       } else {
  37.         l1=(struct line *)farmalloc((long) sizeof(struct line));
  38.         strcpy((l1->text),s);
  39.         l1->next=NULL;
  40.         l1->prev=NULL;
  41.         if (topline==NULL) {
  42.           topline=l1;
  43.           l=l1;
  44.         } else {
  45.           l->next=l1;
  46.           l1->prev=l;
  47.           l=l1;
  48.         }
  49.         ++(*numlines);
  50.       }
  51.     } else {
  52.       if (b[cb]!=10)
  53.         s[sp++]=b[cb];
  54.     }
  55.     ++cb;
  56.     if (cb==nb) {
  57.       nb=read(f,(void *)b,1024);
  58.       cb=0;
  59.     }
  60.   }
  61.   return(topline);
  62. }
  63.  
  64. void kill_file(struct line *topline)
  65. {
  66.   struct line *l,*l1;
  67.  
  68.   l=topline;
  69.   while (l!=NULL) {
  70.     l1=l->next;
  71.     farfree((void *)l);
  72.     l=l1;
  73.   }
  74. }
  75.  
  76. void save_file(char *fn, struct line *topline)
  77. {
  78.   int i,f;
  79.   char s[170];
  80.   struct line *l,*l1;
  81.  
  82.   nl();
  83.   pl("Saving...");
  84.   sprintf(s,"%s%s",syscfg.gfilesdir,fn);
  85.   f=open(s,O_RDWR | O_BINARY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  86.   l=topline;
  87.   while (l!=NULL) {
  88.     strcpy(s,(l->text));
  89.     strcat(s,"\r\n");
  90.     write(f,(void *)s,strlen(s));
  91.     l=l->next;
  92.   }
  93.   s[0]=26;
  94.   write(f,(void *)s,1);
  95.   close(f);
  96. }
  97.  
  98. int printl(int n, struct line *l)
  99. {
  100.   int abort;
  101.   char s[160];
  102.  
  103.   abort=0;
  104.   if (n<1)
  105.     strcpy(s,"     [TOP]");
  106.   else
  107.     if (l==NULL)
  108.       strcpy(s,"     [END]");
  109.     else
  110.       sprintf(s,"%3d: %s",n,(l->text));
  111.   pla(s,&abort);
  112.   if (abort)
  113.     nl();
  114.   return(abort);
  115. }
  116.  
  117. void tedit(char *fn)
  118. {
  119.   struct line *topline,*l,*l1,*c,*bottomline;
  120.   int numlines,curline,i,i1,i2,done,save,abort;
  121.   char s[160],s1[160],rollover[160];
  122.  
  123.   rollover[0]=0;
  124.   thisuser.screenchars -= 5;
  125.   topline=NULL;
  126.   numlines=0;
  127.   curline=0;
  128.   topline=read_file(fn,&numlines);
  129.   npr("%d lines read in.\r\n",numlines);
  130.   nl();
  131.   done=0;
  132.   save=0;
  133.   if (numlines>0) {
  134.     curline=1;
  135.     bottomline=topline;
  136.     while ((bottomline->next)!=NULL)
  137.       bottomline=bottomline->next;
  138.   } else
  139.     curline=0;
  140.   if (numlines<1)
  141.     numlines=0;
  142.   c=topline;
  143.   printl(curline,c);
  144.   do {
  145.     outstr(":");
  146.     input(s,10);
  147.     i=atoi(s);
  148.     if ((s[0]>='0') && (s[0]<='9')) {
  149.       i-=curline;
  150.       if (i==0)
  151.         strcpy(s,"~");
  152.       else
  153.         if (i>0) {
  154.           s[0]='+';
  155.           itoa(i,&(s[1]),10);
  156.         } else {
  157.           itoa(i,s,10);
  158.         }
  159.     }
  160.     if (s[0]==0)
  161.       strcpy(s,"+");
  162.     switch(s[0]) {
  163.       case '?':
  164.         printmenu(11);
  165.         break;
  166.       case 'Q':
  167.         done=1;
  168.         save=0;
  169.         break;
  170.       case 'S':
  171.         done=1;
  172.         save=1;
  173.         break;
  174.       case 'L':
  175.         i=curline;
  176.         l=c;
  177.         while ((l!=NULL) && (printl(i,l)==0)) {
  178.           l=l->next;
  179.           ++i;
  180.         }
  181.         break;
  182.       case 'T':
  183.         c=topline;
  184.         if (topline==NULL)
  185.           curline=0;
  186.         else
  187.           curline=1;
  188.         printl(curline,c);
  189.         break;
  190.       case 'B':
  191.         c=NULL;
  192.         curline=numlines+1;
  193.         printl(curline,c);
  194.         break;
  195.       case '-':
  196.         if (curline) {
  197.           i=atoi(&(s[1]));
  198.           if (i==0)
  199.             i=1;
  200.           for (i1=0; (i1<i) && (curline); i1++) {
  201.             if (c==NULL)
  202.               c=bottomline;
  203.             else
  204.               c=c->prev;
  205.             --curline;
  206.           }
  207.         }
  208.         printl(curline,c);
  209.         break;
  210.       case '+':
  211.         if (curline!=numlines+1) {
  212.           i=atoi(&(s[1]));
  213.           if (i==0)
  214.             i=1;
  215.           for (i1=0; (i1<i) && (curline!=numlines+1); i1++) {
  216.             if (c==NULL)
  217.               c=topline;
  218.             else
  219.               c=c->next;
  220.             ++curline;
  221.           }
  222.         }
  223.         printl(curline,c);
  224.         break;
  225.       case 'D':
  226.         i=atoi(&(s[1]));
  227.         if (i==0)
  228.           i=1;
  229.         for (i1=0; (i1<i) && (c!=NULL); i1++) {
  230.           if (c->prev==NULL)
  231.             if (c->next==NULL) {
  232.               topline=NULL;
  233.               bottomline=NULL;
  234.             } else
  235.               topline=c->next;
  236.           else
  237.             if (c->next==NULL)
  238.               bottomline=c->prev;
  239.           l=c;
  240.           if (c->prev!=NULL)
  241.             c->prev->next=c->next;
  242.           if (c->next!=NULL)
  243.             c->next->prev=c->prev;
  244.           c=c->next;
  245.           farfree((void *)l);
  246.           --numlines;
  247.         }
  248.         printl(curline,c);
  249.         break;
  250.       case 'P':
  251.         printl(curline,c);
  252.         break;
  253.       case 'C':
  254.         nl();
  255.         prt(5,"Clear workspace? ");
  256.         if (yn()) {
  257.           kill_file(topline);
  258.           topline=NULL;
  259.           bottomline=NULL;
  260.           c=NULL;
  261.           numlines=0;
  262.           curline=0;
  263.           pl("File cleared.");
  264.         }
  265.         break;
  266.       case 'I':
  267.         nl();
  268.         pl("Enter '.' on its own line to exit insert mode.");
  269.         i1=0;
  270.         do {
  271.           if (farcoreleft()<10240) {
  272.             nl();
  273.             pl("Not enough memory left to insert anything.");
  274.             nl();
  275.             i1=1;
  276.           } else {
  277.             sprintf(s1,"%3d: ",curline);
  278.             if (curline<1)
  279.               strcpy(s1,"  1: ");
  280.             outstr(s1);
  281.             inli(s1,rollover,150,1);
  282.             if (strcmp(s1,".")==0)
  283.               i1=1;
  284.             else {
  285.               l=(struct line *)farmalloc(sizeof(struct line));
  286.               strcpy((l->text),s1);
  287.               if (c!=NULL) {
  288.                 l->next=c;
  289.                 if (c->prev!=NULL) {
  290.                   c->prev->next=l;
  291.                   l->prev=c->prev;
  292.                   c->prev=l;
  293.                 } else {
  294.                   c->prev=l;
  295.                   l->prev=NULL;
  296.                   topline=l;
  297.                 }
  298.               } else {
  299.                 if (topline==NULL) {
  300.                   l->prev=NULL;
  301.                   l->next=NULL;
  302.                   topline=l;
  303.                   bottomline=l;
  304.                   curline=1;
  305.                 } else
  306.                   if (curline==numlines+1) {
  307.                     l->prev=bottomline;
  308.                     bottomline->next=l;
  309.                     l->next=NULL;
  310.                     bottomline=l;
  311.                   } else {
  312.                     l->prev=NULL;
  313.                     l->next=topline;
  314.                     topline->prev=l;
  315.                     c=topline;
  316.                     topline=l;
  317.                     curline=1;
  318.                   }
  319.               }
  320.               ++numlines;
  321.               ++curline;
  322.             }
  323.           }
  324.         } while ((!i1) && (!hangup));
  325.         break;
  326.     }
  327.   } while ((!done) && (!hangup));
  328.   thisuser.screenchars += 5;
  329.   if (save)
  330.     save_file(fn,topline);
  331.   kill_file(topline);
  332. }
  333.