1 // Copyright (c) 2012 Dennco Project
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 // Created by tkawata on 1/7/2012.
29 void trimString(std::string& str)
31 std::string::size_type pos1 = str.find_first_not_of(' ');
32 std::string::size_type pos2 = str.find_last_not_of(' ');
33 std::string::size_type pos1_2 = str.find_first_not_of('\t');
34 std::string::size_type pos2_2 = str.find_last_not_of('\t');
36 pos1 = pos1 < pos1_2 ? pos1_2 : pos1;
37 pos2 = pos2 < pos2_2 ? pos2_2 : pos2;
39 str = str.substr(pos1 == std::string::npos ? 0 : pos1,
40 pos2 == std::string::npos ? str.length() - 1 : pos2 - pos1 + 1);
42 std::string parseCellCodeForScriptEngine(std::string jname, std::string cellCode)
45 DEBUG_TRACE("TKJSCellCode::TKJSCellCode() parse for %s\n=== Original code =====\n%s\n", jname.c_str(), cellCode.c_str());
48 const char *c = cellCode.c_str();
50 int inComment1 = 0; // //
51 int inComment2 = 0; // /* ... */
52 int inComment2_end = 0;
56 int inDoubleQuote = 0;
61 std::string funcs = "";
62 std::string vars = "";
65 const char* str_comment1 = "//";
66 const char* str_comment2 = "/*";
67 const char* str_comment2_end = "*/";
68 const char* str_function = "function";
69 const char* str_var = "var";
73 if (*c == '\r' || *c == '\n')
77 else if (*c != ' ' && *c != '\t')
84 if (*c == '\r' || *c == '\n' )
89 else if (inComment2 == -1)
91 if (*c == str_comment2_end[inComment2_end])
94 if (str_comment2_end[inComment2_end] == 0)
123 else if (inDoubleQuote == -1)
154 if (*c == str_comment1[inComment1])
157 if (str_comment1[inComment1] == 0)
169 if (*c == str_comment2[inComment2])
172 if (str_comment2[inComment2] == 0)
184 if (!inComment1 && !inComment2 && !inQuote && !inDoubleQuote && *c == '{')
187 if (inFunction == -1 && nest == 1)
190 fff.append(sp,c-sp+1);
191 unsigned long fp = fff.find( "(", 0 );
192 if( fp != std::string::npos )
194 fff = fff.substr(0, fp);
204 else if (!inComment1 && !inComment2 && !inQuote && !inDoubleQuote && *c == '}')
209 if (inFunction == -1)
214 funcs.append(".prototype.");
216 funcs.append(" = function");
217 funcs.append(sp ,c-sp+1);
218 funcs.append("\r\n");
234 vars.append("this.");
243 if (inFunction == -1)
246 if (*c != ' ' && *c != '\t' && *c != '\r' && *c != '\n')
262 if (*c != ' ' && *c != '\t' && *c != '\r' && *c != '\n' && *c != '=')
276 if (nest == 0 && *c == str_function[inFunction] && (inFunction+1) == lp)
279 if (str_function[inFunction] == 0)
289 if (nest == 0 && *c == str_var[inVar] && (inVar+1) == lp)
292 if (str_var[inVar] == 0)
306 std::string stmt = "";
307 stmt.append("function ");
309 stmt.append("(ownerCell)\n{\nthis.cell = ownerCell;\n");
311 stmt.append("\n}\n");
314 DEBUG_TRACE("\n\n==== Translated JS statement:=====\n%s\n=====\n", stmt.c_str());
319 std::string getFQNString(const char *location, const char *name)
323 int l = (int)strlen(location) - 1;
327 if (location[l] == '/')
344 path = node.substr(0,l);
345 node = node.substr(l);
354 const char *c = name;
361 if (i >= 2 && *(c - 2) == '.' && *(c - 1) == '.')
363 unsigned long l = path.find_last_of("/");
364 if( l != std::string::npos )
372 path = path.substr(0, l);
381 else if (i >= 1 && *(c - 1) == '.')
385 else if (i >= 1 && *(c - 1) == '/')
391 if (path.length() > 0 && path.at(0) == '/')
402 size_t pl = path.length();
412 char p1 = path.at(0);
413 if (p1 == '/' || p1 == '.')
428 std::string s(p, c-p);
449 bool hasAnchor = false;
471 int pl = (int)path.length();
475 if (node.at(0) == '/' && node.length() > 1)
477 fqn = node.substr(1);
487 if (path.at(0) == '/' && node.length() > 0 && node.at(0) == '/')
493 fqn = path.append(node);
498 fqn = path.append(node);
504 std::string getJSEscapeString(const char *cstring)
506 const char *c = cstring;
507 std::string result = "";