@@ -31,6 +31,13 @@ static const std::string cxx_val_end = "]]";
3131static const std::string sub_view_start = " <%view" ;
3232static const std::string sub_view_end = " %>" ;
3333
34+ enum class CspLeftTagType
35+ {
36+ kCxxStart ,
37+ kCxxValStart ,
38+ kSubViewStart ,
39+ };
40+
3441using namespace drogon_ctl ;
3542
3643static std::string &replace_all (std::string &str,
@@ -54,6 +61,17 @@ static std::string &replace_all(std::string &str,
5461 return str;
5562}
5663
64+ static std::string parseParameter (const std::string &str)
65+ {
66+ auto iter = str.begin ();
67+ while (iter != str.end () && *iter == ' ' )
68+ ++iter;
69+ auto keyword_start = iter;
70+ while (iter != str.end () && *iter != ' ' )
71+ ++iter;
72+ return std::string (keyword_start, iter);
73+ }
74+
5775static void parseCxxLine (std::ofstream &oSrcFile,
5876 const std::string &line,
5977 const std::string &streamName,
@@ -103,143 +121,150 @@ static void outputSubView(std::ofstream &oSrcFile,
103121 oSrcFile << " }\n " ;
104122}
105123
124+ static void outputText (std::ofstream &oSrcFile,
125+ std::string line,
126+ const std::string &streamName,
127+ bool do_return)
128+ {
129+ if (line.length () == 0 )
130+ {
131+ if (do_return)
132+ {
133+ oSrcFile << " \t " << streamName << " << \"\\ n\"\n ;" ;
134+ }
135+ return ;
136+ }
137+
138+ replace_all (line, " \\ " , " \\\\ " );
139+ replace_all (line, " \" " , " \\\" " );
140+ oSrcFile << " \t " << streamName << " << \" " << line;
141+
142+ if (do_return)
143+ oSrcFile << " \\ n\" ;\n ;" ;
144+ else
145+ oSrcFile << " \" ;\n ;" ;
146+ }
147+
148+ // if not found, second will be std::string::npos.
149+ static std::pair<CspLeftTagType, std::string::size_type> findFirstLeftTag (
150+ const std::string &text)
151+ {
152+ std::pair<CspLeftTagType, std::string::size_type> find_result[] = {
153+ {CspLeftTagType{}, std::string::npos},
154+ {CspLeftTagType::kCxxStart , text.find (cxx_lang)},
155+ {CspLeftTagType::kCxxValStart , text.find (cxx_val_start)},
156+ {CspLeftTagType::kSubViewStart , text.find (sub_view_start)}};
157+
158+ auto &result = find_result[0 ];
159+ for (const auto &i : find_result)
160+ {
161+ if (i.second == std::string::npos)
162+ continue ;
163+
164+ if (i.second <= result.second )
165+ result = i;
166+ }
167+ return result;
168+ }
169+
106170static void parseLine (std::ofstream &oSrcFile,
107171 std::string &line,
108172 const std::string &streamName,
109173 const std::string &viewDataName,
110- int &cxx_flag,
111- int returnFlag = 1 )
174+ int &cxx_flag)
112175{
113176 std::string::size_type pos (0 );
114177 // std::cout<<line<<"("<<line.length()<<")\n";
115178 if (line.length () > 0 && line[line.length () - 1 ] == ' \r ' )
116179 {
117180 line.resize (line.length () - 1 );
118181 }
119- if (line.length () == 0 )
120- {
121- // std::cout<<"blank line!"<<std::endl;
122- // std::cout<<streamName<<"<<\"\\n\";\n";
123- if (returnFlag && !cxx_flag)
124- oSrcFile << streamName << " <<\"\\ n\" ;\n " ;
125- return ;
126- }
182+
127183 if (cxx_flag == 0 )
128184 {
129- // find cxx lang begin
130- if ((pos = line.find (cxx_lang)) != std::string::npos)
185+ auto found_tag = findFirstLeftTag (line);
186+ pos = found_tag.second ;
187+
188+ if (found_tag.second == std::string::npos)
131189 {
190+ // line dose not contain any tags
191+ outputText (oSrcFile, line, streamName, true );
192+ return ;
193+ }
194+
195+ if (pos != 0 )
196+ {
197+ // [output here]<%c++ ...%>...
132198 std::string oldLine = line.substr (0 , pos);
133- if (oldLine.length () > 0 )
134- parseLine (
135- oSrcFile, oldLine, streamName, viewDataName, cxx_flag, 0 );
199+ outputText (oSrcFile, oldLine, streamName, false );
200+ }
201+
202+ if (found_tag.first == CspLeftTagType::kCxxStart )
203+ {
136204 std::string newLine = line.substr (pos + cxx_lang.length ());
137205 cxx_flag = 1 ;
138206 if (newLine.length () > 0 )
139- parseLine (oSrcFile,
140- newLine,
141- streamName,
142- viewDataName,
143- cxx_flag,
144- returnFlag);
207+ parseLine (
208+ oSrcFile, newLine, streamName, viewDataName, cxx_flag);
145209 }
146- else
210+ else if (found_tag. first == CspLeftTagType:: kCxxValStart )
147211 {
148- if ((pos = line.find (cxx_val_start)) != std::string::npos)
212+ std::string newLine = line.substr (pos + cxx_val_start.length ());
213+ if ((pos = newLine.find (cxx_val_end)) != std::string::npos)
149214 {
150- std::string oldLine = line.substr (0 , pos);
215+ std::string keyName = newLine.substr (0 , pos);
216+ auto i = keyName.begin ();
217+ outputVal (oSrcFile,
218+ streamName,
219+ viewDataName,
220+ parseParameter (keyName));
221+
222+ std::string tailLine =
223+ newLine.substr (pos + cxx_val_end.length ());
151224 parseLine (
152- oSrcFile, oldLine, streamName, viewDataName, cxx_flag, 0 );
153- std::string newLine = line.substr (pos + cxx_val_start.length ());
154- if ((pos = newLine.find (cxx_val_end)) != std::string::npos)
155- {
156- std::string keyName = newLine.substr (0 , pos);
157- auto iter = keyName.begin ();
158- while (iter != keyName.end () && *iter == ' ' )
159- ++iter;
160- auto iterEnd = iter;
161- while (iterEnd != keyName.end () && *iterEnd != ' ' )
162- ++iterEnd;
163- keyName = std::string (iter, iterEnd);
164- outputVal (oSrcFile, streamName, viewDataName, keyName);
165- std::string tailLine =
166- newLine.substr (pos + cxx_val_end.length ());
167- parseLine (oSrcFile,
168- tailLine,
169- streamName,
170- viewDataName,
171- cxx_flag,
172- returnFlag);
173- }
174- else
175- {
176- std::cerr << " format err!" << std::endl;
177- exit (1 );
178- }
225+ oSrcFile, tailLine, streamName, viewDataName, cxx_flag);
179226 }
180- else if ((pos = line. find (sub_view_start)) != std::string::npos)
227+ else
181228 {
182- std::string oldLine = line.substr (0 , pos);
183- parseLine (
184- oSrcFile, oldLine, streamName, viewDataName, cxx_flag, 0 );
185- std::string newLine =
186- line.substr (pos + sub_view_start.length ());
187- if ((pos = newLine.find (sub_view_end)) != std::string::npos)
188- {
189- std::string keyName = newLine.substr (0 , pos);
190- auto iter = keyName.begin ();
191- while (iter != keyName.end () && *iter == ' ' )
192- ++iter;
193- auto iterEnd = iter;
194- while (iterEnd != keyName.end () && *iterEnd != ' ' )
195- ++iterEnd;
196- keyName = std::string (iter, iterEnd);
197- outputSubView (oSrcFile, streamName, viewDataName, keyName);
198- std::string tailLine =
199- newLine.substr (pos + sub_view_end.length ());
200- parseLine (oSrcFile,
201- tailLine,
229+ std::cerr << " format err!" << std::endl;
230+ exit (1 );
231+ }
232+ }
233+ else if (found_tag.first == CspLeftTagType::kSubViewStart )
234+ {
235+ std::string newLine = line.substr (pos + sub_view_start.length ());
236+ if ((pos = newLine.find (sub_view_end)) != std::string::npos)
237+ {
238+ std::string keyName = newLine.substr (0 , pos);
239+ outputSubView (oSrcFile,
202240 streamName,
203241 viewDataName,
204- cxx_flag,
205- returnFlag);
206- }
207- else
208- {
209- std::cerr << " format err!" << std::endl;
210- exit (1 );
211- }
242+ parseParameter (keyName));
243+
244+ std::string tailLine =
245+ newLine.substr (pos + sub_view_end.length ());
246+ parseLine (
247+ oSrcFile, tailLine, streamName, viewDataName, cxx_flag);
212248 }
213249 else
214250 {
215- if (line.length () > 0 )
216- {
217- replace_all (line, " \\ " , " \\\\ " );
218- replace_all (line, " \" " , " \\\" " );
219- oSrcFile << " \t " << streamName << " << \" " << line;
220- }
221- if (returnFlag)
222- oSrcFile << " \\ n\" ;\n " ;
223- else
224- oSrcFile << " \" ;\n " ;
251+ std::cerr << " format err!" << std::endl;
252+ exit (1 );
225253 }
226254 }
227255 }
228256 else
229257 {
230258 if ((pos = line.find (cxx_end)) != std::string::npos)
231259 {
232- std::string newLine = line.substr (0 , pos);
233- parseCxxLine (oSrcFile, newLine, streamName, viewDataName);
234- std::string oldLine = line.substr (pos + cxx_end.length ());
260+ std::string cxx_part = line.substr (0 , pos);
261+ parseCxxLine (oSrcFile, cxx_part, streamName, viewDataName);
262+
263+ std::string tailLine = line.substr (pos + cxx_end.length ());
235264 cxx_flag = 0 ;
236- if (oldLine.length () > 0 )
237- parseLine (oSrcFile,
238- oldLine,
239- streamName,
240- viewDataName,
241- cxx_flag,
242- returnFlag);
265+ if (tailLine.length () > 0 )
266+ parseLine (
267+ oSrcFile, tailLine, streamName, viewDataName, cxx_flag);
243268 }
244269 else
245270 {
0 commit comments