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

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