home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1456 / editline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  4.5 KB  |  194 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 **editline(text, linenum, delimiter)
  10. char **text;
  11. int  linenum;
  12. char delimiter;
  13. {
  14. int    position(),        /* where is oldtext in text[], -1 if err */
  15.     cleanup(),
  16.     puts(),
  17.     fputs(),
  18.     fflush(),
  19.     printf(),
  20.     fprintf();
  21.  
  22. char    *realloc(),
  23.     *getline();
  24.  
  25.  
  26. char    *oldtext,
  27.     *newtext,
  28.     buffer[LINELEN+2];
  29.  
  30. int    text_entered,        /* boolean to tell if something was entered */
  31.     textinx=0,        /* index for the subsitutation */
  32.     textlen,        /* length of the original string */
  33.     newlen,            /* length of new text */
  34.     oldlen,            /* length of old text */
  35.     point=0;        /* subscript of text[] where oldtext begins */
  36.  
  37. textlen=strlen(text[linenum-1]);
  38. if ( ! delimiter )
  39.     {
  40.     printf("%3d> ", linenum);
  41.     fputs(text[linenum-1], stdout);
  42.     puts("Return by itself to insert at the beginning.");
  43.     }
  44.     for(;;)
  45.     {
  46.     if ( ! delimiter)
  47.     {
  48.     fputs("Old text: ", stdout);
  49.     fflush(stdout);
  50.     }
  51.     if (getline(buffer, &text_entered, stdin, delimiter, TRUE))
  52.     {
  53.     if ( ! delimiter)
  54.         {
  55.         printf("Max line is %d characters, continuing...\n", LINELEN);
  56.         }
  57.     else
  58.         {
  59.         /* printf("%c%c", delimiter, BELL); */
  60.         }
  61.     buffer[LINELEN+1]='\n';
  62.     buffer[LINELEN+2]='\0';
  63.     }
  64.     fflush(stdout);
  65.     /* if (text_entered == FALSE) */
  66.     if ( strlen(buffer) == 1) /* a single '\n' was entered */
  67.     {
  68.     /* setup to insert at the begining of the line */
  69.     oldlen=0;
  70.     break;
  71.     }
  72.     else
  73.     {
  74.     buffer[strlen(buffer)-1]='\0';
  75.     oldtext=strdup(buffer);
  76.     oldlen=strlen(oldtext);
  77.     if( (point=position(oldtext, text[linenum-1])) == -1)
  78.         {
  79.         printf("\nstring: %s, not found in line %d\n", oldtext, linenum);
  80.         delimiter='\0';
  81.         }
  82.     else
  83.         {
  84.         break;
  85.         }
  86.     }
  87.     }
  88. if ( ! delimiter )
  89.     {
  90.     fputs("New text: ", stdout);
  91.     fflush(stdout);
  92.     }
  93. if(getline(buffer, &text_entered, stdin, delimiter, TRUE))
  94.     {
  95.     if ( ! delimiter )
  96.         {
  97.         printf("Max line is %d characters, continuing...\n", LINELEN);
  98.         }
  99.     else
  100.         {
  101.         /* printf("%c%c", delimiter, BELL); */
  102.         fflush(stdout);
  103.         }
  104.     buffer[LINELEN+1]='\n';
  105.     buffer[LINELEN+2]='\0';
  106.     }
  107. /* if (text_entered == FALSE && oldlen == 0) */
  108. if (strlen(buffer) == 1 && oldlen == 0) /* a singal '\n' was entered */
  109.     {
  110.     /* nothing entered, exit */
  111.     puts("\nNo change...");
  112.     return(text);
  113.     }
  114. if (text_entered == FALSE)
  115.     {
  116.     buffer[0]='\0';
  117.     }
  118. else
  119.     {
  120.     buffer[strlen(buffer)-1]='\0';
  121.     }
  122. newtext=strdup(buffer);
  123. newlen=strlen(newtext);
  124. /* Conditions to handle
  125.  *
  126.  *                * no realloc
  127.  * 1) oldlen == newlen        - strings are same length
  128.  *                * relloc after change so no text is lost
  129.  * 2) newlen == 0 & oldlen > 1    - remove some text
  130.  * 3) newlen <  oldlen        - downsize text[]
  131.  *                * realloc before change so there is
  132.  *                  room for more characters
  133.  * 4) oldlen == 0 & newlen > 1    - insert at beginning of line
  134.  * 5) newlen >  oldlen        - increase size-of text[]
  135.  */
  136. if (newlen == oldlen) /* case 1, same length */
  137.     {
  138.     for (textinx=point; textinx < point+newlen; ++textinx)
  139.     {
  140.     text[linenum-1][textinx] = newtext[textinx-point];
  141.     }
  142.     }
  143. else
  144.     {
  145.     if (newlen < oldlen) /* case 2 & 3 - have extra space */
  146.     {
  147.     /* copy over old text - non-op if new=0 */
  148.     for (textinx=point; textinx < point+newlen; ++textinx)
  149.         {
  150.         text[linenum-1][textinx] = newtext[textinx-point];
  151.         }
  152.     /* roll remaining text back over the oldtext */
  153.     point=textinx;
  154.     for (textinx=point; textinx < textlen; ++textinx)
  155.         {
  156.         text[linenum-1][textinx]=text[linenum-1][textinx+oldlen-newlen];
  157.         }
  158.     text[linenum-1][textlen+newlen-oldlen]='\0';
  159.     if((text[linenum-1]=realloc(text[linenum-1],
  160.         (unsigned int)(textinx+FUDGE)))==NULL)
  161.         {
  162.         fprintf(stderr, "realloc: error=%d\n", errno);
  163.         cleanup(2);
  164.         }
  165.     }
  166.     else /* case 4 & 5 - more space is needed */
  167.     {
  168.     if((text[linenum-1]=realloc(text[linenum-1],
  169.         (unsigned int)(textlen+newlen-oldlen+FUDGE)))==NULL)
  170.         {
  171.         fprintf(stderr, "realloc: error=%d\n", errno);
  172.         cleanup(2);
  173.         }
  174.     /* roll out char to make room for insert */
  175.     
  176.     for (textinx=textlen+newlen-oldlen;
  177.         textinx >= point+newlen-oldlen; --textinx)
  178.         {
  179.         text[linenum-1][textinx]=text[linenum-1][textinx-newlen+oldlen];
  180.         }
  181.     /* place newtext in the text area */
  182.     for (textinx=point; textinx < point+newlen; ++textinx)
  183.         {
  184.         text[linenum-1][textinx] = newtext[textinx-point];
  185.         }
  186.     text[linenum-1][textlen+newlen-oldlen-1]='\n';
  187.     text[linenum-1][textlen+newlen-oldlen]='\0';
  188.     }
  189.     }
  190. printf("\n%3d> ", linenum);
  191. fputs(text[linenum-1], stdout);
  192. return(text);
  193. }
  194.