home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / snip1292.zip / WORDWRAP.C < prev    next >
C/C++ Source or Header  |  1991-09-22  |  2KB  |  88 lines

  1. /*
  2. **  WORDWRAP.C - Simple CRT word wrap demonstration routine
  3. **
  4. **  public domain by Robert Morgan
  5. */ 
  6.  
  7. int get_ln(int rmargin); 
  8. void clr_eol(int curpos, int pos); 
  9.  
  10. void main() 
  11.       printf("Enter text.  Press CTRL-A to quit.\n"); 
  12.       while((get_ln(75)) != 0);     /* Change 75 to whatever number you
  13.                                        wish to be the right margin      */ 
  14.  
  15. void clr_eol(const int curpos, const int pos) 
  16.       int distance; 
  17.       int count; 
  18.  
  19.       distance = curpos - pos; 
  20.  
  21.       for (count = 1; count <= distance; count++) 
  22.             putch('\b'); 
  23.       for (count = 1; count <= distance; count++) 
  24.             putch(' '); 
  25.  
  26. int get_ln(int rmargin) 
  27.       char word[80]; 
  28.       static int wordpos = 0; 
  29.       static int curpos = 1; 
  30.       static int ch = 0; 
  31.       static int pos = 0; 
  32.  
  33.       word[wordpos] = '\0'; 
  34.  
  35.       while (ch != 1) 
  36.       { 
  37.             ch = getch(); 
  38.  
  39.             switch(ch) 
  40.             { 
  41.             case 1:
  42.                   return(0); 
  43.             case ' ':
  44.                   pos = curpos; 
  45.                   putch(' '); 
  46.                   curpos++; 
  47.                   wordpos = 0; 
  48.                   word[0] = '\0'; 
  49.                   break; 
  50.             case '\b':
  51.                   putch('\b'); 
  52.                   curpos--; 
  53.                   if (wordpos > 0) 
  54.                         wordpos--; 
  55.                   break; 
  56.             case '\r':
  57.                   puts("\r"); 
  58.                   wordpos = 0; 
  59.                   word[wordpos] = '\0'; 
  60.                   curpos = 1; 
  61.                   pos = 0; 
  62.                   break; 
  63.             default:
  64.                   putch(ch); 
  65.                   word[wordpos] = ch; 
  66.                   curpos++; 
  67.                   wordpos++; 
  68.                   break; 
  69.             } 
  70.  
  71.             if(curpos == rmargin) 
  72.             { 
  73.                   word[wordpos] = '\0'; 
  74.                   clr_eol(curpos,pos); 
  75.                   wordpos = 0; 
  76.                   curpos = strlen(word); 
  77.                   pos = 0; 
  78.                   puts("\r"); 
  79.                   printf("%s",word); 
  80.             } 
  81.       } 
  82.