📄 utnt.c
字号:
/* * This file was written by Bill Cox, originally in 1991, and maintained since. It is hereby * placed into the public domain. */#include "utnojunk.h"#include <windows.h>#include <direct.h>#include <stdlib.h>#include <io.h>#include "ddutil.h"#include <time.h>jmp_buf utJmpBuf[UT_MAX_SETJMP_DEPTH];/*-------------------------------------------------------------------------------------------------- Decrement the setjmp stack, and longjmp.--------------------------------------------------------------------------------------------------*/void utLongjmp (void){ utSetjmpDepth--; if (utSetjmpDepth < 0 || utSetjmpDepth > UT_MAX_SETJMP_DEPTH) { utExit("utLongjmp: No utSetjmp for utLongjmp"); } else { longjmp(utJmpBuf[utSetjmpDepth], 1); }}/*-------------------------------------------------------------------------------------------------- Default Error notification routine in absence of user-speified.--------------------------------------------------------------------------------------------------*/void UTEXPORT ErrorNotify ( char *string){ printf("%s\n", string);}/*-------------------------------------------------------------------------------------------------- Allocate a block of memory. Don't fill with 0's.--------------------------------------------------------------------------------------------------*/void * UTEXPORT mtCalloc ( uint32 numBlocks, uint16 size){ return calloc((unsigned)numBlocks, (unsigned)size);}/*-------------------------------------------------------------------------------------------------- Allocate a block of memory. Don't fill with 0's.--------------------------------------------------------------------------------------------------*/void * UTEXPORT mtMalloc ( uint32 numBlocks, uint16 size){ return malloc(numBlocks*size);}/*-------------------------------------------------------------------------------------------------- Reallocate and expand memory.--------------------------------------------------------------------------------------------------*/void * UTEXPORT mtRealloc ( void *mem, uint32 numBlocks, uint16 size){ return realloc((char *)mem, (size_t)(numBlocks * size));}/*-------------------------------------------------------------------------------------------------- Free a blo`ck of memory. Return TRUE if sucess.--------------------------------------------------------------------------------------------------*/bool UTEXPORT mtFree ( void *p){ free(p); return true;}/*-------------------------------------------------------------------------------------------------- FUNCTION: utGetProfileString PURPOSE : Function to read a string from a .ini file--------------------------------------------------------------------------------------------------*/bool utGetProfileString(char *szToolName, char *szKeyName, char *szDefault, char *szBuffer, int16 nSize){ bool ret; ret = (bool) GetProfileString(szToolName, szKeyName, szDefault, szBuffer, nSize); return ret;}/*-------------------------------------------------------------------------------------------------- FUNCTION: utGetProfileString PURPOSE : Function to write a string to win.ini file--------------------------------------------------------------------------------------------------*/bool utWriteProfileString(char *szSection, char *szKeyName, char *szValue){ bool ret; ret = (bool) WriteProfileString(szSection, szKeyName, szValue); return ret;}/*-------------------------------------------------------------------------------------------------- FUNCTION: utGetPrivateProfileInt PURPOSE : Function to read a Int from a .ini file--------------------------------------------------------------------------------------------------*/int32 utGetPrivateProfileInt(char *szToolName, char *szKeyName, int32 nDefault, char *szIniFileName){ int32 val; val = GetPrivateProfileInt(szToolName, szKeyName, nDefault, szIniFileName); return val;}/*-------------------------------------------------------------------------------------------------- FUNCTION: utGetPrivateProfileString PURPOSE : Function to read a string from a .ini file--------------------------------------------------------------------------------------------------*/void utGetPrivateProfileString(char *szToolName, char *szKeyName, char *szDefault, char *szBuffer, int16 nSize, char *szIniFileName){ GetPrivateProfileString(szToolName, szKeyName, szDefault, szBuffer, nSize, szIniFileName);}/*-------------------------------------------------------------------------------------------------- FUNCTION: utWritePrivateProfileString PURPOSE : Function to write a string to a .ini file--------------------------------------------------------------------------------------------------*/void utWritePrivateProfileString(char *szToolName, char *szKeyName, char *szSetStr, char *szIniFileName){ WritePrivateProfileString(szToolName, szKeyName, szSetStr, szIniFileName);}/*-------------------------------------------------------------------------------------------------- Launches an application. Simple interface - no params except command line & wkgDir. Command line can include args. Wkg dir can be NULL.--------------------------------------------------------------------------------------------------*/bool UTEXPORT utLaunchApp ( char* cmdLine, char* wkgDir){ PROCESS_INFORMATION processInfo; STARTUPINFO startupInfo; utClearMem((void *)(&startupInfo), sizeof(STARTUPINFO)); startupInfo.cb = sizeof(STARTUPINFO); if (!CreateProcess(NULL, (char *)cmdLine, NULL, NULL, FALSE, 0, NULL, wkgDir, &startupInfo, &processInfo)) { utError("UT1000: Failed to launch program %s - Error %d", cmdLine, GetLastError()); return false; } return true;}/*-------------------------------------------------------------------------------------------------- Terminate. Unix optionally writes a core file for debug, NT just exits.--------------------------------------------------------------------------------------------------*/void utAbnormalProgramTermination(void){ exit(-1);}/*-------------------------------------------------------------------------------------------------- FUNCTION: utGetModuleDirectory PURPOSE : Function to get the path of the specified module (exe or dll) NOTE: Module must be loaded already!--------------------------------------------------------------------------------------------------*/bool utGetModuleDirectory ( char *moduleName, char *path, int16 max_path){ HMODULE hModule; hModule = GetModuleHandle(moduleName); /* get module instance */ if (!hModule) { return false; } GetModuleFileName(hModule, path, max_path); return true;}/*-------------------------------------------------------------------------------------------------- Set the environment variable.--------------------------------------------------------------------------------------------------*/void utSetEnvironmentVariable( char *name, char *value){ SetEnvironmentVariable(name, value);}/*-------------------------------------------------------------------------------------------------- Get the environment variable. Return NULL if it is not set.--------------------------------------------------------------------------------------------------*/char *utGetEnvironmentVariable( char *name){ return getenv(name);}/*-------------------------------------------------------------------------------------------------- Match the string to a file and return the matched file name.--------------------------------------------------------------------------------------------------*/char *utGlob( char *fileName){ if(utAccess(fileName, NULL)) { return utCopyString(fileName); } return NULL;}/*-------------------------------------------------------------------------------------------------- Find the current working directory name.--------------------------------------------------------------------------------------------------*/char *utGetcwd(void){ char buffer[UTSTRLEN]; _getcwd(buffer, UTSTRLEN); return utCopyString(buffer);}/*-------------------------------------------------------------------------------------------------- Take the relative path, and return the full path.--------------------------------------------------------------------------------------------------*/char *utFullPath( char *relativePath){ char fullPath[UTSTRLEN]; _fullpath(fullPath, relativePath, UTSTRLEN); return utCopyString(fullPath);}/*-------------------------------------------------------------------------------------------------- Determine the access to a file--------------------------------------------------------------------------------------------------*/bool utAccess( char *name, char *mode){ if(mode == NULL) { return _access(name, 0) == 0; } if(!strcmp(mode, "r")) { return _access(name, 4) == 0; } if(!strcmp(mode, "w")) { return _access(name, 2) == 0; } if(!strcmp(mode, "x")) { return _access(name, 0) == 0; } utExit("Unknown mode passed to utAccess"); return false; /* Dummy return */}/*-------------------------------------------------------------------------------------------------- Return the path name which an executable resides.--------------------------------------------------------------------------------------------------*/char *utExecPath( char *program){ char *path, *p; char *fullPath; if(strchr(program, UTDIRSEP) != NULL) { return utFullPath(program); } else { path = getenv ("PATH"); if(path != NULL) { p = path; do { path = p; p = strchr(path, UTPATHSEP); if(p == NULL) { p = strchr(path, '\0'); } if(p == path) { fullPath = utCopyString(program); } else { fullPath = utMakeString((p - path) + 1); utStrncpy(fullPath, path, p - path); fullPath = utSprintf("%s%c%s", fullPath, UTDIRSEP, program); } fullPath = utGlob(fullPath); if(fullPath != NULL) { return fullPath; } fullPath = utGlob(utSprintf("%s.exe", fullPath)); if(fullPath != NULL) { return fullPath; } } while(*p++ != '\0'); } } utError("could not find executable %s", program); return NULL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -