home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / W / DEVBBS.ZIP / TEDIT.C < prev    next >
C/C++ Source or Header  |  1992-07-20  |  8KB  |  339 lines

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