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

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