📄 wce_directorymanagement.c
字号:
#include <winbase.h>
#include <shellapi.h>
#include <wce_direct.h>
#include <wce_errno.h>
/*******************************************************************************
* InitCwd - Init the current directory with the path for the file used to create
* the calling process
*
* Description:
*
* Return:
*
* Reference:
*
*******************************************************************************/
static wchar_t Cwd[_MAX_PATH] = { '\0' };
static int InitCwd()
{
if( Cwd[0] )
return 0;
if( !GetModuleFileName( NULL, Cwd, _MAX_PATH ) )
return errno = GetLastError();
else
{
wchar_t* slash = wcsrchr( Cwd, '\\' );
if( !slash )
slash = wcsrchr( Cwd, '/' );
if( slash )
{
if( slash == Cwd )
slash++;
*slash = 0;
}
return 0;
}
}
/*******************************************************************************
* wceex_getcwd - Get the current working directory
*
* Description:
*
* Return:
*
* Reference:
*
*******************************************************************************/
char* wceex_getcwd( char *buffer, int maxlen )
{
if( !buffer && (buffer = (char*)malloc(maxlen)) == NULL )
{
errno = ENOMEM;
return NULL;
}
if( InitCwd() )
return NULL;
if( !WideCharToMultiByte( CP_ACP, 0, Cwd, -1, buffer, maxlen, NULL, NULL ) )
{
errno = GetLastError();
return NULL;
}
return buffer;
}
/*******************************************************************************
* wceex_GetCurrentDirectoryW - Get the current working directory
*
* Description:
*
* Return:
*
* Reference:
*
*******************************************************************************/
DWORD wceex_GetCurrentDirectoryW( DWORD nBufferLength, LPWSTR lpBuffer )
{
*lpBuffer = 0;
if( InitCwd() )
{
SetLastError( errno );
return 0;
}
else
{
size_t slen = wcslen( Cwd );
if( slen >= (size_t)nBufferLength )
return slen + 1;
wcscpy( lpBuffer, Cwd );
return slen;
}
}
/*******************************************************************************
* wceex_wgetcwd - Get the current working directory
*
* Description:
*
* Return:
*
* Reference:
*
*******************************************************************************/
wchar_t* wceex_wgetcwd( wchar_t *buffer, int maxlen )
{
if( !buffer && (buffer = (wchar_t*)malloc(maxlen * sizeof(wchar_t))) == NULL )
{
errno = ENOMEM;
return NULL;
}
else
{
DWORD slen = wceex_GetCurrentDirectoryW( maxlen, buffer );
if( !slen )
return NULL;
if( slen >= (DWORD)maxlen )
{
errno = ERANGE;
return NULL;
}
return buffer;
}
}
/*******************************************************************************
* wceex_wchdir - Change the current working directory
*
* Description:
*
* Return:
*
* Reference:
*
*******************************************************************************/
int wceex_wchdir( const wchar_t *dirname )
{
if( !dirname || *dirname == 0 )
{
errno = ENOENT;
return -1;
}
else
{
SHFILEINFO fi;
if( !SHGetFileInfo( dirname, 0, &fi, sizeof(fi), SHGFI_ATTRIBUTES ) )
{
errno = ENOENT;
return -1;
}
if( !(fi.dwAttributes & SFGAO_FOLDER) )
{
errno = ENOENT;
return -1;
}
wcscpy( Cwd, dirname );
return 0;
}
}
/*******************************************************************************
* wceex_chdir - Change the current working directory
*
* Description:
*
* Return:
*
* Reference:
*
*******************************************************************************/
int wceex_chdir( const char *dirname )
{
if( !dirname || *dirname == 0 )
{
errno = ENOENT;
return -1;
}
else
{
wchar_t wdirname[_MAX_PATH];
if( !MultiByteToWideChar( CP_ACP, 0, dirname, -1, wdirname, _MAX_PATH ) )
{
errno = ENOENT;
return -1;
}
return wceex_wchdir( wdirname );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -