Fossil SCM

Reorganize the new code, making internal functions private. Add COMMENT_PRINT_TRIM_SPACE flag, on by default.

mistachkin 2014-06-20 18:56 experimental
Commit 856d10ae81d8cbac38553168ae1b6081ee6d0802
1 file changed +108 -102
+108 -102
--- src/comformat.c
+++ src/comformat.c
@@ -27,12 +27,13 @@
2727
# include <termios.h>
2828
#endif
2929
3030
#if INTERFACE
3131
#define COMMENT_PRINT_NONE ((u32)0x00000000) /* No flags. */
32
-#define COMMENT_PRINT_WORD_BREAK ((u32)0x00000001) /* Break lines on words. */
33
-#define COMMENT_PRINT_DEFAULT COMMENT_PRINT_NONE /* Default flags. */
32
+#define COMMENT_PRINT_TRIM_SPACE ((u32)0x00000001) /* Trim leading/trailing. */
33
+#define COMMENT_PRINT_WORD_BREAK ((u32)0x00000002) /* Break lines on words. */
34
+#define COMMENT_PRINT_DEFAULT (COMMENT_PRINT_TRIM_SPACE) /* Defaults. */
3435
#endif
3536
3637
/*
3738
** This is the previous value used by most external callers when they
3839
** needed to specify a default maximum line length to be used with the
@@ -48,89 +49,67 @@
4849
#ifndef COMMENT_TAB_WIDTH
4950
# define COMMENT_TAB_WIDTH (8)
5051
#endif
5152
5253
/*
53
-** Given a comment string zText, format that string for printing
54
-** on a TTY. Assume that the output cursors is indent spaces from
55
-** the left margin and that a single line can contain no more than
56
-** width characters. Indent all subsequent lines by indent.
57
-**
58
-** Return the number of newlines that are output.
59
-*/
60
-int comment_print(
61
- const char *zText, /* The comment text to be printed. */
62
- int indent, /* Number of spaces to indent each non-initial line. */
63
- int width, /* Maximum number of characters per line. */
64
- int flags /* Zero or more "COMMENT_PRINT_*" flags, see above. */
65
-){
66
- int maxChars = width - indent;
67
- int lineCnt = 0;
68
- const char *zLine;
69
-
70
-#if defined(_WIN32)
71
- if( width<0 ){
72
- CONSOLE_SCREEN_BUFFER_INFO csbi;
73
- memset(&csbi, 0, sizeof(CONSOLE_SCREEN_BUFFER_INFO));
74
- if( GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) ){
75
- maxChars = csbi.srWindow.Right - csbi.srWindow.Left - indent;
76
- }
77
- }
78
-#elif defined(TIOCGWINSZ)
79
- if( width<0 ){
80
- struct winsize w;
81
- memset(&w, 0, sizeof(struct winsize));
82
- if( ioctl(0, TIOCGWINSZ, &w)!=-1 ){
83
- maxChars = w.ws_col - indent;
84
- }
85
- }
86
-#else
87
- if( width<0 ){
88
- /*
89
- ** Fallback to using more-or-less the "legacy semantics" of hard-coding
90
- ** the maximum line length to a value reasonable for the vast majority
91
- ** of supported systems.
92
- */
93
- maxChars = COMMENT_LEGACY_LINE_LENGTH - indent;
94
- }
95
-#endif
96
- if( zText==0 ) zText = "(NULL)";
97
- if( maxChars<=0 ){
98
- maxChars = strlen(zText);
99
- }
100
- while( fossil_isspace(zText[0]) ){ zText++; }
101
- if( zText[0]==0 ){
102
- fossil_print("\n");
103
- lineCnt++;
104
- return lineCnt;
105
- }
106
- zLine = zText;
107
- for(;;){
108
- comment_print_line(zLine, zLine>zText ? indent : 0, maxChars,
109
- flags & COMMENT_PRINT_WORD_BREAK, &lineCnt,
110
- &zLine);
111
- if( !zLine || !zLine[0] ) break;
112
- }
113
- return lineCnt;
54
+** This function scans the specified comment line starting just after the
55
+** initial index and returns the index of the next spacing character -OR-
56
+** zero if such a character cannot be found. For the purposes of this
57
+** algorithm, the NUL character is treated the same as a spacing character.
58
+*/
59
+static int comment_next_space(
60
+ const char *zLine, /* [in] The comment line being printed. */
61
+ int index /* [in] The current character index being handled. */
62
+){
63
+ int nextIndex = index + 1;
64
+ for(;;){
65
+ char c = zLine[nextIndex];
66
+ if( c==0 || fossil_isspace(c) ){
67
+ return nextIndex;
68
+ }
69
+ nextIndex++;
70
+ }
71
+ return 0; /* NOT REACHED */
72
+}
73
+
74
+/*
75
+** This function is called when printing a logical comment line to perform
76
+** the necessary indenting.
77
+*/
78
+static void comment_print_indent(
79
+ const char *zLine, /* [in] The comment line being printed. */
80
+ int indent, /* [in] Number of spaces to indent, zero for none. */
81
+ int trimSpace, /* [in] Non-zero to trim leading/trailing spaces. */
82
+ int *piIndex /* [in/out] Pointer to first non-space character. */
83
+){
84
+ if( indent>0 ){
85
+ fossil_print("%*s", indent, "");
86
+ if( trimSpace && zLine && piIndex ){
87
+ int index = *piIndex;
88
+ while( fossil_isspace(zLine[index]) ){ index++; }
89
+ *piIndex = index;
90
+ }
91
+ }
11492
}
11593
11694
/*
11795
** This function prints one logical line of a comment, stopping when it hits
11896
** a new line -OR- runs out of space on the logical line.
11997
*/
120
-void comment_print_line(
98
+static void comment_print_line(
12199
const char *zLine, /* [in] The comment line to print. */
122100
int indent, /* [in] Number of spaces to indent, zero for none. */
123101
int lineChars, /* [in] Maximum number of characters to print. */
102
+ int trimSpace, /* [in] Non-zero to trim leading/trailing spaces. */
124103
int wordBreak, /* [in] Non-zero to try breaking on word boundaries. */
125104
int *pLineCnt, /* [in/out] Pointer to the total line count. */
126105
const char **pzLine /* [out] Pointer to the end of the logical line. */
127106
){
128107
int index = 0, charCnt = 0, lineCnt = 0, maxChars;
129108
if( !zLine ) return;
130109
if( lineChars<=0 ) return;
131
- comment_print_indent(zLine, indent, &index);
110
+ comment_print_indent(zLine, indent, trimSpace, &index);
132111
maxChars = lineChars;
133112
for(;;){
134113
char c = zLine[index];
135114
if( c==0 ){
136115
break;
@@ -177,47 +156,74 @@
177156
*pzLine = zLine + index;
178157
}
179158
}
180159
181160
/*
182
-** This function is called when printing a logical comment line to perform
183
-** the necessary indenting.
184
-*/
185
-void comment_print_indent(
186
- const char *zLine, /* [in] The comment line being printed. */
187
- int indent, /* [in] Number of spaces to indent, zero for none. */
188
- int *piIndex /* [in/out] Pointer to first non-space character. */
189
-){
190
- if( indent>0 ){
191
- fossil_print("%*s", indent, "");
192
- if( zLine && piIndex ){
193
- int index = *piIndex;
194
- while( fossil_isspace(zLine[index]) ){ index++; }
195
- *piIndex = index;
196
- }
197
- }
198
-}
199
-
200
-/*
201
-** This function scans the specified comment line starting just after the
202
-** initial index and returns the index of the next spacing character -OR-
203
-** zero if such a character cannot be found. For the purposes of this
204
-** algorithm, the NUL character is treated the same as a spacing character.
205
-*/
206
-int comment_next_space(
207
- const char *zLine, /* [in] The comment line being printed. */
208
- int index /* [in] The current character index being handled. */
209
-){
210
- int nextIndex = index + 1;
211
- for(;;){
212
- char c = zLine[nextIndex];
213
- if( c==0 || fossil_isspace(c) ){
214
- return nextIndex;
215
- }
216
- nextIndex++;
217
- }
218
- return 0; /* NOT REACHED */
161
+** Given a comment string zText, format that string for printing
162
+** on a TTY. Assume that the output cursors is indent spaces from
163
+** the left margin and that a single line can contain no more than
164
+** width characters. Indent all subsequent lines by indent.
165
+**
166
+** Return the number of newlines that are output.
167
+*/
168
+int comment_print(
169
+ const char *zText, /* The comment text to be printed. */
170
+ int indent, /* Number of spaces to indent each non-initial line. */
171
+ int width, /* Maximum number of characters per line. */
172
+ int flags /* Zero or more "COMMENT_PRINT_*" flags, see above. */
173
+){
174
+ int maxChars = width - indent;
175
+ int trimSpace = flags & COMMENT_PRINT_TRIM_SPACE;
176
+ int wordBreak = flags & COMMENT_PRINT_WORD_BREAK;
177
+ int lineCnt = 0;
178
+ const char *zLine;
179
+
180
+#if defined(_WIN32)
181
+ if( width<0 ){
182
+ CONSOLE_SCREEN_BUFFER_INFO csbi;
183
+ memset(&csbi, 0, sizeof(CONSOLE_SCREEN_BUFFER_INFO));
184
+ if( GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) ){
185
+ maxChars = csbi.srWindow.Right - csbi.srWindow.Left - indent;
186
+ }
187
+ }
188
+#elif defined(TIOCGWINSZ)
189
+ if( width<0 ){
190
+ struct winsize w;
191
+ memset(&w, 0, sizeof(struct winsize));
192
+ if( ioctl(0, TIOCGWINSZ, &w)!=-1 ){
193
+ maxChars = w.ws_col - indent;
194
+ }
195
+ }
196
+#else
197
+ if( width<0 ){
198
+ /*
199
+ ** Fallback to using more-or-less the "legacy semantics" of hard-coding
200
+ ** the maximum line length to a value reasonable for the vast majority
201
+ ** of supported systems.
202
+ */
203
+ maxChars = COMMENT_LEGACY_LINE_LENGTH - indent;
204
+ }
205
+#endif
206
+ if( zText==0 ) zText = "(NULL)";
207
+ if( maxChars<=0 ){
208
+ maxChars = strlen(zText);
209
+ }
210
+ if( trimSpace ){
211
+ while( fossil_isspace(zText[0]) ){ zText++; }
212
+ }
213
+ if( zText[0]==0 ){
214
+ fossil_print("\n");
215
+ lineCnt++;
216
+ return lineCnt;
217
+ }
218
+ zLine = zText;
219
+ for(;;){
220
+ comment_print_line(zLine, zLine>zText ? indent : 0, maxChars,
221
+ trimSpace, wordBreak, &lineCnt, &zLine);
222
+ if( !zLine || !zLine[0] ) break;
223
+ }
224
+ return lineCnt;
219225
}
220226
221227
/*
222228
**
223229
** COMMAND: test-comment-format
224230
--- src/comformat.c
+++ src/comformat.c
@@ -27,12 +27,13 @@
27 # include <termios.h>
28 #endif
29
30 #if INTERFACE
31 #define COMMENT_PRINT_NONE ((u32)0x00000000) /* No flags. */
32 #define COMMENT_PRINT_WORD_BREAK ((u32)0x00000001) /* Break lines on words. */
33 #define COMMENT_PRINT_DEFAULT COMMENT_PRINT_NONE /* Default flags. */
 
34 #endif
35
36 /*
37 ** This is the previous value used by most external callers when they
38 ** needed to specify a default maximum line length to be used with the
@@ -48,89 +49,67 @@
48 #ifndef COMMENT_TAB_WIDTH
49 # define COMMENT_TAB_WIDTH (8)
50 #endif
51
52 /*
53 ** Given a comment string zText, format that string for printing
54 ** on a TTY. Assume that the output cursors is indent spaces from
55 ** the left margin and that a single line can contain no more than
56 ** width characters. Indent all subsequent lines by indent.
57 **
58 ** Return the number of newlines that are output.
59 */
60 int comment_print(
61 const char *zText, /* The comment text to be printed. */
62 int indent, /* Number of spaces to indent each non-initial line. */
63 int width, /* Maximum number of characters per line. */
64 int flags /* Zero or more "COMMENT_PRINT_*" flags, see above. */
65 ){
66 int maxChars = width - indent;
67 int lineCnt = 0;
68 const char *zLine;
69
70 #if defined(_WIN32)
71 if( width<0 ){
72 CONSOLE_SCREEN_BUFFER_INFO csbi;
73 memset(&csbi, 0, sizeof(CONSOLE_SCREEN_BUFFER_INFO));
74 if( GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) ){
75 maxChars = csbi.srWindow.Right - csbi.srWindow.Left - indent;
76 }
77 }
78 #elif defined(TIOCGWINSZ)
79 if( width<0 ){
80 struct winsize w;
81 memset(&w, 0, sizeof(struct winsize));
82 if( ioctl(0, TIOCGWINSZ, &w)!=-1 ){
83 maxChars = w.ws_col - indent;
84 }
85 }
86 #else
87 if( width<0 ){
88 /*
89 ** Fallback to using more-or-less the "legacy semantics" of hard-coding
90 ** the maximum line length to a value reasonable for the vast majority
91 ** of supported systems.
92 */
93 maxChars = COMMENT_LEGACY_LINE_LENGTH - indent;
94 }
95 #endif
96 if( zText==0 ) zText = "(NULL)";
97 if( maxChars<=0 ){
98 maxChars = strlen(zText);
99 }
100 while( fossil_isspace(zText[0]) ){ zText++; }
101 if( zText[0]==0 ){
102 fossil_print("\n");
103 lineCnt++;
104 return lineCnt;
105 }
106 zLine = zText;
107 for(;;){
108 comment_print_line(zLine, zLine>zText ? indent : 0, maxChars,
109 flags & COMMENT_PRINT_WORD_BREAK, &lineCnt,
110 &zLine);
111 if( !zLine || !zLine[0] ) break;
112 }
113 return lineCnt;
114 }
115
116 /*
117 ** This function prints one logical line of a comment, stopping when it hits
118 ** a new line -OR- runs out of space on the logical line.
119 */
120 void comment_print_line(
121 const char *zLine, /* [in] The comment line to print. */
122 int indent, /* [in] Number of spaces to indent, zero for none. */
123 int lineChars, /* [in] Maximum number of characters to print. */
 
124 int wordBreak, /* [in] Non-zero to try breaking on word boundaries. */
125 int *pLineCnt, /* [in/out] Pointer to the total line count. */
126 const char **pzLine /* [out] Pointer to the end of the logical line. */
127 ){
128 int index = 0, charCnt = 0, lineCnt = 0, maxChars;
129 if( !zLine ) return;
130 if( lineChars<=0 ) return;
131 comment_print_indent(zLine, indent, &index);
132 maxChars = lineChars;
133 for(;;){
134 char c = zLine[index];
135 if( c==0 ){
136 break;
@@ -177,47 +156,74 @@
177 *pzLine = zLine + index;
178 }
179 }
180
181 /*
182 ** This function is called when printing a logical comment line to perform
183 ** the necessary indenting.
184 */
185 void comment_print_indent(
186 const char *zLine, /* [in] The comment line being printed. */
187 int indent, /* [in] Number of spaces to indent, zero for none. */
188 int *piIndex /* [in/out] Pointer to first non-space character. */
189 ){
190 if( indent>0 ){
191 fossil_print("%*s", indent, "");
192 if( zLine && piIndex ){
193 int index = *piIndex;
194 while( fossil_isspace(zLine[index]) ){ index++; }
195 *piIndex = index;
196 }
197 }
198 }
199
200 /*
201 ** This function scans the specified comment line starting just after the
202 ** initial index and returns the index of the next spacing character -OR-
203 ** zero if such a character cannot be found. For the purposes of this
204 ** algorithm, the NUL character is treated the same as a spacing character.
205 */
206 int comment_next_space(
207 const char *zLine, /* [in] The comment line being printed. */
208 int index /* [in] The current character index being handled. */
209 ){
210 int nextIndex = index + 1;
211 for(;;){
212 char c = zLine[nextIndex];
213 if( c==0 || fossil_isspace(c) ){
214 return nextIndex;
215 }
216 nextIndex++;
217 }
218 return 0; /* NOT REACHED */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
219 }
220
221 /*
222 **
223 ** COMMAND: test-comment-format
224
--- src/comformat.c
+++ src/comformat.c
@@ -27,12 +27,13 @@
27 # include <termios.h>
28 #endif
29
30 #if INTERFACE
31 #define COMMENT_PRINT_NONE ((u32)0x00000000) /* No flags. */
32 #define COMMENT_PRINT_TRIM_SPACE ((u32)0x00000001) /* Trim leading/trailing. */
33 #define COMMENT_PRINT_WORD_BREAK ((u32)0x00000002) /* Break lines on words. */
34 #define COMMENT_PRINT_DEFAULT (COMMENT_PRINT_TRIM_SPACE) /* Defaults. */
35 #endif
36
37 /*
38 ** This is the previous value used by most external callers when they
39 ** needed to specify a default maximum line length to be used with the
@@ -48,89 +49,67 @@
49 #ifndef COMMENT_TAB_WIDTH
50 # define COMMENT_TAB_WIDTH (8)
51 #endif
52
53 /*
54 ** This function scans the specified comment line starting just after the
55 ** initial index and returns the index of the next spacing character -OR-
56 ** zero if such a character cannot be found. For the purposes of this
57 ** algorithm, the NUL character is treated the same as a spacing character.
58 */
59 static int comment_next_space(
60 const char *zLine, /* [in] The comment line being printed. */
61 int index /* [in] The current character index being handled. */
62 ){
63 int nextIndex = index + 1;
64 for(;;){
65 char c = zLine[nextIndex];
66 if( c==0 || fossil_isspace(c) ){
67 return nextIndex;
68 }
69 nextIndex++;
70 }
71 return 0; /* NOT REACHED */
72 }
73
74 /*
75 ** This function is called when printing a logical comment line to perform
76 ** the necessary indenting.
77 */
78 static void comment_print_indent(
79 const char *zLine, /* [in] The comment line being printed. */
80 int indent, /* [in] Number of spaces to indent, zero for none. */
81 int trimSpace, /* [in] Non-zero to trim leading/trailing spaces. */
82 int *piIndex /* [in/out] Pointer to first non-space character. */
83 ){
84 if( indent>0 ){
85 fossil_print("%*s", indent, "");
86 if( trimSpace && zLine && piIndex ){
87 int index = *piIndex;
88 while( fossil_isspace(zLine[index]) ){ index++; }
89 *piIndex = index;
90 }
91 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92 }
93
94 /*
95 ** This function prints one logical line of a comment, stopping when it hits
96 ** a new line -OR- runs out of space on the logical line.
97 */
98 static void comment_print_line(
99 const char *zLine, /* [in] The comment line to print. */
100 int indent, /* [in] Number of spaces to indent, zero for none. */
101 int lineChars, /* [in] Maximum number of characters to print. */
102 int trimSpace, /* [in] Non-zero to trim leading/trailing spaces. */
103 int wordBreak, /* [in] Non-zero to try breaking on word boundaries. */
104 int *pLineCnt, /* [in/out] Pointer to the total line count. */
105 const char **pzLine /* [out] Pointer to the end of the logical line. */
106 ){
107 int index = 0, charCnt = 0, lineCnt = 0, maxChars;
108 if( !zLine ) return;
109 if( lineChars<=0 ) return;
110 comment_print_indent(zLine, indent, trimSpace, &index);
111 maxChars = lineChars;
112 for(;;){
113 char c = zLine[index];
114 if( c==0 ){
115 break;
@@ -177,47 +156,74 @@
156 *pzLine = zLine + index;
157 }
158 }
159
160 /*
161 ** Given a comment string zText, format that string for printing
162 ** on a TTY. Assume that the output cursors is indent spaces from
163 ** the left margin and that a single line can contain no more than
164 ** width characters. Indent all subsequent lines by indent.
165 **
166 ** Return the number of newlines that are output.
167 */
168 int comment_print(
169 const char *zText, /* The comment text to be printed. */
170 int indent, /* Number of spaces to indent each non-initial line. */
171 int width, /* Maximum number of characters per line. */
172 int flags /* Zero or more "COMMENT_PRINT_*" flags, see above. */
173 ){
174 int maxChars = width - indent;
175 int trimSpace = flags & COMMENT_PRINT_TRIM_SPACE;
176 int wordBreak = flags & COMMENT_PRINT_WORD_BREAK;
177 int lineCnt = 0;
178 const char *zLine;
179
180 #if defined(_WIN32)
181 if( width<0 ){
182 CONSOLE_SCREEN_BUFFER_INFO csbi;
183 memset(&csbi, 0, sizeof(CONSOLE_SCREEN_BUFFER_INFO));
184 if( GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) ){
185 maxChars = csbi.srWindow.Right - csbi.srWindow.Left - indent;
186 }
187 }
188 #elif defined(TIOCGWINSZ)
189 if( width<0 ){
190 struct winsize w;
191 memset(&w, 0, sizeof(struct winsize));
192 if( ioctl(0, TIOCGWINSZ, &w)!=-1 ){
193 maxChars = w.ws_col - indent;
194 }
195 }
196 #else
197 if( width<0 ){
198 /*
199 ** Fallback to using more-or-less the "legacy semantics" of hard-coding
200 ** the maximum line length to a value reasonable for the vast majority
201 ** of supported systems.
202 */
203 maxChars = COMMENT_LEGACY_LINE_LENGTH - indent;
204 }
205 #endif
206 if( zText==0 ) zText = "(NULL)";
207 if( maxChars<=0 ){
208 maxChars = strlen(zText);
209 }
210 if( trimSpace ){
211 while( fossil_isspace(zText[0]) ){ zText++; }
212 }
213 if( zText[0]==0 ){
214 fossil_print("\n");
215 lineCnt++;
216 return lineCnt;
217 }
218 zLine = zText;
219 for(;;){
220 comment_print_line(zLine, zLine>zText ? indent : 0, maxChars,
221 trimSpace, wordBreak, &lineCnt, &zLine);
222 if( !zLine || !zLine[0] ) break;
223 }
224 return lineCnt;
225 }
226
227 /*
228 **
229 ** COMMAND: test-comment-format
230

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button