Utils.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // 2002.11.23
  2. // copy from kdphoto projects
  3. #include "stdafx.h"
  4. int GetChar( char ** str )
  5. {
  6. int code = **((unsigned char **)str);
  7. if ( !str ) return 0;
  8. if ( !*str ) return 0;
  9. if ( !code ) return 0;
  10. (*str) ++;
  11. if ( code > 0x80 )
  12. {
  13. code *= 256;
  14. code += **((unsigned char **)str);
  15. (*str) ++;
  16. }
  17. return code;
  18. };
  19. int SetChar( char **str, int chr )
  20. {
  21. int byte = 1;
  22. if ( !str ) return 0;
  23. if ( !*str ) return 0;
  24. if ( chr < 256 )
  25. {
  26. **((unsigned char **)str) = chr % 256;
  27. (*str) ++;
  28. }
  29. else
  30. {
  31. **((unsigned char **)str) = chr / 256;
  32. (*str) ++;
  33. **((unsigned char **)str) = chr % 256;
  34. (*str) ++;
  35. byte ++;
  36. }
  37. return byte;
  38. }
  39. int PeekChar( char **str )
  40. {
  41. int code = **((unsigned char **)str);
  42. if ( !str ) return 0;
  43. if ( !*str ) return 0;
  44. if ( !code ) return 0;
  45. if ( code > 0x80 )
  46. {
  47. code *= 256;
  48. code += *((unsigned char *)(*str + 1));
  49. }
  50. return code;
  51. }
  52. int CatChar( char *str, int cc)
  53. {
  54. int len = 0;
  55. while( *str )
  56. {
  57. GetChar( &str );
  58. len++;
  59. }
  60. SetChar( &str, cc );
  61. *str = 0;
  62. return (++len);
  63. }
  64. char *whitespaces = " \t\n";
  65. ///去掉空格的函数
  66. void trim( char *buf, char *result )
  67. {
  68. char * p;
  69. if ( !buf || !result )
  70. return;
  71. p = strlen( buf ) + buf;
  72. while ( (*buf) && strchr( whitespaces, *buf) ) buf++;
  73. while( (p >= buf) && strchr( whitespaces, *p) ) p--;
  74. while ( buf <= p ) *result++ = *buf++;
  75. *result = 0;
  76. }
  77. char *next_token( char *buf, char *token, char *stopchars )
  78. {
  79. if ( !stopchars )
  80. stopchars = whitespaces;
  81. int cc;
  82. char *p = token;
  83. while ( (cc = GetChar(&buf)) && (!strchr( stopchars, cc)) )
  84. SetChar( &token, cc);
  85. SetChar( &token, 0);
  86. trim( p, p );
  87. return buf;
  88. }
  89. CString GetPathName( const char * filename )
  90. {
  91. /*
  92. CString path = filename;
  93. char *file = path.GetBuffer(512);
  94. char *p = file + strlen( file );
  95. for ( ; *p != '\\' && *p != '/' && p > file; p-- );
  96. if ( *p == '\\' || *p == '/') p++;
  97. *p = 0;
  98. path.ReleaseBuffer();
  99. */
  100. char path[MAX_PATH];
  101. strcpy( path, filename );
  102. char *p = path + strlen( path );
  103. for ( ; *p != '\\' && *p != '/' && p > path; p-- );
  104. if ( *p == '\\' || *p == '/') p++;
  105. *p = 0;
  106. return CString(path);
  107. }
  108. ///获取文件扩展名
  109. const char * GetExt( const char * filename )
  110. {
  111. const char *p = strlen(filename) + filename;
  112. while( p > filename && *p != '.' ) p--;
  113. if ( *p != '.' )
  114. return "\0";
  115. else
  116. return p;
  117. }
  118. ///获取文件扩展名
  119. CString GetFileName( const char * filename, int ext = 0)
  120. {
  121. /*
  122. CString path = filename;
  123. char *file = path.GetBuffer(512);
  124. char *p = file + strlen( file );
  125. for ( ; *p != '\\' && *p != '/' && p > file; p-- );
  126. if ( *p == '\\' || *p == '/') p++;
  127. if ( !ext )
  128. {
  129. char *p1 = p;
  130. while( *p1 && *p1 != '.' ) p1++;
  131. *p1 = 0;
  132. }
  133. path.ReleaseBuffer();
  134. return p;
  135. */
  136. char path[MAX_PATH];
  137. strcpy( path, filename );
  138. char *p = path + strlen(path);
  139. for ( ; *p != '\\' && *p != '/' && p > path; p-- );
  140. if ( *p == '\\' || *p == '/') p++;
  141. if ( !ext )
  142. {
  143. char *p1 = p;
  144. while( *p1 && *p1 != '.' ) p1++;
  145. *p1 = 0;
  146. }
  147. return CString(p);
  148. }
  149. typedef HRESULT (_stdcall *RegFunc)(void);
  150. BOOL RegisterControl( const char * file, int reg )
  151. {
  152. HMODULE hmod = LoadLibrary( file );
  153. if ( !hmod )
  154. return FALSE;
  155. RegFunc func;
  156. if ( reg )
  157. func = (RegFunc)GetProcAddress( hmod, "DllRegisterServer" );
  158. else
  159. func = (RegFunc)GetProcAddress( hmod, "DllUnregisterServer" );
  160. if ( func )
  161. {
  162. HRESULT hr = func();
  163. FreeLibrary( hmod );
  164. return S_OK == hr;
  165. }
  166. FreeLibrary( hmod );
  167. return FALSE;
  168. }
  169. //0:win9x
  170. //1:winnt
  171. //2:win2000/winxp
  172. //3:win32s
  173. int GetWindowVersion()
  174. {
  175. DWORD ret;
  176. DWORD dwVersion = GetVersion();
  177. // Get the Windows version.
  178. DWORD dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
  179. DWORD dwWindowsMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion)));
  180. // Get the build number.
  181. DWORD dwBuild;
  182. if (dwVersion < 0x80000000) // Windows NT/2000/XP
  183. {
  184. dwBuild = (DWORD)(HIWORD(dwVersion));
  185. if ( dwBuild == 5 )
  186. ret = 2;
  187. else
  188. ret = 1;
  189. }
  190. else if (dwWindowsMajorVersion < 4) // Win32s
  191. {
  192. dwBuild = (DWORD)(HIWORD(dwVersion) & ~0x8000);
  193. ret = 3;
  194. }
  195. else // Windows 95/98/Me
  196. {
  197. dwBuild = 0;
  198. ret = 0;
  199. }
  200. return ret;
  201. }
  202. CString GetSystemPath()
  203. {
  204. CString systempath;
  205. ::GetWindowsDirectory(systempath.GetBuffer(MAX_PATH), MAX_PATH);
  206. systempath.ReleaseBuffer();
  207. BOOL bWin9x = ( GetWindowVersion() == 0 );
  208. if (systempath.Right(1) != _T("\\"))
  209. {
  210. //is win9x
  211. if ( bWin9x )
  212. systempath += _T("\\System\\");
  213. else
  214. systempath += _T("\\System32\\");
  215. }
  216. return systempath;
  217. }
  218. int debug_printf(char *fmt, ...);
  219. //be careful!
  220. void RecursiveDelete(CString szPath)
  221. {
  222. CFileFind ff;
  223. CString path = szPath;
  224. if(path.Right(1) != "\\")
  225. path += "\\";
  226. path += "*.*";
  227. BOOL res = ff.FindFile(path);
  228. ///debug_printf("delete dir!:%s\n", (LPCSTR)szPath);
  229. while(res)
  230. {
  231. res = ff.FindNextFile();
  232. if (!ff.IsDots() && !ff.IsDirectory())
  233. {
  234. DeleteFile(ff.GetFilePath());
  235. }
  236. else if (ff.IsDirectory() && !ff.IsDots() )
  237. {
  238. path = ff.GetFilePath();
  239. RecursiveDelete(path);
  240. RemoveDirectory(path);
  241. }
  242. }
  243. }