home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume5 / rsed / mem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-03  |  5.7 KB  |  275 lines

  1.     
  2. /*
  3.  * This code copyright 1988 by Doug Davis (doug@letni.lawnet.com) 
  4.  *  You are free to modify, hack, fold, spindle, or mutlate this code in
  5.  *  any maner provided you give credit where credit is due and don't pretend
  6.  *  you wrote it.
  7.  *  If you do my lawyers (and I have a lot of lawyers) will teach you a lesson
  8.  *  in copyright law that you will never ever forget.
  9.  */
  10. #include "defs.h"
  11. #include "externs.h"
  12.  
  13. #define __FILE__    "mem.c"
  14.  
  15. clear_stuff()
  16. {
  17.     LINE *temp, *next;
  18.     if (l_start != (LINE *) NULL) {
  19.         /* something inside, clear 'em out */
  20.         for (temp = l_end; temp != l_start ;) {
  21.             next = temp->previous;
  22.             free(temp);
  23.             temp = next;
  24.         }
  25.     }
  26.     l_start = (LINE *) malloc(sizeof(LINE) + 2);
  27.     l_start -> previous     = (LINE *) NULL;
  28.     l_start -> next        = (LINE *) NULL;
  29.     l_end             = l_start;
  30.     current            = l_start;
  31.     CurrentLine        = 0L;
  32.     NumberLines        = 0L;
  33. }
  34. long
  35. loadem(inputfile, place)
  36. FILE *inputfile;
  37. long place;
  38. {
  39.     while ((fgets(buf, BUFSIZ, inputfile)) != NULL) {
  40.         if(add(buf, place++) != OK) {
  41.             fprintf(stderr, "%s:%d loadem() add() failure\n", 
  42.                 __FILE__, __LINE__);
  43.             return(!OK);
  44.         }
  45.         if (Dots == 'Y' || Debug) {
  46.             putchar('.');
  47.             fflush(stdout);
  48.         }
  49.     }
  50.     putchar('\n');
  51.     return(NumberLines);
  52. }
  53. LINE *
  54. setline(number)
  55. long number;
  56. {
  57.     LINE *tmp;
  58.     tmp=getline(number);
  59.     if (tmp != (LINE *) NULL)  {
  60.         CurrentLine = number;
  61.         current = tmp;
  62.     }
  63.  
  64.     return(tmp);
  65. }
  66. LINE *
  67. getline(number)
  68. long number;
  69. {
  70.     LINE *inc;
  71.     long i;
  72.     if (number > NumberLines) {
  73.         printf("%s-%s: getline(%ld) > NumberLines == %ld\n",
  74.             Progname, __FILE__, number, NumberLines);
  75.         return((LINE *)NULL);
  76.     }
  77.     if (number == 0L) 
  78.         return(l_start);
  79.     for (i=1L, inc=l_start; i<number ; i++, inc=inc->next);
  80.     return(inc);
  81. }
  82. printline(number)
  83. long number;
  84. {
  85.     LINE *tmp;
  86.     if ((tmp=setline(number)) != (LINE *) NULL) {
  87.         fputs(tmp->text, stdout);
  88.         fflush(stdout);
  89.         return(OK);
  90.     }
  91.     fprintf(stderr, "%s: printline(%ld) Bad line number\n", __FILE__, number);
  92.     fflush(stderr);
  93.     return(!OK);
  94. }
  95.  
  96.  
  97.  
  98. add(buf, number)
  99. char *buf;
  100. long number;
  101. {
  102.     LINE *new, *temp;
  103.     if (Debug) printf("add(%ld)\n", number);
  104.     new = (LINE *) malloc(sizeof(LINE) + strlen(buf));
  105.     if (number > 0L) {
  106.         temp=getline(number);
  107.         if (temp == (LINE *) NULL)  {
  108.             fprintf(stderr, "%s: add(%ld) bad line number\n",
  109.                 __FILE__, number);
  110.             return(!OK);
  111.         }
  112.         new -> previous = temp;
  113.         if (temp->next != (LINE *) NULL)  {
  114.             temp->next->previous = new;
  115.             new -> next = temp->next;
  116.         } else {
  117.             new->next = (LINE *) NULL;
  118.             l_end = new;
  119.         }
  120.         temp -> next = new;
  121.     } else {
  122.         new -> previous = (LINE *) NULL;
  123.         if (NumberLines < 1L) {
  124.             new -> next = (LINE *) NULL;
  125.             l_end = new;
  126.         } else 
  127.             new -> next = l_end;
  128.  
  129.         /* Now, blow away anything nasty, still hanging around */
  130.         if (l_start != (LINE *) NULL)
  131.             free(l_start); 
  132.  
  133.         l_start = new;
  134.         current = new;
  135.     }
  136.     /* make sure line has a newline on the end.. */
  137.     if (buf[strlen(buf)-1] != '\n')
  138.         strcat(buf, "\n");
  139.     strcpy(new->text, buf);
  140.     NumberLines++;
  141.     current=new;
  142.     CurrentLine=NumberLines;
  143.     return(OK);
  144. }
  145. subtract(number)
  146. long number;
  147. {
  148.     LINE *tmp;
  149.     tmp = getline(number);
  150.     if (tmp == (LINE *)NULL) {
  151.         fprintf(stderr, "%s: subtract(%ld) bad line number\n", 
  152.             __FILE__, number);
  153.         return(!OK);
  154.     }
  155.     if (l_end == tmp) {
  156.         l_end = l_end -> previous;
  157.     }
  158.     if (tmp -> previous != (LINE *) NULL) 
  159.         tmp -> previous -> next = tmp -> next; 
  160.      else {
  161.         if (tmp -> next != (LINE *) NULL) {
  162.             tmp -> next -> previous = (LINE *) NULL;
  163.             l_start = tmp -> next;
  164.         }
  165.     }
  166.  
  167.     if (tmp -> next != (LINE *) NULL)
  168.         tmp -> next -> previous  = tmp -> previous;
  169.      else {
  170.         if (tmp -> previous != (LINE *) NULL) {
  171.             tmp -> previous -> next = (LINE *) NULL;
  172.             l_end = tmp -> previous;
  173.         }
  174.     }
  175.     if (NumberLines-- > 1L)
  176.         free(tmp);
  177.     current=setline(number < NumberLines ? number : NumberLines);
  178.     return(current != (LINE *) NULL ? OK : !OK);
  179. }
  180.     
  181. replace(number, buf)
  182. char *buf;
  183. long number;
  184. {
  185.     LINE *old, *new;
  186.     old=getline(number);
  187.     if (old == (LINE *) NULL)  {
  188.         fprintf(stderr, "%s: replace(%ld) bad line number\n", __FILE__, number);
  189.         return(!OK);
  190.     }
  191.     new = (LINE *) malloc(sizeof(LINE) + strlen(buf));
  192.     new -> previous        = old -> previous;
  193.     new -> next        = old -> next;
  194.     if (new -> previous != (LINE *) NULL)  
  195.         new -> previous -> next = new;
  196.     else 
  197.         l_start = new;
  198.  
  199.     if (new -> next != (LINE *) NULL)  
  200.         new -> next -> previous = new;
  201.     else 
  202.         l_end = new;
  203.  
  204.     if (STRCHR(buf, '\n') == NULL)
  205.         strcat(buf, "\n");
  206.     strcpy(new->text, buf);
  207.     free(old);
  208.     current=setline(number);
  209.     return(current != (LINE *) NULL ? OK : !OK );
  210. }
  211. srch_n_replace(number, old_str, new_str, glob)
  212. long number;
  213. char *old_str, *new_str;
  214. char glob;
  215. {
  216.     LINE *work;
  217.     char *p, *b, *buff;
  218.     int len_old, cnt=0;
  219.     work = getline(number);
  220.     if (work == (LINE *) NULL) {
  221.         fprintf(stderr, "%s: srch_n_replace(%ld) bad line number\n",
  222.             __FILE__, number);
  223.         return(!OK);
  224.     }
  225.     len_old = strlen(old_str);
  226.     buff = malloc(MAXREPLACE);
  227.     if (buff == NULL)
  228.         return(!OK);
  229.     p=work->text;
  230.     b=buff;
  231.     do {
  232.         cnt++;
  233.         if (*p == *old_str)  {
  234.             if (!strncmp(p, old_str, len_old)) {
  235.                 p += (len_old-1); /* advance past string */
  236.                 b = strappend(b, new_str);
  237.                 if (glob == 'N')  {
  238.                     p++;
  239.                     strcat(b, p);
  240.                     break;
  241.                 }
  242.             } else
  243.             *(b++) = *p;
  244.         } else 
  245.             *(b++) = *p;
  246.     } while ((*(p++) != '\0') && cnt < MAXREPLACE);
  247.     replace(number, buff);
  248.     free(buff);
  249.     return(OK);
  250. }
  251. saveem(file)
  252. char *file;
  253. {
  254.     LINE *work;
  255.     int out;
  256.     long count=0L;
  257.     out = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  258.     if (out < 0) {
  259.         fprintf(stderr, "%s: Could not open \"%s\" for writing",
  260.             Progname, file);
  261.         perror("");
  262.         return(!OK);
  263.     }
  264.     work = l_start;
  265.     while (work != (LINE *) NULL && count < NumberLines)  {
  266.         count++;
  267.         write(out, work->text, strlen(work->text));
  268.         work = work-> next;
  269.         if (WriteOnQuit != 'Y') putchar('.');
  270.     }
  271.     close(out);
  272.     putchar('\n');
  273.     return(OK);
  274. }
  275.