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