File: wordwrap.c

package info (click to toggle)
c-cpp-reference 2.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 8,012 kB
  • ctags: 4,612
  • sloc: ansic: 26,960; sh: 11,014; perl: 1,854; cpp: 1,324; asm: 1,239; python: 258; makefile: 115; java: 77; awk: 34; csh: 9
file content (92 lines) | stat: -rwxr-xr-x 2,334 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
**  WORDWRAP.C - Simple CRT word wrap demonstration routine
**
**  public domain by Robert Morgan
*/ 

#include <stdio.h>
#include <conio.h>
#include <string.h>

int get_ln(int rmargin); 
void clr_eol(const int curpos, const int pos); 
 
void main() 
{ 
      printf("Enter text.  Press CTRL-A to quit.\n"); 
      while((get_ln(75)) != 0)      /* Change 75 to whatever number you */
            ;                       /* wish to be the right margin      */ 
} 
 
void clr_eol(const int curpos, const int pos) 
{ 
      int distance; 
      int count; 
 
      distance = curpos - pos; 
 
      for (count = 1; count <= distance; count++) 
            putch('\b'); 
      for (count = 1; count <= distance; count++) 
            putch(' '); 
} 
 
int get_ln(int rmargin) 
{ 
      char word[80]; 
      static int wordpos = 0; 
      static int curpos = 1; 
      static int ch = 0; 
      static int pos = 0; 

      word[wordpos] = '\0'; 
 
      while (ch != 1) 
      { 
            ch = getch(); 
 
            switch(ch) 
            { 
            case 1:
                  return(0); 
            case ' ':
                  pos = curpos; 
                  putch(' '); 
                  curpos++; 
                  wordpos = 0; 
                  word[0] = '\0'; 
                  break; 
            case '\b':
                  putch('\b'); 
                  curpos--; 
                  if (wordpos > 0) 
                        wordpos--; 
                  break; 
            case '\r':
                  puts("\r"); 
                  wordpos = 0; 
                  word[wordpos] = '\0'; 
                  curpos = 1; 
                  pos = 0; 
                  break; 
            default:
                  putch(ch); 
                  word[wordpos] = (char)ch; 
                  curpos++; 
                  wordpos++; 
                  break; 
            } 

            if(curpos == rmargin) 
            { 
                  word[wordpos] = '\0'; 
                  clr_eol(curpos,pos); 
                  wordpos = 0; 
                  curpos = strlen(word); 
                  pos = 0; 
                  puts("\r"); 
                  printf("%s",word); 
            } 
      } 
      return -1;
}