📄 utility.cpp
字号:
/********************************************************************
*********************************************************************
** Utility
** This file contain soem importants functions I use in all my
** progs
**
**
**
** Enjoy to use it like you want ;)
**
** Gaetan SEMET
**
** gaetan.semet@wanadoo.fr
**
*********************************************************************
*********************************************************************/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "Utility.h"
#include <stdio.h>
#include <malloc.h>
char __FILETOLOG[MAX_PATH] = "c:\\LogToFile.log";
/******************************************************************\
Memory management section
\******************************************************************/
/*
This function create a buffer in memoy.
size : the size of the buffer to create
zero : fill with zero or not?
Return : if success, return a pointer to the new buffer, if fail,
it return an HeapAlloc errror
*/
LPVOID utilGetMem(int size, BOOL zero) //
{
return HeapAlloc(GetProcessHeap(), (zero ? HEAP_ZERO_MEMORY : 0), size);
}
/*
Free the memory allocated by GetMem
x : the buffer to free
Return : Nothing
*/
void utilFreeMem(LPVOID x)
{
if (!x)
HeapFree(GetProcessHeap(), 0, x);
}
/*
Change the size for the buffer
x : buffer to resize
newsize : new size of the buffer
zero : fill with zero extra data (if newsize if > previous size) ?
Return: if success, return a pointer to new resized buffer. If fail
return an HeapReAlloc error.
*/
PVOID utilReAllocMem(LPVOID x, int newsize, BOOL zero) // heap = GetProcessHeap()
{
if (x == 0)
{
x = GetMem(newsize,zero);
return x;
}
else
{
return HeapReAlloc(GetProcessHeap(), (zero ? HEAP_ZERO_MEMORY : 0), x, newsize);
}
}
/* ReDim : ReDimention a dynamic table of pointers...
pointer : address of pointer to resize
typesize : size of POINTED value (if pointer is char **, size must be
sizeof(char*)
NewCount : New count of entry there will have....
If minor than previous, datas can be loosed
size : size of BASE value (if pointer is char **, size must be
sizeof (char) = 1
count : count to size to create in BASE value (for exemple 50 to
create a 50 chars string if typesize is char **)
Notes: 1- pointer must be equal to 0 if you want to use it the first time,
to destroy him, but in this case typesize must be set to '-1',
and NewCount must have the real number of entry there is in...
2- To Redimention an BASE string, you can use MemRealloc.
But to resize to table of pointers, yo should use ReDim
3- If count or size is equal to 0, dynamique table is modificied,
but items are not allocated. For example, if you Redim ptr to
25 pointers, but with size and/or count set to 0, if you
use ptr[10], it will occurs an access violation. Use
new or you own memory allocation methode. BUT if you want to
call ReDim with -1 for size or count, you have to destroy
you pointer (ptr[10] for exemple) BEFORE using ReDim, and set
this pointer to NULL before calling ReDim.
Example:
{
classThing **ptr;
ReDim((void ***) &ptr,10,0,0);
for (int i = 0; i < 10; i++)
ptr[i] = new classThing;
for (int j = 0; j < 10; j++)
{
delete ptr[j];
ptr[j] = NULL;
}
ReDim((void ***) &ptr,-1,-1);
}
You will note than NEW pointer added to the dynamic table is
set to 0
Sample of call:
void MyFunc()
{
char **lpt;
lpt = NULL; // initialize pointer to 0 to be able to create it
ReDim((void ***) &lpt,5,sizeof(char),50);
// create 5 strings which have 50 chars in
lstrcpy(lpt[0],"First String");
lstrcpy(lpt[1],"Second String");
lstrcpy(lpt[2],"Third String");
lstrcpy(lpt[3],"Fourth String");
lstrcpy(lpt[4],"Fifth String");
ReDim((void ***) &lpt,6,sizeof(char),50);
// Resize to 6 strings which have 50 chars in
lstrcpy(lpt[5],"Sixth String");
ReDim((void ***) &lpt,6,-1,-1); // -1 = destroy, 6 is the number of entry in
}
*/
BOOL utilReDim(void ***pointer, UINT NewCount, int size, int count)
{
UINT i=0;
if (!pointer)
return false;
if (*pointer)
if ((size == -1) || (count == -1))
{
for (i=0; i < NewCount; i++)
{
if ((*pointer)[i])
{
FreeMem((*pointer)[i]);
(*pointer)[i] = NULL;
}
}
utilFreeMem(*pointer);
*pointer = NULL;
return true;
}
if ((size == -1) || (count == -1))
return false;
if (!*pointer)
{
*pointer = (void **) GetMem((sizeof(void *) * NewCount),true);
if ((count) && (size))
for (i=0; i < NewCount; i++)
{
if (!(*pointer)[i])
(*pointer)[i] = (void *) GetMem((size * count), true);
}
return true;
}
*pointer = (void **) ReAllocMem(*pointer,(sizeof(void *) * NewCount),true,);
if ((count) && (size))
for (i=0; i < NewCount; i++)
{
if (!(*pointer)[i])
(*pointer)[i] = (void *) GetMem((size * count), true);
}
return true;
}
/******************************************************************\
Command line functions section
\******************************************************************/
/*
Put the next parameter of command line in parm
cmd : command line to process
parm : will contain new parameter
Return : return command line to proceed. Use return value to
call GetCommandParameter in cmd parameter for next
command line parameter.
*/
LPSTR utilGetCommandParameter(LPSTR cmd, LPSTR parm)
{
*parm = 0;
while (isspace(*cmd))
cmd++;
if (!*cmd)
return cmd;
if (*cmd == '"')
{
cmd++;
while (*cmd && (*cmd != '"'))
{
if (*cmd == '\\')
{
if ((cmd[1] == '"') || (cmd[1] == '\\'))
cmd++;
*parm++ = *cmd++;
}
else
*parm++ = *cmd++;
}
while (*cmd == '"')
cmd++;
}
else
{
while (*cmd && !isspace(*cmd))
*parm++ = *cmd++;
}
*parm = 0;
while (isspace(*cmd))
cmd++;
return cmd;
}// Note: I took this source from Joe D. 's HPIPack.
/* utilParseCommandLineInit
* Initialise memory for argv table
* argv : pointer to two dimension table argv
* if argv == NULL, it will be allocated
* if != NULL, it's supposed to be already allocated in memory
* max_parameter_number : maximum number of parameter
* max_parameter_size : maximum size of each parameter
*/
void utilParseCommandLineInit(char** argv, int max_parameter_number, int max_parameter_size)
{
if (!argv)
argv=(char**) calloc(max_parameter_number, sizeof(char*) );
for ( int i=0; i<max_parameter_number; ++i)
argv[i]= (char*)calloc(max_parameter_size,sizeof(char));
}
/* utilParseCommandLineFree
* Free memory for argv table
* argv : pointer to two dimension table argv
* max_parameter_number : maximum number of parameter
* destoy_me : 0 : will not destroy argv (must be a char **)
* 1 : will destroy argv (if it's a char * [???])
*/
void utilParseCommandLineFree(char** argv, int max_parameter_number, int destroy_me)
{
for ( int i=0; i<max_parameter_number; ++i)
free(argv[i]);
if (destroy_me)
free(argv);
}
/* utilParseCommandLine
* "Cut" the command line into the argv table
* cmdLine: full command line
* argv a two dimension table (use for exemple a char * argv[1024])
* argc will receive the number of argument
*/
void utilParseCommandLine(const char *cmdLine, char **argv,int*argc)
{
char *buffer[8192], *ptr; //8192 parameters limitation!!!
int quote, ch;
char *bufcmdline= (char*) calloc (lstrlen(cmdLine)+1, sizeof(char));
char * pt_bufcmdline = bufcmdline;
buffer[0]=bufcmdline;
lstrcpy(bufcmdline,cmdLine);
int i=0;
for(quote = *argc = 0, (buffer)[0] = ptr = (char*)bufcmdline;;)
{
if((ch = *bufcmdline++) == '"')
{
quote ^= 1;
}
else if(ch && (quote || (ch != ' ' && ch != '\t')))
{
*ptr++ = (char) ch;
}
else
{
if(ptr != (argv)[*argc])
{
*ptr++ = '\0';
buffer[++i] = ptr;
}
if(ch == '\0')
{
break;
}
}
}
for (int j=0;j<i;++j)
{
++(*argc);
lstrcpy((argv)[j],buffer[j]);
}
free(pt_bufcmdline);
}
/******************************************************************\
Strings functions section
\******************************************************************/
/* utilGetProcessDirectory
* Retreive the directory where the process was launched
* buf : string buffer. Will receive the datas
* buflen : max data lenght to copy
*/
BOOL utilGetProcessDirectory(char *buf, int buflen)
{
char *cmdline;
static char * szProcessDirectory=NULL;
char *argv[1024];
int argc;
if (szProcessDirectory)
{
lstrcpyn(buf,szProcessDirectory,buflen);
return true;
}
cmdline = GetCommandLine();
utilParseCommandLineInit(argv,1024,256);
utilParseCommandLine(cmdline,argv,&argc);
szProcessDirectory = (char *) calloc(1024,sizeof(char));
utilExtractFilePath(argv[0],szProcessDirectory,1024);
lstrcpyn(buf,szProcessDirectory,buflen);
utilParseCommandLineFree(argv,1024, false);
return true;
}
/*
Remove Back Slash from path and put the result into buf.
path : path to remove '\'
buf : will receive the new string without '\'
BufLen : length in byte of buf
Return true if success, false if fail
*/
BOOL utilRemBkSlash(LPSTR path, LPSTR buf, int BufLen)
{
int i;
LPSTR newstr = (LPSTR) GetMem(BufLen,true);
if (path[lstrlen(path)-1] == '\\')
{
if (BufLen < lstrlen(path) + 1)
{
lstrcpy(buf,"");
return FALSE;
}
for ( i = 0; i<=lstrlen(path)-2; i++)
newstr[i] = path[i];
newstr[lstrlen(path)-1] = '\0';
lstrcpy(buf, newstr);
FreeMem(newstr);
return TRUE;
}
else
{
if (BufLen >= lstrlen(path) + 1)
{
lstrcpy(buf,path);
FreeMem(newstr);
return TRUE;
}
else
{
lstrcpy(buf,"");
FreeMem(newstr);
return FALSE;
}
}
lstrcpy(buf,"");
FreeMem(newstr);
return (FALSE);
}
/*
Add Back Slash from path and put the result into buf.
path : path to add '\'
buf : will receive the new string with '\'
BufLen : length in byte of buf
Return true if success, false if fail
*/
BOOL utilAddBkSlash(LPSTR path, LPSTR buf, int BufLen)
{
int i;
LPSTR newstr = (LPSTR) GetMem(BufLen,true);
if (path[lstrlen(path)-1] != '\\')
{
if (BufLen < lstrlen(path) + 2)
{
lstrcpy(buf,"");
FreeMem(newstr);
return FALSE;
}
for ( i = 0; i<=lstrlen(path); i++)
newstr[i] = path[i];
newstr[lstrlen(path)] = '\\';
newstr[lstrlen(path)+1] = '\0';
lstrcpy(buf,newstr);
FreeMem(newstr);
return TRUE;
}
else
{
if (BufLen >= lstrlen(path) + 1)
{
lstrcpy(buf,path);
FreeMem(newstr);
return TRUE;
}
else
{
lstrcpy(buf,"");
FreeMem(newstr);
return FALSE;
}
}
lstrcpy(buf,"");
FreeMem(newstr);
return (FALSE);
}
/*
Extract filename from path and put result into buf.
path : file path to extract the name
buf : will receive the filename
BufLen : length in byte of buf
Return 0 if fail, or the length of buf if success
*/
int utilExtractFileName(LPSTR path, LPSTR buf, int BufLen)
{
int i,j,m;
int result;
LPSTR newstr = (LPSTR) GetMem(BufLen,true);
result = 0;
if ((!lstrlen(path)) || (BufLen < 1) )
{
lstrcpy(buf,"");
FreeMem(newstr);
return 0;
}
if (!strstr(path,"\\"))
{
strcpy(buf,path);
return lstrlen(buf);
}
for (i = lstrlen(path);i >= 0; i--)
{
if (path[i] == '\\')
{
if (BufLen < (lstrlen(path) - i) )
{
lstrcpy(buf,"");
FreeMem(newstr);
return 0;
}
m=0;
for ( j = i+1; j <= lstrlen(path); j++)
{
m=j-(i+1);
newstr[m] = path[j];
}
newstr[m+1]='\0';
result = lstrlen(newstr);
lstrcpy(buf,newstr);
FreeMem(newstr);
return result;
}
}
lstrcpy(buf,"");
FreeMem(newstr);
return (result);
}
/*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -