Fossil SCM

Enhance the "translate" utility so that formatting characters can occur in between the "%" and "C" of a printf-style conversion on @-lines.

drh 2012-04-28 03:32 trunk
Commit f9711803e22ee8887484b981dc227b19d69d58c5
1 file changed +17 -12
+17 -12
--- src/translate.c
+++ src/translate.c
@@ -12,10 +12,12 @@
1212
** Author contact information:
1313
** [email protected]
1414
** http://www.hwaci.com/drh/
1515
**
1616
*******************************************************************************
17
+**
18
+** SYNOPSIS:
1719
**
1820
** Input lines that begin with the "@" character are translated into
1921
** either cgi_printf() statements or string literals and the
2022
** translated code is written on standard output.
2123
**
@@ -28,10 +30,11 @@
2830
**
2931
** This tool allows us to put raw HTML, without the special codes, in
3032
** the middle of a C program. This program then translates the text
3133
** into standard C by inserting all necessary backslashes and other
3234
** punctuation.
35
+**
3336
*/
3437
#include <stdio.h>
3538
#include <ctype.h>
3639
#include <stdlib.h>
3740
#include <string.h>
@@ -119,36 +122,38 @@
119122
fprintf(out,"%*s\"%s\\n\"\n",indent, "", zOut);
120123
}
121124
}else{
122125
/* Otherwise (if the last non-whitespace was not '=') then generate
123126
** a cgi_printf() statement whose format is the text following the '@'.
124
- ** Substrings of the form "%C(...)" where C is any character will
125
- ** puts "%C" in the format and add the "..." as an argument to the
126
- ** cgi_printf call.
127
+ ** Substrings of the form "%C(...)" (where C is any sequence of
128
+ ** characters other than \000 and '(') will put "%C" in the
129
+ ** format and add the "(...)" as an argument to the cgi_printf call.
127130
*/
128131
int indent;
132
+ int nC;
133
+ char c;
129134
i++;
130135
if( isspace(zLine[i]) ){ i++; }
131136
indent = i;
132137
for(j=0; zLine[i] && zLine[i]!='\r' && zLine[i]!='\n'; i++){
133138
if( zLine[i]=='"' || zLine[i]=='\\' ){ zOut[j++] = '\\'; }
134139
zOut[j++] = zLine[i];
135140
if( zLine[i]!='%' || zLine[i+1]=='%' || zLine[i+1]==0 ) continue;
136
- if( zLine[i+2]!='(' ) continue;
137
- i++;
138
- zOut[j++] = zLine[i];
141
+ for(nC=1; zLine[i+nC] && zLine[i+nC]!='('; nC++){}
142
+ if( zLine[i+nC]!='(' || !isalpha(zLine[i+nC-1]) ) continue;
143
+ while( --nC ) zOut[j++] = zLine[++i];
139144
zArg[nArg++] = ',';
140
- i += 2;
141
- k = 1;
142
- while( zLine[i] ){
143
- if( zLine[i]==')' ){
145
+ k = 0; i++;
146
+ while( (c = zLine[i])!=0 ){
147
+ zArg[nArg++] = c;
148
+ if( c==')' ){
144149
k--;
145150
if( k==0 ) break;
146
- }else if( zLine[i]=='(' ){
151
+ }else if( c=='(' ){
147152
k++;
148153
}
149
- zArg[nArg++] = zLine[i++];
154
+ i++;
150155
}
151156
}
152157
zOut[j] = 0;
153158
if( !inPrint ){
154159
fprintf(out,"%*scgi_printf(\"%s\\n\"",indent-2,"", zOut);
155160
--- src/translate.c
+++ src/translate.c
@@ -12,10 +12,12 @@
12 ** Author contact information:
13 ** [email protected]
14 ** http://www.hwaci.com/drh/
15 **
16 *******************************************************************************
 
 
17 **
18 ** Input lines that begin with the "@" character are translated into
19 ** either cgi_printf() statements or string literals and the
20 ** translated code is written on standard output.
21 **
@@ -28,10 +30,11 @@
28 **
29 ** This tool allows us to put raw HTML, without the special codes, in
30 ** the middle of a C program. This program then translates the text
31 ** into standard C by inserting all necessary backslashes and other
32 ** punctuation.
 
33 */
34 #include <stdio.h>
35 #include <ctype.h>
36 #include <stdlib.h>
37 #include <string.h>
@@ -119,36 +122,38 @@
119 fprintf(out,"%*s\"%s\\n\"\n",indent, "", zOut);
120 }
121 }else{
122 /* Otherwise (if the last non-whitespace was not '=') then generate
123 ** a cgi_printf() statement whose format is the text following the '@'.
124 ** Substrings of the form "%C(...)" where C is any character will
125 ** puts "%C" in the format and add the "..." as an argument to the
126 ** cgi_printf call.
127 */
128 int indent;
 
 
129 i++;
130 if( isspace(zLine[i]) ){ i++; }
131 indent = i;
132 for(j=0; zLine[i] && zLine[i]!='\r' && zLine[i]!='\n'; i++){
133 if( zLine[i]=='"' || zLine[i]=='\\' ){ zOut[j++] = '\\'; }
134 zOut[j++] = zLine[i];
135 if( zLine[i]!='%' || zLine[i+1]=='%' || zLine[i+1]==0 ) continue;
136 if( zLine[i+2]!='(' ) continue;
137 i++;
138 zOut[j++] = zLine[i];
139 zArg[nArg++] = ',';
140 i += 2;
141 k = 1;
142 while( zLine[i] ){
143 if( zLine[i]==')' ){
144 k--;
145 if( k==0 ) break;
146 }else if( zLine[i]=='(' ){
147 k++;
148 }
149 zArg[nArg++] = zLine[i++];
150 }
151 }
152 zOut[j] = 0;
153 if( !inPrint ){
154 fprintf(out,"%*scgi_printf(\"%s\\n\"",indent-2,"", zOut);
155
--- src/translate.c
+++ src/translate.c
@@ -12,10 +12,12 @@
12 ** Author contact information:
13 ** [email protected]
14 ** http://www.hwaci.com/drh/
15 **
16 *******************************************************************************
17 **
18 ** SYNOPSIS:
19 **
20 ** Input lines that begin with the "@" character are translated into
21 ** either cgi_printf() statements or string literals and the
22 ** translated code is written on standard output.
23 **
@@ -28,10 +30,11 @@
30 **
31 ** This tool allows us to put raw HTML, without the special codes, in
32 ** the middle of a C program. This program then translates the text
33 ** into standard C by inserting all necessary backslashes and other
34 ** punctuation.
35 **
36 */
37 #include <stdio.h>
38 #include <ctype.h>
39 #include <stdlib.h>
40 #include <string.h>
@@ -119,36 +122,38 @@
122 fprintf(out,"%*s\"%s\\n\"\n",indent, "", zOut);
123 }
124 }else{
125 /* Otherwise (if the last non-whitespace was not '=') then generate
126 ** a cgi_printf() statement whose format is the text following the '@'.
127 ** Substrings of the form "%C(...)" (where C is any sequence of
128 ** characters other than \000 and '(') will put "%C" in the
129 ** format and add the "(...)" as an argument to the cgi_printf call.
130 */
131 int indent;
132 int nC;
133 char c;
134 i++;
135 if( isspace(zLine[i]) ){ i++; }
136 indent = i;
137 for(j=0; zLine[i] && zLine[i]!='\r' && zLine[i]!='\n'; i++){
138 if( zLine[i]=='"' || zLine[i]=='\\' ){ zOut[j++] = '\\'; }
139 zOut[j++] = zLine[i];
140 if( zLine[i]!='%' || zLine[i+1]=='%' || zLine[i+1]==0 ) continue;
141 for(nC=1; zLine[i+nC] && zLine[i+nC]!='('; nC++){}
142 if( zLine[i+nC]!='(' || !isalpha(zLine[i+nC-1]) ) continue;
143 while( --nC ) zOut[j++] = zLine[++i];
144 zArg[nArg++] = ',';
145 k = 0; i++;
146 while( (c = zLine[i])!=0 ){
147 zArg[nArg++] = c;
148 if( c==')' ){
149 k--;
150 if( k==0 ) break;
151 }else if( c=='(' ){
152 k++;
153 }
154 i++;
155 }
156 }
157 zOut[j] = 0;
158 if( !inPrint ){
159 fprintf(out,"%*scgi_printf(\"%s\\n\"",indent-2,"", zOut);
160

Keyboard Shortcuts

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