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

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