Utility.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. ** Copyright (C) 1996, 1997 Microsoft Corporation. All Rights Reserved.
  3. **
  4. ** File: utility.cpp
  5. **
  6. ** Author:
  7. **
  8. ** Description:
  9. ** Implementation of the non-templated utl classes. See utl.h.
  10. **
  11. ** History:
  12. */
  13. #include "pch.h"
  14. const Rotation c_rotationZero(0.0f, 0.0f, 1.0f, 0.0f);
  15. char UTL::s_artworkPath[MAX_PATH] = "";
  16. char UTL::s_szUrlRoot[MAX_PATH] = "";
  17. #ifndef DREAMCAST
  18. void UTL::SetUrlRoot(const char * szUrlRoot)
  19. {
  20. assert (szUrlRoot);
  21. strcpy(s_szUrlRoot, szUrlRoot);
  22. }
  23. void UTL::SetArtPath(const char* szArtwork)
  24. {
  25. int cbsz = lstrlen(szArtwork);
  26. assert(cbsz > 0 && cbsz < MAX_PATH);
  27. bool fTrailingBS = szArtwork[cbsz - 1] == '\\';
  28. lstrcpy(s_artworkPath, szArtwork);
  29. if (!fTrailingBS)
  30. {
  31. s_artworkPath[cbsz] = '\\';
  32. s_artworkPath[cbsz + 1] = 0;
  33. }
  34. }
  35. HRESULT UTL::getFile( const char* name,
  36. const char* extension,
  37. OUT char* artwork,
  38. bool downloadF,
  39. bool createF)
  40. {
  41. HRESULT rc = E_FAIL;
  42. assert (name && extension && artwork && *s_artworkPath);
  43. strcpy(artwork, s_artworkPath);
  44. {
  45. char* pArtwork = artwork + strlen(artwork);
  46. const char* pName = name;
  47. while ((*pName != '\0') && (*pName != ' '))
  48. *(pArtwork++) = *(pName++);
  49. strcpy(pArtwork, extension);
  50. }
  51. OFSTRUCT filedata;
  52. filedata.cBytes = sizeof(filedata);
  53. if (OpenFile(artwork, &filedata, OF_EXIST) != HFILE_ERROR)
  54. rc = S_OK;
  55. else
  56. debugf("Unable to open %s%s\n", name, extension);
  57. return rc;
  58. }
  59. /////////////////////////////////////////////////////////
  60. // SaveFile()
  61. //
  62. // Writes a blob to disk.
  63. //
  64. // szErrorMsg should be NULL if no error msg is wanted
  65. //
  66. // returns true iff no errors
  67. //
  68. bool UTL::SaveFile(const char * szFilename, const void *pData, unsigned cLen, OUT char * szErrorMsg, bool bCreateAsTemp)
  69. {
  70. HANDLE hFile = CreateFile(szFilename,
  71. GENERIC_WRITE,
  72. FILE_SHARE_READ,
  73. NULL,
  74. CREATE_ALWAYS,
  75. bCreateAsTemp ? FILE_ATTRIBUTE_TEMPORARY : FILE_ATTRIBUTE_NORMAL, // use bCreateAsTemp for don't write to disk write away (for better performance).
  76. NULL);
  77. if (hFile == INVALID_HANDLE_VALUE)
  78. {
  79. if (szErrorMsg)
  80. sprintf(szErrorMsg, "Failed create file (%s); Error Code: %d ", szFilename, GetLastError());
  81. return false;
  82. }
  83. unsigned long cBytesWritten;
  84. if (!WriteFile(hFile, pData, cLen, &cBytesWritten, NULL))
  85. {
  86. if (szErrorMsg)
  87. sprintf(szErrorMsg, "Failed write file (%s); Error Code: %d ", szFilename, GetLastError());
  88. return false;
  89. }
  90. if (!::CloseHandle(hFile))
  91. {
  92. if (szErrorMsg)
  93. sprintf(szErrorMsg, "Couldn't close file (%s); Error Code: %d ", szFilename, GetLastError());
  94. return false;
  95. }
  96. return true;
  97. }
  98. /////////////////////////////////////////////////////////
  99. // AppendFile()
  100. //
  101. // Appends data to the end of a file
  102. //
  103. // returns true iff no errors
  104. //
  105. bool UTL::AppendFile(const char * szFileName, const void * data, unsigned bytes)
  106. {
  107. FILE* f;
  108. f = fopen( szFileName, "ab" ); // a == open for appending
  109. if (f == 0) return 0;
  110. if (fwrite( data, bytes, 1, f ) != 1)
  111. {
  112. fclose(f);
  113. return 0;
  114. }
  115. if (fclose(f)) return 0;
  116. return 1;
  117. }
  118. /////////////////////////////////////////////////////////
  119. // SearchAndReplace()
  120. //
  121. // Searches a string, replacing a particular word with another. The case of the old word
  122. // doesn't matter.
  123. //
  124. // returns number of replacements made
  125. //
  126. int UTL::SearchAndReplace(char * szDest, const char * szSource, const char * szNewWord, const char * szOldWord)
  127. {
  128. int cbSource = strlen(szSource) + 1;
  129. int cbDest = strlen(szSource) + 1;
  130. int nLengthOldWord = strlen(szOldWord);
  131. int nLengthNewWord = strlen(szNewWord);
  132. char * szUpperSource = (char*)_alloca(cbSource);
  133. char * szUpperOldWord = (char*)_alloca(nLengthOldWord+1);
  134. strcpy(szUpperSource, szSource);
  135. strcpy(szUpperOldWord, szOldWord);
  136. _strupr(szUpperSource);
  137. _strupr(szUpperOldWord);
  138. memset(szDest, 0, cbDest);
  139. char * pszCurrent = szUpperSource;
  140. char * pszPrev = szUpperSource;
  141. int nSourceOffset = 0;
  142. int nDestOffset = 0;
  143. int cReplacements = 0;
  144. while(1)
  145. {
  146. pszCurrent = strstr(pszCurrent, szUpperOldWord);
  147. if (!pszCurrent) // szOldWord not found
  148. {
  149. // finish up and quit
  150. strcpy(szDest + nDestOffset, szSource + nSourceOffset);
  151. return cReplacements;
  152. }
  153. cReplacements++;
  154. int nMovement = pszCurrent-pszPrev;
  155. strncpy(szDest + nDestOffset, szSource + nSourceOffset, nMovement);
  156. strcat(szDest, szNewWord);
  157. nSourceOffset += nMovement + nLengthOldWord;
  158. nDestOffset += nMovement + nLengthNewWord;
  159. pszCurrent += nLengthOldWord;
  160. pszPrev = pszCurrent;
  161. }
  162. }
  163. #endif //dreamcast
  164. /*-------------------------------------------------------------------------
  165. * GetPathFromReg
  166. *-------------------------------------------------------------------------
  167. Purpose:
  168. Get a path out of the registry
  169. Parameters:
  170. Obvious. Notice that szPath must be of size MAX_PATH
  171. Returns:
  172. Return value from RegOpenKeyEx or RegQueryValueEx
  173. */
  174. LONG UTL::GetPathFromReg(IN HKEY hkey,
  175. IN const char * szSubKey,
  176. OUT char * szPath)
  177. {
  178. HKEY hKey;
  179. DWORD cb = MAX_PATH;
  180. DWORD dw = REG_SZ;
  181. assert(hkey && szSubKey);
  182. szPath[0] = 0;
  183. LONG ret = RegOpenKeyEx(hkey, szSubKey, 0, KEY_READ, &hKey);
  184. if (ERROR_SUCCESS == ret)
  185. {
  186. ret = RegQueryValueEx(hKey, "ArtPath", NULL, &dw, (unsigned char*)szPath, &cb);
  187. RegCloseKey(hKey);
  188. }
  189. return ret;
  190. }
  191. // converts char * of hex to int. Assumes uppercase for 'A' to 'F'.
  192. int UTL::hextoi(const char * pHex)
  193. {
  194. int n = 0;
  195. while(1)
  196. {
  197. if (*pHex >= '0' && *pHex <= '9')
  198. {
  199. n *= 16;
  200. n += *pHex - '0';
  201. }
  202. else
  203. if (*pHex >= 'A' && *pHex <= 'F')
  204. {
  205. n *= 16;
  206. n += 10 + *pHex - 'A';
  207. }
  208. else break;
  209. pHex++;
  210. }
  211. return n;
  212. }
  213. /*-------------------------------------------------------------------------
  214. * CompareFileVersion
  215. *-------------------------------------------------------------------------
  216. * Purpose:
  217. * Compare a file's version (not file's product version) with a given version
  218. *
  219. * Parameters:
  220. * hInstance of file, version elements
  221. *
  222. * Returns:
  223. * If file's version is less than supplied version: -1
  224. * If file's version is equal to supplied version: 0
  225. * If file's version is greater than supplied version: 1
  226. */
  227. /*
  228. int CompareFileVersion(HINSTANCE hInstance, WORD v1, WORD v2, WORD v3, WORD v4)
  229. {
  230. GetModuleFileName(hInstDPlay, szFullPath, sizeof(szFullPath));
  231. if (0 == (dwVerInfoSize = GetFileVersionInfoSize(szFullPath, &dwVerHnd)))
  232. goto Cleanup;
  233. if (NULL == (hMem = GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize)) ||
  234. NULL == (lpvMem = GlobalLock(hMem)))
  235. goto Cleanup;
  236. GetFileVersionInfo(szFullPath, dwVerHnd, dwVerInfoSize, lpvMem);
  237. if (!VerQueryValue(lpvMem, "\\", &lpBuffer, &dwBytes))
  238. goto Cleanup;
  239. lpvsFixedFileInfo = (VS_FIXEDFILEINFO*)lpBuffer;
  240. }
  241. */
  242. char* UTL::strdup(const char* s)
  243. {
  244. if (s)
  245. {
  246. char* r = new char [strlen(s) + 1];
  247. assert (r);
  248. strcpy(r, s);
  249. return r;
  250. }
  251. else
  252. return NULL;
  253. }
  254. void UTL::putName(char* name, const char* newVal)
  255. {
  256. assert (name);
  257. if (newVal)
  258. {
  259. strncpy(name, newVal, c_cbName - 1);
  260. name[c_cbName - 1] = '\0';
  261. }
  262. else
  263. name[0] = '\0';
  264. }
  265. void UTL::putFileName(char* fileName, const char* newVal)
  266. {
  267. assert (fileName);
  268. if (newVal)
  269. {
  270. strncpy(fileName, newVal, c_cbName - 1);
  271. fileName[c_cbName - 1] = '\0';
  272. }
  273. else
  274. fileName[0] = '\0';
  275. }
  276. /*
  277. ** Definitions for the List_utl class:
  278. */
  279. List_utl::List_utl(void)
  280. :
  281. m_first(NULL),
  282. m_last(NULL),
  283. m_n(0)
  284. {
  285. }
  286. void List_utl::first(Link_utl* l)
  287. {
  288. assert (l);
  289. l->unlink();
  290. l->m_txen = NULL;
  291. l->m_next = m_first;
  292. l->m_list = this;
  293. if (m_first)
  294. m_first->m_txen = l;
  295. else
  296. m_last = l;
  297. m_first = l;
  298. m_n++;
  299. }
  300. void List_utl::last(Link_utl* l)
  301. {
  302. assert (l);
  303. l->unlink();
  304. l->m_txen = m_last;
  305. l->m_next = NULL;
  306. l->m_list = this;
  307. if (m_last)
  308. m_last->m_next = l;
  309. else
  310. m_first = l;
  311. m_last = l;
  312. m_n++;
  313. }
  314. Link_utl* List_utl::operator [] (int index) const
  315. {
  316. //Get the offset handling the case where index is < 0.
  317. int n = (index >= 0) ? index : (m_n + index);
  318. Link_utl* link = NULL;
  319. if ((n >= 0) && (n < m_n))
  320. {
  321. if ((n << 1) < m_n)
  322. {
  323. /*
  324. ** Desired element is in the 1st half of the list, search from the front.
  325. */
  326. link = m_first;
  327. while (n--)
  328. link = link->next();
  329. }
  330. else
  331. {
  332. /*
  333. ** Desired element is in the last half of the list, search from the back.
  334. */
  335. link = m_last;
  336. while (++n < m_n)
  337. link = link->txen();
  338. }
  339. }
  340. return link;
  341. }
  342. /*
  343. ** Definitions for the Link_utl class:
  344. */
  345. Link_utl::Link_utl(void)
  346. :
  347. m_list(NULL),
  348. m_txen(NULL),
  349. m_next(NULL)
  350. {
  351. }
  352. bool Link_utl::unlink(void)
  353. {
  354. if (!m_list)
  355. return false;
  356. if (m_next)
  357. {
  358. m_next->m_txen = m_txen;
  359. }
  360. else
  361. {
  362. m_list->m_last = m_txen;
  363. }
  364. if (m_txen)
  365. {
  366. m_txen->m_next = m_next;
  367. }
  368. else
  369. {
  370. m_list->m_first = m_next;
  371. }
  372. m_list->m_n--;
  373. m_list = NULL;
  374. m_next = NULL;
  375. m_txen = NULL;
  376. return true;
  377. }
  378. bool Link_utl::next(Link_utl* l)
  379. {
  380. if ((!m_list) || (l == NULL) || (l == this))
  381. return false;
  382. l->unlink();
  383. l->m_list = m_list;
  384. l->m_txen = this;
  385. l->m_next = m_next;
  386. if (m_next)
  387. m_next->m_txen = l;
  388. else
  389. m_list->m_last = l;
  390. m_next = l;
  391. m_list->m_n++;
  392. return true;
  393. }
  394. bool Link_utl::txen(Link_utl* l)
  395. {
  396. if ((!m_list) || (l == NULL) || (l == this))
  397. return false;
  398. l->unlink();
  399. l->m_list = m_list;
  400. l->m_txen = m_txen;
  401. l->m_next = this;
  402. if (m_txen)
  403. m_txen->m_next = l;
  404. else
  405. m_list->m_first = l;
  406. m_txen = l;
  407. m_list->m_n++;
  408. return true;
  409. }
  410. #ifndef DREAMCAST
  411. Lock_utl::Lock_utl(void)
  412. :
  413. m_lockedS(CreateSemaphore(NULL, 1, 1, NULL)),
  414. m_locking_threadID(0),
  415. m_locks(0)
  416. {
  417. }
  418. Lock_utl::~Lock_utl(void)
  419. {
  420. CloseHandle(m_lockedS);
  421. }
  422. void Lock_utl::lock(void) const
  423. {
  424. Lock_utl* t = (Lock_utl*)this; //cast away constness
  425. DWORD threadID = GetCurrentThreadId();
  426. if (t->m_locking_threadID == threadID)
  427. {
  428. /*
  429. ** Just use the standard increment since this clause
  430. ** could only be executed if the list has been locked
  431. ** by this thread and this thread can only do one
  432. ** thing at a time.
  433. */
  434. t->m_locks++;
  435. }
  436. else
  437. {
  438. WaitForSingleObject(t->m_lockedS, INFINITE);
  439. t->m_locking_threadID = threadID;
  440. t->m_locks = 1;
  441. }
  442. }
  443. void Lock_utl::unlock(void) const
  444. {
  445. Lock_utl* t = (Lock_utl*)this; //cast away constness
  446. /*
  447. ** We only need the standard decrement here because unlock should
  448. ** only be called from the controlling thread & therefore we don't
  449. ** have to worry about other threads making simultaneous calls.
  450. */
  451. assert (GetCurrentThreadId() == t->m_locking_threadID); //NYI
  452. if (--(t->m_locks) == 0)
  453. {
  454. t->m_locking_threadID = 0;
  455. ReleaseSemaphore(t->m_lockedS, 1, NULL);
  456. }
  457. }
  458. #endif //dreamcast
  459. Rotation::Rotation(void)
  460. :
  461. m_angle(0.0f)
  462. {
  463. m_axis.x = m_axis.y = 0.0f;
  464. m_axis.z = -1.0;
  465. }
  466. Rotation::Rotation(const Rotation& r)
  467. :
  468. m_angle(r.m_angle)
  469. {
  470. m_axis = r.m_axis;
  471. }
  472. void Rotation::reset(void)
  473. {
  474. m_angle = m_axis.x = m_axis.y = 0.0f;
  475. m_axis.z = -1.0;
  476. }
  477. void Rotation::set(const Vector& axis,
  478. D3DVALUE angle)
  479. {
  480. m_axis = axis;
  481. m_angle = angle;
  482. }
  483. Rotation& Rotation::operator = (const Rotation& r)
  484. {
  485. m_axis = r.m_axis;
  486. m_angle = r.m_angle;
  487. return *this;
  488. }
  489. void Rotation::axis(const Vector& a)
  490. {
  491. m_axis = a;
  492. }
  493. void Rotation::angle(D3DVALUE a)
  494. {
  495. m_angle = a;
  496. }
  497. void Rotation::x(D3DVALUE t)
  498. {
  499. m_axis.x = t;
  500. }
  501. void Rotation::y(D3DVALUE t)
  502. {
  503. m_axis.y = t;
  504. }
  505. void Rotation::z(D3DVALUE t)
  506. {
  507. m_axis.z = t;
  508. }