webCommon.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "webConfig.h"
  23. #include "webCommon.h"
  24. #include <string>
  25. #include <vector>
  26. // Platform specific shared library handling
  27. #ifdef WIN32
  28. #pragma warning( disable : 4996)
  29. #define TORQUE_OPEN LoadLibraryA
  30. #define TORQUE_FUNCTION GetProcAddress
  31. #define TORQUE_CLOSE FreeLibrary
  32. #define strncasecmp strnicmp
  33. #define strcasecmp stricmp
  34. #else // Mac
  35. #define TORQUE_OPEN(path) dlopen(path, RTLD_LAZY | RTLD_LOCAL)
  36. #define TORQUE_FUNCTION dlsym
  37. #define TORQUE_CLOSE dlclose
  38. #endif
  39. // C Interface exported from the Torque 3D DLL (or Bundle on Mac)
  40. extern "C"
  41. {
  42. // initialize Torque 3D including argument handling
  43. bool (*torque_engineinit)(int argc, const char **argv) = NULL;
  44. // tick Torque 3D's main loop
  45. int (*torque_enginetick)() = NULL;
  46. // set Torque 3D into web deployment mode (disable fullscreen exlusive mode, etc)
  47. int (*torque_setwebdeployment)() = NULL;
  48. // shutdown the engine
  49. bool (*torque_engineshutdown)() = NULL;
  50. // signal an engine shutdown (as with the quit(); console command)
  51. void (*torque_enginesignalshutdown)() = NULL;
  52. // reset the engine, unloading any current level and returning to the main menu
  53. void (*torque_reset)() = NULL;
  54. // Evaluate arbitrary TorqueScript (ONLY CALL torque_evaluate FROM TRUSTED CODE!!!)
  55. const char* (*torque_evaluate)(const char* code) = NULL;
  56. // Get a console variable
  57. const char* (*torque_getvariable)(const char* name) = NULL;
  58. // Set a console variable
  59. void (*torque_setvariable)(const char* name, const char* value) = NULL;
  60. // Export a function to the Torque 3D console system which matches the StringCallback function prototype
  61. // specify the nameSpace, functionName, usage, min and max arguments
  62. void (*torque_exportstringcallback)(StringCallback cb, const char *nameSpace, const char *funcName, const char* usage, int minArgs, int maxArgs) = NULL;
  63. // Set a TorqueScript console function as secure and available for JavaScript via the callScript plugin method
  64. void (*torque_addsecurefunction)(const char* nameSpace, const char* fname) = NULL;
  65. // Call a TorqueScript console function that has been marked as secure
  66. const char* (*torque_callsecurefunction)(const char* nameSpace, const char* name, int argc, const char ** argv) = NULL;
  67. // resize the Torque 3D child window to the specified width and height
  68. void (*torque_resizewindow)(int width, int height) = NULL;
  69. #ifndef WIN32
  70. // On Mac, handle the parent safari window
  71. void (*torque_setsafariwindow)(NSWindow* window, int32 x, int32 y, int32 width, int32 height) = NULL;
  72. // On Mac, sets the executable path
  73. void (*torque_setexecutablepath)(const char *path) = NULL;
  74. #else
  75. // retrieve the render windows hwnd
  76. void* (*torque_gethwnd)() = NULL;
  77. // directly add a message to the Torque 3D event queue, bypassing the Windows event queue
  78. // this is useful in the case of the IE plugin, where we are hooking into an application
  79. // level message, and posting to the windows queue would cause a hang
  80. void (*torque_directmessage)(unsigned int message, unsigned int wparam, unsigned int lparam) = NULL;
  81. #endif
  82. };
  83. namespace WebCommon
  84. {
  85. std::string gPluginMIMEType;
  86. #ifdef WIN32
  87. HMODULE gTorque3DModule = NULL;
  88. HMODULE gPluginModule = 0;
  89. // bring up a platform specific message box (used for error reporting)
  90. void MessageBox(void* parentWindow, const char* msg, const char* caption )
  91. {
  92. ::MessageBoxA( (HWND) parentWindow, msg, caption, MB_OK|MB_ICONWARNING);
  93. }
  94. // retrieve the game directory using the filename (which includes the full path) of the plugin DLL
  95. const char* GetGameDirectory()
  96. {
  97. static char dir[4096];
  98. GetModuleFileNameA(gPluginModule, dir, 4096);
  99. int i = strlen(dir) - 1;
  100. while (i>=0)
  101. {
  102. if (dir[i] == '\\' || dir[i] == '/')
  103. {
  104. dir[i] = 0;
  105. break;
  106. }
  107. i--;
  108. }
  109. return dir;
  110. }
  111. // retrieve the name of our game DLL (includes Torque 3D engine) based on naming convention
  112. const char* GetGameLibrary()
  113. {
  114. char dir[4096];
  115. static char lib[4096];
  116. lib[0] = 0;
  117. GetModuleFileNameA(gPluginModule, dir, 4096);
  118. int i = strlen(dir) - 1;
  119. while (i>=0)
  120. {
  121. if (dir[i] == '\\' || dir[i] == '/')
  122. {
  123. // copy, minus the "NP " or "IE " of plugin name
  124. #ifdef _DEBUG
  125. sprintf(lib, "%s_DEBUG.dll", &dir[i+4]);
  126. #else
  127. sprintf(lib, "%s.dll", &dir[i+4]);
  128. #endif
  129. return lib;
  130. }
  131. // strip off end
  132. if (!strncmp(&dir[i], " Plugin", 7))
  133. dir[i] = 0;
  134. i--;
  135. }
  136. return lib;
  137. }
  138. #else
  139. void* gTorque3DModule = NULL;
  140. NSBundle* gPluginBundle = NULL;
  141. // bring up a platform specific message box (used for error reporting)
  142. void MessageBox(void* parentWindow, const char* msg, const char* caption )
  143. {
  144. // convert title and message to NSStrings
  145. NSString *nsTitle = [NSString stringWithUTF8String:caption];
  146. NSString *nsMessage = [NSString stringWithUTF8String:msg];
  147. NSAlert *alert = [NSAlert alertWithMessageText:nsTitle
  148. defaultButton:@"OK"
  149. alternateButton:nil
  150. otherButton:nil
  151. informativeTextWithFormat:nsMessage];
  152. [alert runModal];
  153. }
  154. NSBundle* GetPluginBundle()
  155. {
  156. if (gPluginBundle)
  157. return gPluginBundle;
  158. NSDictionary *mimeTypes;
  159. NSString *mime;
  160. NSEnumerator *f;
  161. NSArray *bundles = [NSBundle allBundles];
  162. for (int i = 0; i < [bundles count]; i++) {
  163. NSBundle *b = [bundles objectAtIndex:i];
  164. mimeTypes=[b objectForInfoDictionaryKey:@"WebPluginMIMETypes"];
  165. if (!mimeTypes)
  166. continue;
  167. f=[mimeTypes keyEnumerator];
  168. while((mime=[f nextObject]))
  169. {
  170. if (gPluginMIMEType == std::string([mime UTF8String]))
  171. {
  172. gPluginBundle = b;
  173. break;
  174. }
  175. }
  176. }
  177. return gPluginBundle;
  178. }
  179. // retrieve the game's install folder based on entries from our plugin's Info.plist
  180. const char* GetGameDirectory()
  181. {
  182. static char gamePath[2048] = {'\0'};
  183. if (gamePath[0])
  184. return gamePath;
  185. NSBundle* pluginBundle = GetPluginBundle();
  186. if (!pluginBundle)
  187. return NULL;
  188. NSString *gameInstallPathKey = [NSString stringWithUTF8String:"GameInstallPath"];
  189. NSString *gameInstallPath = [pluginBundle objectForInfoDictionaryKey: gameInstallPathKey];
  190. if (!gameInstallPath)
  191. return NULL;
  192. strcpy(gamePath, [gameInstallPath UTF8String]);
  193. if (!gamePath[0])
  194. return NULL;
  195. return gamePath;
  196. }
  197. // retrieve the game bundle (including Torque 3D engine) from the plugin Info.plist
  198. const char* GetGameLibrary()
  199. {
  200. static char libPath[2048] = {'\0'};
  201. if (libPath[0])
  202. return libPath;
  203. const char* gamePath = GetGameDirectory();
  204. if (!gamePath)
  205. return NULL;
  206. NSBundle* pluginBundle = GetPluginBundle();
  207. if (!pluginBundle)
  208. return NULL;
  209. // NSString* bundleIdentifier = [pluginBundle bundleIdentifier];
  210. NSString *gameNameKey = [NSString stringWithUTF8String:"GameName"];
  211. NSString *gameName = [pluginBundle objectForInfoDictionaryKey: gameNameKey];
  212. if (!gameName)
  213. return NULL;
  214. const char* cgameName = [gameName UTF8String];
  215. if (!cgameName[0])
  216. return NULL;
  217. #ifdef DEBUG
  218. sprintf(libPath, "%s%s_DEBUG.app/Contents/Frameworks/%s Bundle.bundle/Contents/MacOS/%s Bundle", gamePath, cgameName, cgameName, cgameName);
  219. #else
  220. sprintf(libPath, "%s%s.app/Contents/Frameworks/%s Bundle.bundle/Contents/MacOS/%s Bundle", gamePath, cgameName, cgameName, cgameName);
  221. #endif
  222. return libPath;
  223. }
  224. #endif
  225. bool ChangeToGameDirectory()
  226. {
  227. const char* gameDir = GetGameDirectory();
  228. if (!gameDir)
  229. return false;
  230. #ifdef WIN32
  231. return SetCurrentDirectoryA(gameDir);
  232. #else
  233. return (chdir(gameDir) == 0);
  234. #endif
  235. }
  236. // loads the Torque 3D shared library, sets web deployment mode, retrieves engine "C" interface
  237. bool InitTorque3D(void* platformWindow, int clipLeft, int clipTop, int clipRight, int clipBottom)
  238. {
  239. const char* gameDir = GetGameDirectory();
  240. const char* gameLib = GetGameLibrary();
  241. if (gTorque3DModule)
  242. {
  243. WebCommon::MessageBox( 0, "This plugin allows only one instance", "Error");
  244. return false;
  245. }
  246. if (!gameDir || !gameLib)
  247. {
  248. WebCommon::MessageBox( 0, "Unable to get game plugin information", "Error");
  249. return false;
  250. }
  251. if (!ChangeToGameDirectory())
  252. return false;
  253. std::string gameLibStr = gameLib;
  254. #ifdef WIN32
  255. // We want to use an absolute path to the game library
  256. gameLibStr = gameDir;
  257. WebCommon::ConvertToWindowsPathSep(gameLibStr);
  258. gameLibStr += "\\";
  259. gameLibStr += gameLib;
  260. #endif
  261. gTorque3DModule = TORQUE_OPEN(gameLibStr.c_str());
  262. if (!gTorque3DModule)
  263. {
  264. char error[4096];
  265. #ifdef WIN32
  266. sprintf(error, "Could not load game library: %s/%s. Please make sure you have the latest DirectX installed.", gameDir, gameLib);
  267. #else
  268. sprintf(error, "Could not load game library: %s/%s. ", gameDir, gameLib);
  269. #endif
  270. WebCommon::MessageBox( 0, error, "Error");
  271. return false;
  272. }
  273. // snag all the exported functions of the "C" interface
  274. torque_engineinit = (bool (*)(int argc, const char **argv))TORQUE_FUNCTION(gTorque3DModule, "torque_engineinit");
  275. torque_enginetick = (int (*)())TORQUE_FUNCTION(gTorque3DModule, "torque_enginetick");
  276. torque_setwebdeployment = (int (*)())TORQUE_FUNCTION(gTorque3DModule, "torque_setwebdeployment");
  277. torque_engineshutdown = (bool (*)())TORQUE_FUNCTION(gTorque3DModule, "torque_engineshutdown");
  278. torque_enginesignalshutdown = (void (*)())TORQUE_FUNCTION(gTorque3DModule, "torque_enginesignalshutdown");
  279. torque_reset = (void (*)())TORQUE_FUNCTION(gTorque3DModule, "torque_reset");
  280. torque_evaluate = (const char* (*)(const char* code))TORQUE_FUNCTION(gTorque3DModule, "torque_evaluate");
  281. torque_getvariable = (const char* (*)(const char* name))TORQUE_FUNCTION(gTorque3DModule, "torque_getvariable");
  282. torque_setvariable = (void (*)(const char* name, const char* value))TORQUE_FUNCTION(gTorque3DModule, "torque_setvariable");
  283. torque_exportstringcallback = (void (*)(StringCallback cb, const char *nameSpace, const char *funcName, const char* usage, int minArgs, int maxArgs))TORQUE_FUNCTION(gTorque3DModule, "torque_exportstringcallback");
  284. torque_addsecurefunction = (void (*)(const char* nameSpace, const char* name))TORQUE_FUNCTION(gTorque3DModule, "torque_addsecurefunction");
  285. torque_callsecurefunction = (const char* (*)(const char* nameSpace, const char* name, int argc, const char ** argv))TORQUE_FUNCTION(gTorque3DModule, "torque_callsecurefunction");
  286. torque_resizewindow = (void (*)(int width, int height))TORQUE_FUNCTION(gTorque3DModule, "torque_resizewindow");
  287. // check that we got them all
  288. if (!torque_engineinit ||
  289. !torque_enginetick ||
  290. !torque_setwebdeployment ||
  291. !torque_engineshutdown ||
  292. !torque_enginesignalshutdown ||
  293. !torque_reset ||
  294. !torque_evaluate ||
  295. !torque_getvariable ||
  296. !torque_setvariable ||
  297. !torque_exportstringcallback ||
  298. !torque_addsecurefunction ||
  299. !torque_callsecurefunction ||
  300. !torque_resizewindow )
  301. {
  302. WebCommon::MessageBox( platformWindow, "The plugin could not be initialized (missing function exports)", "Error");
  303. TORQUE_CLOSE(gTorque3DModule);
  304. gTorque3DModule = NULL;
  305. return false;
  306. }
  307. #ifndef WIN32
  308. torque_setexecutablepath = (void (*)(const char *path)) dlsym(gTorque3DModule, "torque_setexecutablepath");
  309. torque_setsafariwindow = (void (*)(NSWindow* nswnd, int32, int32, int32, int32)) dlsym(gTorque3DModule, "torque_setsafariwindow");
  310. if (!torque_setexecutablepath || !torque_setsafariwindow)
  311. {
  312. WebCommon::MessageBox( platformWindow, "The plugin could not be initialized (missing function exports)", "Error");
  313. TORQUE_CLOSE(gTorque3DModule);
  314. gTorque3DModule = NULL;
  315. return false;
  316. }
  317. #else
  318. torque_gethwnd = (void* (*)())TORQUE_FUNCTION(gTorque3DModule, "torque_gethwnd");
  319. torque_directmessage = (void (*)(unsigned int message, unsigned int wparam, unsigned int lparam))TORQUE_FUNCTION(gTorque3DModule, "torque_directmessage");
  320. if (!torque_gethwnd || !torque_directmessage)
  321. {
  322. WebCommon::MessageBox( platformWindow, "The plugin could not be initialized (missing function exports)", "Error");
  323. TORQUE_CLOSE(gTorque3DModule);
  324. gTorque3DModule = NULL;
  325. return false;
  326. }
  327. #endif
  328. //tell Torque3D that we're a browser plugin
  329. torque_setwebdeployment();
  330. const char* args[3];
  331. int argc;
  332. #ifdef WIN32
  333. // windows uses a command line arg for parent window
  334. char parentWindow[256];
  335. argc = 3;
  336. sprintf(parentWindow, "%I64u", (unsigned __int64)platformWindow);
  337. args[0] = "game.exe"; //just to satisfy command line parsing
  338. args[1] = "-window";
  339. args[2] = parentWindow;
  340. #else
  341. NSWindow* browserWindow = (NSWindow*) platformWindow;
  342. // tell Torque 3D about our parent browser window
  343. // we initialize with zero size as the page hasn't completely loaded yet
  344. // so, the plugin hasn't been resized by the page and it is better to not show
  345. // anything than wrong extents
  346. torque_setsafariwindow( browserWindow, 0, 0, 0, 0);
  347. argc = 1;
  348. args[0] = gameDir; // just to satisfy command line parsing
  349. #endif
  350. // initialize Torque 3D!
  351. if (!torque_engineinit(argc, args))
  352. {
  353. WebCommon::MessageBox( platformWindow, "The plugin could not be initialized (internal initialization error)", "Error");
  354. return false;
  355. }
  356. return true;
  357. }
  358. // unloads the Torque 3D shared library (first signaling a shutdown for clean exit)
  359. void ShutdownTorque3D()
  360. {
  361. if (gTorque3DModule)
  362. {
  363. ChangeToGameDirectory();
  364. torque_enginesignalshutdown();
  365. torque_enginetick();
  366. torque_engineshutdown();
  367. TORQUE_CLOSE(gTorque3DModule);
  368. }
  369. gTorque3DModule = NULL;
  370. }
  371. // checks a given domain against the allowed domains in webConfig.h
  372. bool CheckDomain(const char* url)
  373. {
  374. bool domainCheck = true;
  375. #ifndef WEBDEPLOY_DOMAIN_CHECK
  376. domainCheck = false;
  377. #endif
  378. #ifdef DEBUG
  379. # ifdef WEBDEPLOY_DOMAIN_ALLOW_DEBUG
  380. domainCheck = false;
  381. # endif
  382. #endif
  383. if (!domainCheck)
  384. return true; // gets rid of "unreachable code" warning
  385. if (strlen(url) > 512)
  386. return false;
  387. if (strlen(url) < 5)
  388. return false;
  389. //do not allow file when using domain checking
  390. if (!strncasecmp(url,"file",4))
  391. return false;
  392. char curl[512] = {0};
  393. unsigned int begin = 0;
  394. while(url[begin])
  395. {
  396. if (url[begin] == ':')
  397. {
  398. if (begin + 3 > strlen(url))
  399. return false;
  400. begin+=3; //skip ://
  401. break;
  402. }
  403. begin++;
  404. }
  405. unsigned int end = begin;
  406. while(end < strlen(url))
  407. {
  408. if (url[end] == '/')
  409. {
  410. break;
  411. }
  412. end++;
  413. }
  414. strcpy(curl, &url[begin]);
  415. curl[end-begin] = 0;
  416. // iterate checking against our allowed domains
  417. for (int i = 0; gAllowedDomains[i]; i++)
  418. if (!strcasecmp(curl, gAllowedDomains[i]))
  419. return true;
  420. WebCommon::MessageBox( 0 , "This plugin cannot be executed from the domain specified", "Error");
  421. return false;
  422. }
  423. // exposes TorqueScript functions marked as secure in webConfig.h, these functions can then be called on the page via Javascript
  424. void AddSecureFunctions()
  425. {
  426. char snamespace[256];
  427. char fname[256];
  428. //define secure functions here
  429. for (unsigned int i = 0; gSecureScript[i]; i++)
  430. {
  431. snamespace[0] = 0;
  432. strcpy(fname, gSecureScript[i]);
  433. //scan through looking for namespace
  434. for (unsigned int j = 1; j < strlen(fname)-2; j++)
  435. {
  436. if (fname[j] == ':' && fname[j+1] == ':')
  437. {
  438. strcpy(snamespace, gSecureScript[i]);
  439. snamespace[j] = 0;
  440. strcpy(fname,&gSecureScript[i][j+2]);
  441. break;
  442. }
  443. }
  444. torque_addsecurefunction(snamespace, fname);
  445. }
  446. }
  447. //simple string copy that eats white space
  448. void StringCopy(char* dst, const char* src, int count)
  449. {
  450. int i, j;
  451. bool eat = true;
  452. for (i = 0, j = 0; i < count ; i++)
  453. {
  454. if (src[i] == 0)
  455. {
  456. dst[j] = 0;
  457. return;
  458. }
  459. if (src[i] != '"' && src[i] != '\t' && src[i] != '\n' && src[i] != ')' && src[i] != '(')
  460. {
  461. if (eat && src[i] == ' ')
  462. continue;
  463. if (src[i] == '\'')
  464. {
  465. eat = !eat;
  466. continue;
  467. }
  468. dst[j++] = src[i];
  469. }
  470. }
  471. }
  472. void SetVariable(const char* variable, const char* value)
  473. {
  474. char mvar[1024];
  475. char mvar2[1024];
  476. if (strlen(variable) > 1023)
  477. {
  478. WebCommon::MessageBox( 0, "WebCommon::SetVariable - buffer overrun", "Error");
  479. return;
  480. }
  481. // make local copies stripping off $ decorator is needed
  482. if (variable[0] == '$')
  483. strcpy(mvar, &variable[1]);
  484. else
  485. strcpy(mvar, variable);
  486. const char* js = "javascript::";
  487. if (strncasecmp(js, mvar, 12))
  488. sprintf(mvar2, "Javascript::%s", mvar);
  489. else
  490. strcpy(mvar2, mvar);
  491. torque_setvariable(mvar2, value);
  492. }
  493. const char* GetVariable(const char* variable)
  494. {
  495. char mvar[1024];
  496. char mvar2[1024];
  497. if (strlen(variable) > 1023)
  498. {
  499. WebCommon::MessageBox( 0, "WebCommon::GetVariable - buffer overrun", "Error");
  500. return "0";
  501. }
  502. // make local copies stripping off $ decorator is needed
  503. if (variable[0] == '$')
  504. strcpy(mvar, &variable[1]);
  505. else
  506. strcpy(mvar, variable);
  507. const char* js = "javascript::";
  508. if (strncasecmp(js, mvar, 12))
  509. sprintf(mvar2, "Javascript::%s", mvar);
  510. else
  511. strcpy(mvar2, mvar);
  512. return torque_getvariable(mvar2);
  513. }
  514. #ifdef WIN32
  515. // string conversion to/from wstring and string
  516. std::wstring StringToWString( const std::string& str )
  517. {
  518. size_t size = str.length();
  519. wchar_t* w = new wchar_t[size+1];
  520. memset( w, 0, sizeof(wchar_t) * (size+1) );
  521. MultiByteToWideChar( CP_ACP,
  522. 0,
  523. str.c_str(),
  524. size,
  525. w,
  526. size );
  527. std::wstring ws(w);
  528. delete[] w;
  529. return ws;
  530. }
  531. std::string WStringToString( const std::wstring& wstr )
  532. {
  533. size_t size = wstr.length();
  534. char* s = new char[size+1];
  535. memset( s, 0, sizeof(char) * (size+1) );
  536. WideCharToMultiByte( CP_ACP,
  537. 0,
  538. wstr.c_str(),
  539. size,
  540. s,
  541. size,
  542. NULL,
  543. NULL );
  544. std::string str(s);
  545. delete[] s;
  546. return str;
  547. }
  548. void ConvertToWindowsPathSep(std::string& path)
  549. {
  550. size_t pos = 0;
  551. while(pos < path.size() || pos != std::string::npos)
  552. {
  553. pos = path.find("/", pos);
  554. if(pos != std::string::npos)
  555. {
  556. path.replace(pos, 1, "\\");
  557. ++pos;
  558. }
  559. }
  560. }
  561. #endif
  562. } //namespace WebCommon