00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007 #include "vmsFileUtil.h"
00008 #include <shlobj.h>
00009
00010 void vmsFileUtil::WriteString(vmsFile &file, LPCSTR psz)
00011 {
00012 int l = lstrlen (psz);
00013 file.Write (&l, sizeof (l));
00014 file.Write (psz, l);
00015 }
00016
00017 void vmsFileUtil::ReadString(vmsFile &file, fsString &str)
00018 {
00019 int l = 0;
00020 file.Read (&l, sizeof (l));
00021
00022 if (l > 100 * 1024 * 1024)
00023 throw ERROR_BAD_FORMAT;
00024
00025 char* psz = new char [l+1];
00026 try {
00027 file.Read (psz, l);
00028 psz [l] = 0;
00029 } catch (DWORD dw) {
00030 delete [] psz;
00031 throw dw;
00032 }
00033
00034 str = psz;
00035 delete [] psz;
00036 }
00037
00038 void vmsFileUtil::WriteHeader(vmsFile& file, LPCSTR pszDesc, WORD wVersion)
00039 {
00040 WriteString (file, pszDesc);
00041 file.Write (&wVersion, sizeof (WORD));
00042 }
00043
00044 void vmsFileUtil::ReadHeader(vmsFile& file, fsString& strDesc, WORD &wVersion)
00045 {
00046 ReadString (file, strDesc);
00047 file.Read (&wVersion, sizeof (WORD));
00048 }
00049
00050 void vmsFileUtil::MakePathOK(LPSTR szPath, bool bNeedBackslashAtEnd)
00051 {
00052 LPSTR psz = szPath;
00053 while (*psz)
00054 {
00055 if (*psz == '/')
00056 *psz = '\\';
00057 psz++;
00058 }
00059
00060 if (bNeedBackslashAtEnd && psz [-1] != '\\')
00061 {
00062 psz [0] = '\\';
00063 psz [1] = 0;
00064 }
00065 }
00066
00067 void vmsFileUtil::GetAppDataPath(LPCSTR pszAppName, LPSTR szPath)
00068 {
00069 LPITEMIDLIST pidl = NULL;
00070 SHGetSpecialFolderLocation (NULL, CSIDL_APPDATA, &pidl);
00071 SHGetPathFromIDList (pidl, szPath);
00072
00073 vmsFileUtil::MakePathOK (szPath);
00074 lstrcat (szPath, pszAppName);
00075 lstrcat (szPath, "\\");
00076 }
00077
00078 void vmsFileUtil::BuildPathToFile(LPCSTR pszPathName)
00079 {
00080 BuildPath (GetPathFromPathName (pszPathName));
00081 }
00082
00083 fsString vmsFileUtil::GetPathFromPathName(LPCSTR pszPathName)
00084 {
00085 char szPath [MY_MAX_PATH];
00086 lstrcpy (szPath, pszPathName);
00087 MakePathOK (szPath, false);
00088 LPSTR psz = strrchr (szPath, '\\');
00089 if (psz == NULL)
00090 return "";
00091 *psz = 0;
00092 return szPath;
00093 }
00094
00095 void vmsFileUtil::BuildPath(LPCSTR pszPath)
00096 {
00097 char szPath [MY_MAX_PATH];
00098 lstrcpy (szPath, pszPath);
00099 MakePathOK (szPath, true);
00100 LPSTR psz = szPath;
00101 if (psz [1] == ':')
00102 psz += 3;
00103
00104 while (*psz)
00105 {
00106 char szPathNow [MY_MAX_PATH];
00107 psz = strchr (psz, '\\') + 1;
00108 lstrcpyn (szPathNow, szPath, psz - szPath);
00109 if (FALSE == CreateDirectory (szPathNow, NULL) &&
00110 ERROR_ALREADY_EXISTS != GetLastError ())
00111 throw GetLastError ();
00112 }
00113 }