Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

515 lines
8.3 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. string.c
  5. Abstract:
  6. This file implements string functions for fax.
  7. Author:
  8. Wesley Witt (wesw) 23-Jan-1995
  9. Environment:
  10. User Mode
  11. --*/
  12. #include <windows.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <tchar.h>
  16. #include "faxutil.h"
  17. LPTSTR
  18. StringDup(
  19. LPCTSTR String
  20. )
  21. {
  22. LPTSTR NewString;
  23. if (!String) {
  24. return NULL;
  25. }
  26. NewString = (LPTSTR) MemAlloc( (_tcslen( String ) + 1) * sizeof(TCHAR) );
  27. if (!NewString) {
  28. return NULL;
  29. }
  30. _tcscpy( NewString, String );
  31. return NewString;
  32. }
  33. VOID
  34. FreeString(
  35. LPVOID String
  36. )
  37. {
  38. MemFree( String );
  39. }
  40. LPWSTR
  41. AnsiStringToUnicodeString(
  42. LPCSTR AnsiString
  43. )
  44. {
  45. DWORD Count;
  46. LPWSTR UnicodeString;
  47. //
  48. // first see how big the buffer needs to be
  49. //
  50. Count = MultiByteToWideChar(
  51. CP_ACP,
  52. MB_PRECOMPOSED,
  53. AnsiString,
  54. -1,
  55. NULL,
  56. 0
  57. );
  58. //
  59. // i guess the input string is empty
  60. //
  61. if (!Count) {
  62. return NULL;
  63. }
  64. //
  65. // allocate a buffer for the unicode string
  66. //
  67. Count += 1;
  68. UnicodeString = (LPWSTR) MemAlloc( Count * sizeof(UNICODE_NULL) );
  69. if (!UnicodeString) {
  70. return NULL;
  71. }
  72. //
  73. // convert the string
  74. //
  75. Count = MultiByteToWideChar(
  76. CP_ACP,
  77. MB_PRECOMPOSED,
  78. AnsiString,
  79. -1,
  80. UnicodeString,
  81. Count
  82. );
  83. //
  84. // the conversion failed
  85. //
  86. if (!Count) {
  87. MemFree( UnicodeString );
  88. return NULL;
  89. }
  90. return UnicodeString;
  91. }
  92. LPSTR
  93. UnicodeStringToAnsiString(
  94. LPCWSTR UnicodeString
  95. )
  96. {
  97. DWORD Count;
  98. LPSTR AnsiString;
  99. //
  100. // first see how big the buffer needs to be
  101. //
  102. Count = WideCharToMultiByte(
  103. CP_ACP,
  104. 0,
  105. UnicodeString,
  106. -1,
  107. NULL,
  108. 0,
  109. NULL,
  110. NULL
  111. );
  112. //
  113. // i guess the input string is empty
  114. //
  115. if (!Count) {
  116. return NULL;
  117. }
  118. //
  119. // allocate a buffer for the unicode string
  120. //
  121. Count += 1;
  122. AnsiString = (LPSTR) MemAlloc( Count );
  123. if (!AnsiString) {
  124. return NULL;
  125. }
  126. //
  127. // convert the string
  128. //
  129. Count = WideCharToMultiByte(
  130. CP_ACP,
  131. 0,
  132. UnicodeString,
  133. -1,
  134. AnsiString,
  135. Count,
  136. NULL,
  137. NULL
  138. );
  139. //
  140. // the conversion failed
  141. //
  142. if (!Count) {
  143. MemFree( AnsiString );
  144. return NULL;
  145. }
  146. return AnsiString;
  147. }
  148. VOID
  149. MakeDirectory(
  150. LPCTSTR Dir
  151. )
  152. /*++
  153. Routine Description:
  154. Attempt to create all of the directories in the given path.
  155. Arguments:
  156. Dir - Directory path to create
  157. Return Value:
  158. TRUE for success, FALSE on error
  159. --*/
  160. {
  161. LPTSTR p, NewDir;
  162. NewDir = p = ExpandEnvironmentString( Dir );
  163. __try {
  164. if (*p != '\\') p += 2;
  165. while( *++p ) {
  166. while(*p && *p != TEXT('\\')) p++;
  167. if (!*p) {
  168. CreateDirectory( NewDir, NULL );
  169. return;
  170. }
  171. *p = 0;
  172. CreateDirectory( NewDir, NULL );
  173. *p = TEXT('\\');
  174. }
  175. } __except (EXCEPTION_EXECUTE_HANDLER) {
  176. }
  177. MemFree( NewDir );
  178. }
  179. VOID
  180. HideDirectory(
  181. LPTSTR Dir
  182. )
  183. /*++
  184. Routine Description:
  185. Hide the specified directory
  186. Arguments:
  187. Dir - Directory path to hide
  188. Return Value:
  189. none.
  190. --*/
  191. {
  192. DWORD attrib;
  193. //
  194. // make sure it exists
  195. //
  196. if (!Dir) {
  197. return;
  198. }
  199. MakeDirectory( Dir );
  200. attrib = GetFileAttributes(Dir);
  201. if (attrib == 0xFFFFFFFF) {
  202. return;
  203. }
  204. attrib |= FILE_ATTRIBUTE_HIDDEN;
  205. SetFileAttributes( Dir, attrib );
  206. return;
  207. }
  208. VOID
  209. DeleteDirectory(
  210. LPTSTR Dir
  211. )
  212. /*++
  213. Routine Description:
  214. Attempt to create all of the directories in the given path.
  215. Arguments:
  216. Dir - Directory path to create
  217. Return Value:
  218. TRUE for success, FALSE on error
  219. --*/
  220. {
  221. LPTSTR p;
  222. __try {
  223. while (TRUE) {
  224. if (!RemoveDirectory( Dir )) {
  225. return;
  226. }
  227. p = Dir + _tcslen( Dir ) - 1;
  228. while (*p != TEXT('\\') && p != Dir) p--;
  229. if (p == Dir) return;
  230. *p = 0;
  231. }
  232. } __except (EXCEPTION_EXECUTE_HANDLER) {
  233. }
  234. }
  235. int
  236. FormatElapsedTimeStr(
  237. FILETIME *ElapsedTime,
  238. LPTSTR TimeStr,
  239. DWORD StringSize
  240. )
  241. /*++
  242. Routine Description:
  243. Convert ElapsedTime to a string.
  244. Arguments:
  245. ElaspedTime - the elapsed time
  246. TimeStr - buffer to store the string into
  247. StringSize - size of the buffer in bytes
  248. Return Value:
  249. The return value of GetTimeFormat()
  250. --*/
  251. {
  252. SYSTEMTIME SystemTime;
  253. FileTimeToSystemTime( ElapsedTime, &SystemTime );
  254. return GetTimeFormat(
  255. LOCALE_SYSTEM_DEFAULT,
  256. LOCALE_NOUSEROVERRIDE | TIME_FORCE24HOURFORMAT | TIME_NOTIMEMARKER,
  257. &SystemTime,
  258. NULL,
  259. TimeStr,
  260. StringSize
  261. );
  262. }
  263. LPTSTR
  264. ExpandEnvironmentString(
  265. LPCTSTR EnvString
  266. )
  267. {
  268. DWORD Size;
  269. LPTSTR String;
  270. Size = ExpandEnvironmentStrings( EnvString, NULL, 0 );
  271. if (Size == 0) {
  272. return NULL;
  273. }
  274. Size += 1;
  275. String = (LPTSTR) MemAlloc( Size * sizeof(TCHAR) );
  276. if (String == NULL) {
  277. return NULL;
  278. }
  279. if (ExpandEnvironmentStrings( EnvString, String, Size ) == 0) {
  280. MemFree( String );
  281. return NULL;
  282. }
  283. return String;
  284. }
  285. LPTSTR
  286. GetEnvVariable(
  287. LPCTSTR EnvString
  288. )
  289. {
  290. DWORD Size;
  291. LPTSTR EnvVar;
  292. Size = GetEnvironmentVariable( EnvString, NULL, 0 );
  293. if (!Size) {
  294. return NULL;
  295. }
  296. EnvVar = (LPTSTR) MemAlloc( Size * sizeof(TCHAR) );
  297. if (EnvVar == NULL) {
  298. return NULL;
  299. }
  300. Size = GetEnvironmentVariable( EnvString, EnvVar, Size );
  301. if (!Size) {
  302. MemFree( EnvVar );
  303. return NULL;
  304. }
  305. return EnvVar;
  306. }
  307. LPTSTR
  308. ConcatenatePaths(
  309. LPTSTR BasePath,
  310. LPCTSTR AppendPath
  311. )
  312. {
  313. DWORD len;
  314. len = _tcslen(BasePath);
  315. if (BasePath[len-1] != (TCHAR) TEXT('\\')) {
  316. _tcscat(BasePath, TEXT("\\") );
  317. }
  318. _tcscat(BasePath, AppendPath);
  319. return BasePath;
  320. }
  321. int MyLoadString(
  322. HINSTANCE hInstance,
  323. UINT uID,
  324. LPTSTR lpBuffer,
  325. int nBufferMax,
  326. LANGID LangID
  327. )
  328. {
  329. HRSRC hFindRes; // Handle from FindResourceEx
  330. HANDLE hLoadRes; // Handle from LoadResource
  331. LPWSTR pSearch; // Pointer to search for correct string
  332. int cch = 0; // Count of characters
  333. #ifndef UNICODE
  334. LPWSTR pString; // Pointer to temporary string
  335. #endif
  336. //
  337. // String Tables are broken up into segments of 16 strings each. Find the segment containing the string we want.
  338. //
  339. if ((!(hFindRes = FindResourceEx(hInstance, RT_STRING, (LPTSTR) ((LONG) (((USHORT) uID >> 4) + 1)), (WORD) LangID)))) {
  340. //
  341. // Could not find resource. Return 0.
  342. //
  343. return (cch);
  344. }
  345. //
  346. // Load the resource.
  347. //
  348. hLoadRes = LoadResource(hInstance, hFindRes);
  349. //
  350. // Lock the resource.
  351. //
  352. if (pSearch = (LPWSTR) LockResource(hLoadRes)) {
  353. //
  354. // Move past the other strings in this segment. (16 strings in a segment -> & 0x0F)
  355. //
  356. uID &= 0x0F;
  357. //
  358. // Find the correct string in this segment.
  359. //
  360. while (TRUE) {
  361. cch = *((WORD *) pSearch++);
  362. if (uID-- == 0) {
  363. break;
  364. }
  365. pSearch += cch;
  366. }
  367. //
  368. // Store the found pointer in the given pointer.
  369. //
  370. if (nBufferMax < cch) {
  371. SetLastError(ERROR_INSUFFICIENT_BUFFER);
  372. return 0;
  373. }
  374. #ifndef UNICODE
  375. pString = MemAlloc(sizeof(WCHAR) * nBufferMax);
  376. ZeroMemory(pString, sizeof(WCHAR) * nBufferMax);
  377. CopyMemory(pString, pSearch, sizeof(WCHAR) * cch);
  378. WideCharToMultiByte(CP_THREAD_ACP, 0, pString, -1, lpBuffer, (cch + 1), NULL, NULL);
  379. MemFree(pString);
  380. #else
  381. ZeroMemory(lpBuffer, sizeof(WCHAR) * nBufferMax);
  382. CopyMemory(lpBuffer, pSearch, sizeof(WCHAR) * cch);
  383. #endif
  384. }
  385. //
  386. // Return the number of characters in the string.
  387. //
  388. return (cch);
  389. }