home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNIP9404.ZIP / WORDWRAP.C < prev    next >
C/C++ Source or Header  |  1994-04-03  |  2KB  |  93 lines

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