#include "coretools.h"
#include "coretypes.h"
-static String MyGetSysError(int nerr);
-static BOOL MyCreateDirectoryIfNeeded(LPCTSTR lpPathName, String * perrstr);
-
-BOOL GetFileTimes(LPCTSTR szFilename,
- LPSYSTEMTIME pMod,
- LPSYSTEMTIME pCreate /*=NULL*/,
- LPSYSTEMTIME pAccess /*=NULL*/)
-{
- WIN32_FIND_DATA ffi;
- assert(szFilename != NULL);
- assert(pMod != NULL);
-
- HANDLE hff = FindFirstFile(szFilename, &ffi);
- if (hff != INVALID_HANDLE_VALUE)
- {
- FILETIME ft;
- FileTimeToLocalFileTime(&ffi.ftLastWriteTime, &ft);
- FileTimeToSystemTime(&ft, pMod);
- if (pCreate)
- {
- FileTimeToLocalFileTime(&ffi.ftCreationTime, &ft);
- FileTimeToSystemTime(&ft, pCreate);
- }
- if (pAccess)
- {
- FileTimeToLocalFileTime(&ffi.ftLastAccessTime, &ft);
- FileTimeToSystemTime(&ft, pAccess);
- }
- FindClose(hff);
- return TRUE;
- }
- return FALSE;
-}
-
-time_t GetFileModTime(LPCTSTR szPath)
-{
- if (!szPath || !szPath[0]) return 0;
- struct _stat mystats = {0};
- int stat_result = _tstat(szPath, &mystats);
- if (stat_result!=0)
- return 0;
- return mystats.st_mtime;
-}
-
-DWORD GetFileSizeEx(LPCTSTR szFilename)
-{
- WIN32_FIND_DATA ffi;
- assert(szFilename != NULL);
-
- HANDLE hff = FindFirstFile(szFilename, &ffi);
- if (hff != INVALID_HANDLE_VALUE)
- {
- FindClose(hff);
- return ffi.nFileSizeLow;
- }
- return 0L;
-}
-
-// Doesn't _tcsclen do this ?
-int tcssubptr(LPCTSTR start, LPCTSTR end)
-{
- LPCTSTR p = start;
- register int cnt=0;
- while (p <= end)
- {
- cnt++;
- p = _tcsinc(p);
- }
- return cnt;
-}
-
size_t linelen(const char *string, size_t maxlen)
{
size_t stringlen = 0;
return stringlen;
}
-void GetLocalDrives(LPTSTR letters)
-{
- TCHAR temp[100];
-
- _tccpy(letters,_T("\0"));
- if (GetLogicalDriveStrings(100,temp))
- {
- TCHAR *p,*pout=letters;
- for (p=temp; *p != _T('\0'); )
- {
- if (GetDriveType(p)!=DRIVE_REMOTE)
- {
- _tccpy(pout,p);
- pout = _tcsinc(pout);
- }
- p = _tcsninc(p,_tcslen(p)+1);
- }
- _tccpy(pout,_T("\0"));
- }
-}
-
-DWORD FPRINTF(HANDLE hf, LPCTSTR fmt, ... )
-{
- static TCHAR fprintf_buffer[8192];
- va_list vl;
- va_start( vl, fmt );
-
- _vsntprintf(fprintf_buffer, countof(fprintf_buffer), fmt, vl);
- DWORD dwWritten;
- WriteFile(hf, fprintf_buffer, _tcslen(fprintf_buffer)*sizeof(TCHAR), &dwWritten, NULL);
-
- va_end( vl );
- return dwWritten;
-}
-
-DWORD FPUTS(LPCTSTR s, HANDLE hf)
-{
- static DWORD fputs_written;
- WriteFile(hf, s, _tcslen(s)*sizeof(TCHAR), &fputs_written, NULL);
- return fputs_written;
-}
-
-
-HANDLE FOPEN(LPCTSTR path, DWORD mode /*= GENERIC_READ*/, DWORD access /*= OPEN_EXISTING*/)
-{
- HANDLE hf = CreateFile(path,
- mode,
- 0,
- NULL,
- access,
- FILE_ATTRIBUTE_NORMAL,
- NULL);
- if (hf == INVALID_HANDLE_VALUE) // give it another shot
- return CreateFile(path,
- mode,
- 0,
- NULL,
- access,
- FILE_ATTRIBUTE_NORMAL,
- NULL);
- return hf;
-}
-
void replace_char(LPTSTR s, int target, int repl)
{
TCHAR *p;
*p = (TCHAR)repl;
}
-BOOL FileExtMatches(LPCTSTR filename, LPCTSTR ext)
-{
- LPCTSTR p;
-
- /* if ext is empty, it is considered a no-match */
- if (*ext == _T('\0'))
- return FALSE;
-
- p = filename;
- p = _tcsninc(p, _tcslen(filename) - _tcslen(ext));
- if (p >= filename)
- return (!_tcsicmp(p,ext));
-
- return FALSE;
-}
-
/**
* @brief Return true if *pszChar is a slash (either direction) or a colon
*
SplitFilename(sViewName.c_str(), path, name, ext);
}
}
-#ifdef _DEBUG
-// Test code for SplitFilename above
-void TestSplitFilename()
-{
- LPCTSTR tests[] = {
- _T("\\\\hi\\"), _T("\\\\hi"), 0, 0
- , _T("\\\\hi\\a.a"), _T("\\\\hi"), _T("a"), _T("a")
- , _T("a.hi"), 0, _T("a"), _T("hi")
- , _T("a.b.hi"), 0, _T("a.b"), _T("hi")
- , _T("c:"), _T("c:"), 0, 0
- , _T("c:\\"), _T("c:"), 0, 0
- , _T("c:\\d:"), _T("c:\\d:"), 0, 0
- };
- for (int i=0; i < countof(tests); i += 4)
- {
- LPCTSTR dir = tests[i];
- String path, name, ext;
- SplitFilename(dir, &path, &name, &ext);
- LPCTSTR szpath = tests[i+1] ? tests[i+1] : _T("");
- LPCTSTR szname = tests[i+2] ? tests[i+2] : _T("");
- LPCTSTR szext = tests[i+3] ? tests[i+3] : _T("");
- assert(path == szpath);
- assert(name == szname);
- assert(ext == szext);
- }
-}
-#endif
-
-void AddExtension(LPTSTR name, LPCTSTR ext)
-{
- TCHAR *p;
-
- assert(ext[0] != _T('.'));
- if (!((p=_tcsrchr(name,_T('.'))) != NULL
- && !_tcsicmp(p+1,ext)))
- {
- _tcscat(name,_T("."));
- _tcscat(name,ext);
- }
-}
-
-int fcmp(float a,float b)
-/* return -1 if a<b, 0 if a=b, or 1 if a>b */
-{
- long la,lb;
-
- la = (long)(a * 10000.0);
- lb = (long)(b * 10000.0);
-
- if (la < lb)
- return -1;
-
- return (la > lb);
-}
-
-BOOL FindAnyFile(LPTSTR filespec, LPTSTR name)
-{
-// Use 64-bit versions with VS2003.Net and later
-#if _MSC_VER >= 1300
- _tfinddata64_t c_file;
- intptr_t hFile;
- hFile = _tfindfirst64( filespec, &c_file );
-#else
-// Use 32-bit versions with VC6
- _tfinddata_t c_file;
- long hFile;
- hFile = _tfindfirst( filespec, &c_file );
-#endif // _MSC_VER >= 1300
-
- if (hFile == -1L)
- return FALSE;
-
- _tcscpy(name, c_file.name);
- _findclose( hFile );
- return TRUE;
-}
-
-
-long SwapEndian(long val)
-{
-#ifndef _WINDOWS
- long t = 0x00000000;
- t = ((val >> 24) & 0x000000FF);
- t |= ((val >> 8) & 0x0000FF00);
- t |= ((val << 8) & 0x00FF0000);
- t |= ((val << 24) & 0xFF000000);
- return (long)t;
-#else
- return val;
-#endif
-}
-
-
-
-short int SwapEndian(short int val)
-{
-#ifndef _WINDOWS
- short int t = 0x0000;
- t = (val & 0xFF00) >> 8;
- t |= (val & 0x00FF) << 8;
- return (short int)t;
-#else
- return val;
-#endif
-}
-
-// Get user language description of error, if available
-static String MyGetSysError(int nerr)
-{
- LPTSTR lpMsgBuf = NULL;
- String str(_T("?"));
- if (FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- nerr,
- 0, // Default language
- (LPTSTR) &lpMsgBuf,
- 0,
- NULL
- ))
- {
- str = lpMsgBuf;
- // Free the buffer.
- LocalFree( lpMsgBuf );
- }
- return str;
-}
-
-// Create directory (via Win32 API)
-// if success, or already exists, return TRUE
-// if failure, return system error string
-// (NB: Win32 CreateDirectory reports failure if already exists)
-// TODO: replace this with calls to paths_CreateIfNeeded()
-static BOOL MyCreateDirectoryIfNeeded(LPCTSTR lpPathName, String * perrstr)
-{
- LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL;
- int rtn = CreateDirectory(lpPathName, lpSecurityAttributes);
- if (!rtn)
- {
- DWORD errnum = GetLastError();
- // Consider it success if directory already exists
- if (errnum == ERROR_ALREADY_EXISTS)
- return TRUE;
- if (perrstr)
- {
- String errdesc = MyGetSysError(errnum);
- TCHAR tmp[10];
- *perrstr = _itot(errnum, tmp, 10);
- *perrstr += _T(": ");
- *perrstr += errdesc;
- }
- }
- return rtn;
-}
-
-float RoundMeasure(float measure, float units)
-{
- float res1,res2,divisor;
- if (units == 25.4f)
- divisor = 0.03937f;
- else if (units == 6.f || units == 72.f)
- divisor = 0.0139f;
- else // 128ths
- divisor = 0.0078125f;
-
- res1 = (float)fmod(measure,divisor);
- res2 = divisor - res1;
- if (res1 > res2)
- return (measure-res1);
-
- return (measure-res1+divisor);
-}
-
-
-BOOL HaveAdminAccess()
-{
- // make sure this is NT first
- OSVERSIONINFO ver = { 0 };
- ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
-
- // if this fails, we want to default to being enabled
- if (!(GetVersionEx(&ver) && ver.dwPlatformId==VER_PLATFORM_WIN32_NT))
- {
- return TRUE;
- }
-
- // now check with the security manager
- HANDLE hHandleToken;
-
- if(!::OpenProcessToken(::GetCurrentProcess(), TOKEN_READ, &hHandleToken))
- return FALSE;
-
- UCHAR TokenInformation[1024];
- DWORD dwTokenInformationSize;
-
- BOOL bSuccess = ::GetTokenInformation(hHandleToken, TokenGroups,
- TokenInformation, sizeof(TokenInformation), &dwTokenInformationSize);
-
- ::CloseHandle(hHandleToken);
-
- if(!bSuccess)
- return FALSE;
-
- SID_IDENTIFIER_AUTHORITY siaNtAuthority = SECURITY_NT_AUTHORITY;
- PSID psidAdministrators;
-
- if(!::AllocateAndInitializeSid(&siaNtAuthority, 2,
- SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0,
- &psidAdministrators))
- return FALSE;
-
- // assume that we don't find the admin SID.
- bSuccess = FALSE;
-
- PTOKEN_GROUPS ptgGroups = (PTOKEN_GROUPS)TokenInformation;
-
- for(UINT x=0; x < ptgGroups->GroupCount; x++)
- {
- if(::EqualSid(psidAdministrators, ptgGroups->Groups[x].Sid))
- {
- bSuccess = TRUE;
- break;
- }
- }
-
- ::FreeSid(psidAdministrators);
-
- return bSuccess;
-}
-
-
-
-String LegalizeFileName(LPCTSTR szFileName)
-{
- TCHAR tempname[_MAX_PATH] = {0};
- LPTSTR p;
-
- _tcscpy(tempname, szFileName);
- while ((p=_tcspbrk(tempname, _T("\\'/:?\"<>|*\t\r\n"))) != NULL)
- {
- *p = _T('_');
- }
-
- return String(tempname);
-}
-
-static double tenpow(int expon)
-{
- double base=10;
- double rtn = pow(base, expon);
- return rtn;
-}
HANDLE RunIt(LPCTSTR szExeFile, LPCTSTR szArgs, BOOL bMinimized /*= TRUE*/, BOOL bNewConsole /*= FALSE*/)
{
return FALSE;
}
-#ifdef BROKEN
-// This function assigns to path, which is clearly bad as path is LPCTSTR
-BOOL IsLocalPath(LPCTSTR path)
-{
-
- _TCHAR finalpath[_MAX_PATH] = {0};
- _TCHAR temppath[_MAX_PATH] = {0};
- _TCHAR uncname[_MAX_PATH] = {0};
- _TCHAR computername[_MAX_PATH] = {0};
-
- BOOL bUNC=FALSE;
- BOOL bLocal=FALSE;
-
- TCHAR* pLoc=0;
-
- CString szInfo;
-
- //We need to get the root directory into an sz
- if((pLoc=_tcsstr(path, _T("\\\\")))!=NULL)
- {
- bUNC=TRUE;
-
- _tcscpy(temppath, pLoc+_tcslen(_T("\\\\")));
- _tcscpy(finalpath, _T("\\\\"));
-
- if((pLoc=_tcsstr(temppath, _T("\\")))!=NULL)
- {
- _tcsncpy(uncname, temppath, pLoc-temppath);
- _tcscat(finalpath, uncname);
-
- }
- else
- {
- _tcscpy(uncname, temppath);
- _tcscat(finalpath, uncname);
- }
-
- }
- else //standard path
- {
- _tcscpy(temppath, path);
- if((pLoc=_tcsstr(temppath, _T("\\")))!=NULL)
- {
- *pLoc = _T('\0');
- }
- _tcscpy(finalpath, temppath);
- }
-
- if(bUNC)
- {
- // get the machine name and compare it to the local machine name
- //if cluster, GetComputerName returns the name of the cluster
- DWORD len=_MAX_PATH-1;
- if(GetComputerName(computername, &len) && _tcsicmp(computername, uncname)==0
- )
- {
- //szInfo = _T("This is a UNC path to the local machine or cluster");
- bLocal=TRUE;
- }
- else
- {
- //szInfo = _T("This is a UNC path to a remote machine");
- }
- }
- else
- {
- //test the standard path
- UINT uType;
- uType = GetDriveType(finalpath);
-
- switch(uType)
- {
- case DRIVE_UNKNOWN:
- //szInfo = _T("The drive type cannot be determined.");
- break;
- case DRIVE_NO_ROOT_DIR:
- //szInfo = _T("The root directory does not exist.");
- break;
- case DRIVE_REMOVABLE:
- //szInfo = _T("The disk can be removed from the drive.");
- bLocal=TRUE;
- break;
- case DRIVE_FIXED:
- //szInfo = _T("The disk cannot be removed from the drive.");
- bLocal=TRUE;
- break;
- case DRIVE_REMOTE:
- //szInfo = _T("The drive is a remote (network) drive.");
- break;
- case DRIVE_CDROM:
- //szInfo = _T("The drive is a CD-ROM drive.");
- bLocal=TRUE;
- break;
- case DRIVE_RAMDISK:
- //szInfo = _T("The drive is a RAM disk.");
- bLocal=TRUE;
- break;
- //default:
- //szInfo = _T("GetDriveType returned an unknown type");
-
- }
- }
-
- return bLocal;
-}
-#endif
-
/**
* @brief Return module's path component (without filename).
* @param [in] hModule Module's handle.
return spath;
}
-/**
- * @brief Returns Application Data path in user profile directory
- * if one exists
- */
-BOOL GetAppDataPath(String &sAppDataPath)
-{
- TCHAR path[_MAX_PATH] = {0};
- if (GetEnvironmentVariable(_T("APPDATA"), path, _MAX_PATH))
- {
- sAppDataPath = path;
- return TRUE;
- }
- else
- {
- sAppDataPath = _T("");
- return FALSE;
- }
-}
-
-/**
- * @brief Returns User Profile path (if available in environment)
- */
-BOOL GetUserProfilePath(String &sAppDataPath)
-{
- TCHAR path[_MAX_PATH] = {0};
- if (GetEnvironmentVariable(_T("USERPROFILE"), path, countof(path)))
- {
- sAppDataPath = path;
- return TRUE;
- }
- else
- {
- sAppDataPath = _T("");
- return FALSE;
- }
-}
-
-/**
- * @brief Decorates commandline for giving to CreateProcess() or
- * ShellExecute().
- *
- * Adds quotation marks around executable path if needed, but not
- * around commandline switches. For example (C:\p ath\ex.exe -p -o)
- * becomes ("C:\p ath\ex.exe" -p -o)
- * @param [in] sCmdLine commandline to decorate
- * @param [out] sDecoratedCmdLine decorated commandline
- * @param [out] sExecutable Executable for validating file extension etc
- */
-void GetDecoratedCmdLine(String sCmdLine, String &sDecoratedCmdLine,
- String &sExecutable)
-{
- BOOL pathEndFound = FALSE;
- BOOL addQuote = FALSE;
- size_t prevPos = 0;
-
- sDecoratedCmdLine.erase();
- sExecutable.erase();
-
- // Remove whitespaces from begin and and
- std::string::size_type clpos = sCmdLine.find_first_not_of(_T(" \n\t"));
- if (clpos != 0)
- sCmdLine.erase(0, clpos);
- clpos = sCmdLine.find_last_not_of(_T(" \n\t"));
- if (clpos != sCmdLine.length() - 1)
- sCmdLine.erase(clpos, clpos - sCmdLine.length());
-
- std::string::size_type pos = sCmdLine.find(_T(" "));
- if (pos != String::npos)
- {
- // First space was before switch, we don't need "s
- // (executable path didn't contain spaces)
- if (sCmdLine[pos + 1] == '/' || sCmdLine[pos + 1] == '-')
- {
- pathEndFound = TRUE;
- }
- else
- {
- addQuote = TRUE;
- sDecoratedCmdLine = _T("\"");
- }
-
- // Loop until executable path end (first switch) is found
- while (pathEndFound == FALSE)
- {
- prevPos = pos;
- pos = sCmdLine.find(_T(" "), prevPos + 1);
-
- if (pos != String::npos)
- {
- if (sCmdLine[pos + 1] == '/' || sCmdLine[pos + 1] == '-')
- {
- pathEndFound = TRUE;
- }
- }
- else
- {
- pathEndFound = TRUE;
- }
- }
-
- if (addQuote)
- {
- if (pos != String::npos)
- {
- sExecutable = sCmdLine.substr(0, pos);
- sDecoratedCmdLine += sExecutable;
- sDecoratedCmdLine += _T("\"");
- sDecoratedCmdLine += sCmdLine.substr(pos, sCmdLine.length() - pos);
- }
- else
- {
- sExecutable = sCmdLine;
- sDecoratedCmdLine += sCmdLine;
- sDecoratedCmdLine += _T("\"");
- }
- }
- else
- {
- sDecoratedCmdLine = sCmdLine;
- sExecutable = sCmdLine;
- }
- }
- else
- {
- sDecoratedCmdLine = sCmdLine;
- sExecutable = sCmdLine;
- }
-}
#include "UnicodeString.h"
-/******** types & defines ********/
-#define BYTES 1L
-#define KBYTES 1024L
-#define MBYTES 1048576L
-
-// file types for GetFileType()
-#define FTYPE_UNDETERMINED 0
-#define FTYPE_EPS 1
-#define FTYPE_PS 2
-#define FTYPE_TIFF 3
-#define FTYPE_TEXT 4
-#define FTYPE_LAYOUT 5
-#define FTYPE_PRESS 6
-#define FTYPE_STEPPER 7
-#define FTYPE_FOLD 8
-#define FTYPE_TEMPLATE 9
-#define FTYPE_MARKTEMP 10
-#define FTYPE_DDES2 11
-#define FTYPE_PDF 12
-
-/******** macros ********/
-// use this to get number of elements in a static array
-#define ARRAY_SIZE(ar) (sizeof(ar)/sizeof(ar[0]))
-
-// bitwise flag macros
-#define FlagSet(var, flag) (((flag) & (var)) == (flag))
-#define AddFlag(var, flag) ((var) |= (flag))
-#define RemoveFlag(var, flag) ((var) = (var) & (~(flag)))
-#define ToggleFlag(var, flag, mask) { RemoveFlag(var,mask); AddFlag(var,flag); }
-#define GetFlag(var, mask) (var & mask)
-
-
-
-/******** function protos ********/
-
-template <class T>
-void selection_sort(T *ary, UINT cnt)
-{
- register int i,j,smallest;
- T t;
- for (i=0; i < (int)cnt-1; i++)
- {
- smallest = i;
- for (j=i+1; (UINT)j < cnt; j++)
- {
- if (ary[j] < ary[smallest])
- smallest = j;
- }
-
- t = ary[smallest];
- ary[smallest] = ary[i];
- ary[i] = t;
- }
-}
-
-template <class T>
-int linear_search(T *array, T target, int max)
-{
- register int i=0;
- for (i=0; i < max; i++)
- if (array[i] == target)
- return i;
-
- return NONE_FOUND;
-}
-
-BOOL GetFileTimes(LPCTSTR szFilename,
- LPSYSTEMTIME pMod,
- LPSYSTEMTIME pCreate =NULL,
- LPSYSTEMTIME pAccess =NULL);
-time_t GetFileModTime(LPCTSTR szPath);
-DWORD GetFileSizeEx(LPCTSTR szFilename);
-BOOL HaveAdminAccess();
-DWORD FPRINTF(HANDLE hf, LPCTSTR fmt, ... );
-DWORD FPUTS(LPCTSTR s, HANDLE hf);
-HANDLE FOPEN(LPCTSTR path, DWORD mode = GENERIC_READ, DWORD access = OPEN_EXISTING);
-void GetLocalDrives(LPTSTR letters);
void replace_char(LPTSTR s, int target, int repl);
-BOOL FileExtMatches(LPCTSTR filename, LPCTSTR ext);
void SplitFilename(LPCTSTR s, String * path, String * name, String * ext);
void SplitViewName(LPCTSTR s, String * path, String * name, String * ext);
-void TestSplitFilename();
-void AddExtension(LPTSTR name, LPCTSTR ext);
-int fcmp(float a,float b);
-BOOL FindAnyFile(LPTSTR filespec, LPTSTR name);
-long SwapEndian(long val);
-short int SwapEndian(short int val);
String GetModulePath(HMODULE hModule = NULL);
String GetPathOnly(LPCTSTR fullpath);
-float RoundMeasure(float measure, float units);
-
-String LegalizeFileName(LPCTSTR szFileName);
-int tcssubptr(LPCTSTR start, LPCTSTR end);
size_t linelen(const char *string, size_t maxlen);
HANDLE RunIt(LPCTSTR szExeFile, LPCTSTR szArgs, BOOL bMinimized = TRUE, BOOL bNewConsole = FALSE);
BOOL HasExited(HANDLE hProcess, DWORD *pCode = NULL);
-BOOL IsLocalPath(LPCTSTR path);
-BOOL GetAppDataPath(String &sAppDataPath);
-BOOL GetUserProfilePath(String &sAppDataPath);
-void GetDecoratedCmdLine(String sCmdLine, String &sDecoratedCmdLine,
- String &sExecutable);
#endif