home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / WORDWRAP.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  96 lines

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