Leaked source code of windows server 2003
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.

342 lines
6.9 KiB

  1. #include "headers.hxx"
  2. #include "global.hpp"
  3. #include "constants.hpp"
  4. #include "resourceDspecup.h"
  5. #include "AdsiHelpers.hpp"
  6. //////////////// ReadLine
  7. #define CHUNK_SIZE 100
  8. HRESULT
  9. ReadLine
  10. (
  11. HANDLE handle,
  12. String& text,
  13. bool *endLineFound_/*=NULL*/
  14. )
  15. {
  16. LOG_FUNCTION(ReadLine);
  17. ASSERT(handle != INVALID_HANDLE_VALUE);
  18. bool endLineFound=false;
  19. text.erase();
  20. // Acumulating chars read on text would cause the same
  21. // kind of reallocation and copy that text+=chunk will
  22. static wchar_t chunk[CHUNK_SIZE+1];
  23. HRESULT hr=S_OK;
  24. bool flagEof=false;
  25. do
  26. {
  27. LARGE_INTEGER pos;
  28. hr = WinGetVLFilePointer(handle,&pos);
  29. BREAK_ON_FAILED_HRESULT(hr);
  30. long nChunks=0;
  31. wchar_t *csr=NULL;
  32. while(!flagEof && !endLineFound)
  33. {
  34. DWORD bytesRead;
  35. hr = Win::ReadFile(
  36. handle,
  37. chunk,
  38. CHUNK_SIZE*sizeof(wchar_t),
  39. bytesRead,
  40. 0);
  41. if(hr==EOF_HRESULT)
  42. {
  43. flagEof=true;
  44. hr=S_OK;
  45. }
  46. BREAK_ON_FAILED_HRESULT(hr);
  47. if(bytesRead==0)
  48. {
  49. flagEof=true;
  50. }
  51. else
  52. {
  53. *(chunk+bytesRead/sizeof(wchar_t))=0;
  54. csr=wcschr(chunk,L'\n');
  55. if(csr!=NULL)
  56. {
  57. pos.QuadPart+= sizeof(wchar_t)*
  58. ((nChunks * CHUNK_SIZE) + (csr - chunk)+1);
  59. hr=Win::SetFilePointerEx(
  60. handle,
  61. pos,
  62. 0,
  63. FILE_BEGIN);
  64. BREAK_ON_FAILED_HRESULT(hr);
  65. *csr=0;
  66. endLineFound=true;
  67. }
  68. text+=chunk;
  69. nChunks++;
  70. }
  71. }
  72. BREAK_ON_FAILED_HRESULT(hr);
  73. //We know the length will fit in a long
  74. // and we want IA64 to build.
  75. long textLen=static_cast<long>(text.length());
  76. if(textLen!=0 && endLineFound && text[textLen-1]==L'\r')
  77. {
  78. text.erase(textLen-1,1);
  79. }
  80. if(endLineFound_ != NULL)
  81. {
  82. *endLineFound_=endLineFound;
  83. }
  84. if(flagEof)
  85. {
  86. hr=EOF_HRESULT;
  87. }
  88. } while(0);
  89. LOG_HRESULT(hr);
  90. return hr;
  91. }
  92. // Reads all the file to a string
  93. HRESULT
  94. ReadAllFile
  95. (
  96. const String &fileName,
  97. String &fileStr
  98. )
  99. {
  100. LOG_FUNCTION(ReadAllFile);
  101. HRESULT hr=S_OK;
  102. fileStr.erase();
  103. HANDLE file;
  104. hr=FS::CreateFile(fileName,
  105. file,
  106. GENERIC_READ);
  107. if(FAILED(hr))
  108. {
  109. error=String::format(IDS_COULD_NOT_CREATE_FILE,fileName.c_str());
  110. LOG_HRESULT(hr);
  111. return hr;
  112. }
  113. do
  114. {
  115. bool flagEof=false;
  116. while(!flagEof)
  117. {
  118. String line;
  119. hr=ReadLine(file,line);
  120. if(hr==EOF_HRESULT)
  121. {
  122. hr=S_OK;
  123. flagEof=true;
  124. }
  125. BREAK_ON_FAILED_HRESULT_ERROR(hr,fileName);
  126. fileStr+=line+L"\r\n";
  127. }
  128. BREAK_ON_FAILED_HRESULT_ERROR(hr,fileName);
  129. } while(0);
  130. if ( (fileStr.size() > 0) && (fileStr[0] == 0xfeff) )
  131. {
  132. fileStr.erase(0,1);
  133. }
  134. CloseHandle(file);
  135. LOG_HRESULT(hr);
  136. return hr;
  137. }
  138. HRESULT
  139. GetTempFileName
  140. (
  141. const wchar_t *lpPathName, // directory name
  142. const wchar_t *lpPrefixString, // file name prefix
  143. String &name // file name
  144. )
  145. {
  146. LOG_FUNCTION(GetTempFileName);
  147. ASSERT(FS::PathExists(lpPathName));
  148. HRESULT hr=S_OK;
  149. do
  150. {
  151. if (!FS::PathExists(lpPathName))
  152. {
  153. hr=E_FAIL;
  154. error=String::format(IDS_COULD_NOT_FIND_PATH,lpPathName);
  155. break;
  156. }
  157. DWORD result;
  158. wchar_t lpName[MAX_PATH]={0};
  159. result=::GetTempFileName(lpPathName,lpPrefixString,0,lpName);
  160. if (result == 0)
  161. {
  162. hr = Win::GetLastErrorAsHresult();
  163. error=String::format(IDS_COULD_NOT_GET_TEMP,lpPathName);
  164. break;
  165. }
  166. name=lpName;
  167. } while(0);
  168. LOG_HRESULT(hr);
  169. return hr;
  170. }
  171. // Retrieves a unique temporary file name
  172. HRESULT
  173. GetWorkTempFileName
  174. (
  175. const wchar_t *lpPrefixString,
  176. String &name
  177. )
  178. {
  179. LOG_FUNCTION(GetWorkTempFileName);
  180. HRESULT hr=S_OK;
  181. String path;
  182. do
  183. {
  184. hr=Win::GetTempPath(path);
  185. BREAK_ON_FAILED_HRESULT_ERROR(hr,String::format(IDS_NO_WORK_PATH));
  186. path=path.substr(0,path.size()-1);
  187. hr=GetTempFileName(path.c_str(),lpPrefixString,name);
  188. BREAK_ON_FAILED_HRESULT(hr);
  189. } while(0);
  190. LOG_HRESULT(hr);
  191. return hr;
  192. }
  193. // locate the file with the highest-numbered extension, then add 1 and
  194. // return the result.
  195. int
  196. DetermineNextFileNumber
  197. (
  198. const String& dir,
  199. const String& baseName,
  200. const wchar_t *extension
  201. )
  202. {
  203. LOG_FUNCTION(DetermineNextFileNumber);
  204. ASSERT(!dir.empty());
  205. ASSERT(!baseName.empty());
  206. int largest = 0;
  207. String filespec = dir + L"\\" + baseName + L".*."+ extension;
  208. WIN32_FIND_DATA findData;
  209. HANDLE ff = ::FindFirstFile(filespec.c_str(), &findData);
  210. if (ff != INVALID_HANDLE_VALUE)
  211. {
  212. for (;;)
  213. {
  214. String current = findData.cFileName;
  215. // grab the text between the dots: "nnn" in foo.nnn.ext
  216. // first dot
  217. size_t pos = current.find(L".");
  218. if (pos == String::npos)
  219. {
  220. continue;
  221. }
  222. String foundExtension = current.substr(pos + 1);
  223. // second dot
  224. pos = foundExtension.find(L".");
  225. if (pos == String::npos)
  226. {
  227. continue;
  228. }
  229. foundExtension = foundExtension.substr(0, pos);
  230. int i = 0;
  231. foundExtension.convert(i);
  232. largest = max(i, largest);
  233. if (!::FindNextFile(ff, &findData))
  234. {
  235. BOOL success = ::FindClose(ff);
  236. ASSERT(success);
  237. break;
  238. }
  239. }
  240. }
  241. // roll over after 255
  242. return (++largest & 0xFF);
  243. }
  244. // Retrieves a unique file name
  245. void
  246. GetWorkFileName
  247. (
  248. const String& dir,
  249. const String& baseName,
  250. const wchar_t *extension,
  251. String &fileName
  252. )
  253. {
  254. LOG_FUNCTION(GetFileName);
  255. int logNumber = DetermineNextFileNumber(dir,baseName,extension);
  256. fileName = dir
  257. + L"\\"
  258. + baseName
  259. + String::format(L".%1!03d!.", logNumber)
  260. + extension;
  261. if (::GetFileAttributes(fileName.c_str()) != 0xFFFFFFFF)
  262. {
  263. // could exist, as the file numbers roll over
  264. BOOL success = ::DeleteFile(fileName.c_str());
  265. ASSERT(success);
  266. }
  267. }