home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1484 / modify.c
C/C++ Source or Header  |  1990-12-28  |  4KB  |  137 lines

  1.  
  2. /*
  3.  * Copyright (C) 1990 Jay Konigsberg - see Makefile for details
  4.  * This is Free Software, distrubited under the GNU Software Aggrement.
  5.  */
  6.  
  7. #include "simped.h"
  8.  
  9. char **modify(text, linenum)
  10. char **text;
  11. int  linenum;
  12. {
  13. extern    char    *realloc();
  14.  
  15. char    *getline();
  16.  
  17. int    printf(),
  18.     fprintf(),
  19.     fputs(),
  20.     puts(),
  21.     cleanup();
  22.  
  23. int    modifylen,    /* length of the modify directive string */
  24.     modifyinx,    /* pointer inside the directive string */
  25.     loopinx,    /* for inserts and deletes */
  26.     newlen,        /* length of the new text */
  27.     newinx,        /* index for newtext */
  28.     adjust=0,    /* ins & del change text[], so text ref must adjust
  29.                Note: adjust may be positive *or* negitive */
  30.     text_entered=0;
  31.  
  32. char    modifybuf[LINELEN+2],    /* the modify directives */
  33.     *newtext;        /* a pointer into modifybuf for inserts */
  34.  
  35. printf("%3d> ", linenum);
  36. fputs(text[linenum-1], stdout);
  37. printf("edit>");
  38. if (getline(modifybuf, &text_entered, stdin, '\0', TRUE))
  39.     {
  40.     puts("\nWarning: modify directive overflow, continuing");
  41.     }
  42.  
  43. /* remove the trailing '\n' */
  44. modifylen=strlen(modifybuf);
  45. modifybuf[--modifylen]='\0';
  46.  
  47. /* now parse modify's buffer for the changes to text[linenum-1] */
  48. for(modifyinx=0; modifyinx < modifylen; ++modifyinx)
  49.     {
  50.     if (modifybuf[modifyinx] == ' ')
  51.         {
  52.         /* do nothing */
  53.         }
  54. /*    else if ((modifybuf[modifyinx] >= 'a' && modifybuf[modifyinx] <= 'z' ||
  55.           modifybuf[modifyinx] >= 'A' && modifybuf[modifyinx] <= 'Z')&&
  56.           modifyinx+adjust != strlen(text[linenum-1])-1)
  57.     {
  58.     * replace that char in text[] *
  59.     text[linenum-1][modifyinx + adjust]=modifybuf[modifyinx];
  60.     }
  61. */
  62.     else if (modifybuf[modifyinx] == '&')
  63.     {
  64.     /* replace char with a sp */
  65.     text[linenum-1][modifyinx+adjust]=' ';
  66.     }
  67.     else if (modifybuf[modifyinx] == '#')
  68.     {
  69.     if (modifyinx+adjust != strlen(text[linenum-1])-1)
  70.         {
  71.         /* delete char - requires roll back. Note: adjust may be negitive */
  72.         for (loopinx=modifyinx+adjust; loopinx <= strlen(text[linenum-1]);
  73.         ++loopinx)
  74.         {
  75.         text[linenum-1][loopinx]=text[linenum-1][loopinx+1];
  76.         }
  77.         --adjust;
  78.         }
  79.     }
  80.     else if (modifybuf[modifyinx] == '^' ||
  81.     modifyinx+adjust == strlen(text[linenum-1])-1)
  82.     {
  83.     /* insert char(s) - requires a roll out and maybe a realloc */
  84.     if (modifyinx+adjust != strlen(text[linenum-1])-1)
  85.         newtext=(char *)&modifybuf[modifyinx+1];
  86.     else
  87.         {
  88.         newtext=(char *)&modifybuf[modifyinx];
  89.         }
  90.     if (newtext[0]=='#') /* insert a # */
  91.         {
  92.         newlen=1;
  93.         }
  94.     else
  95.         {
  96.         for (newlen=0; newtext[newlen] != '#'; ++newlen)
  97.         {
  98.         if (newtext[newlen] == '\0')
  99.             break;
  100.         }
  101.         }
  102.     /* create the space needed */
  103.     if((text[linenum-1]=realloc(text[linenum-1],
  104.         (unsigned int)(strlen(text[linenum-1])+newlen+adjust)))==NULL)
  105.         {
  106.         fprintf(stderr, "realloc: error=%d\n", errno);
  107.         cleanup(2);
  108.         }
  109.     /* do the roll out */
  110.     for (loopinx=strlen(text[linenum-1])+newlen;
  111.         loopinx >= modifyinx-1+newlen; --loopinx)
  112.         {
  113.         text[linenum-1][loopinx]=text[linenum-1][loopinx-newlen];
  114.         }
  115.     /* copy in the new text */
  116.     newinx=0;
  117.     for (loopinx=modifyinx+adjust;loopinx<modifyinx+adjust+newlen;++loopinx)
  118.         {
  119.         text[linenum-1][loopinx]=newtext[newinx++];
  120.         }
  121.     /* adjust the modifyinx pointer to skip insert command */
  122.     if (newtext[0] == '#')
  123.         ++modifyinx; /* special case, inserting a # */
  124.     else
  125.         modifyinx += newlen + 1; /* one more will be added on the for */
  126.     adjust += newlen;
  127.     }
  128.     else /* replace that character directly into the text */
  129.     {
  130.     text[linenum-1][modifyinx + adjust]=modifybuf[modifyinx];
  131.     }
  132.     }
  133. printf("\n%3d> ", linenum);
  134. fputs(text[linenum-1], stdout);
  135. return (text);
  136. }
  137.