实现了跨平台的路径管理
mpath.h文件
#ifndef MPATH_H
#define MPATH_H
#ifdef WINDOWS
#define PATH_SPLIT '\\'
#endif
#ifdef UNIX
#define PATH_SPLIT '/'
#endif
#define ALL_SPLITS "\\/$"
#define MAX_PATH_LEN 500
///转换路径
///@param target 目标
///@param source 源
void convertPath(char *target, const char *source);
///替换标准的fopen函数
FILE *mfopen(const char *filename, const char *mode);
#endif
mapth.cpp文件
void convertPath(char *target, const char *source)
{
const char *s;
char *t;// 200
for (s=source, t=target; ((s-source)<MAX_PATH_LEN) && (*s!='\0'); s++, t++)
{
if (strchr(ALL_SPLITS,*s)!=NULL)
{
*t=PATH_SPLIT;
}
else
{
*t=*s;
}
}
*t='\0';
}
FILE *mfopen(const char *filename, const char *mode)
{
char actualName[MAX_PATH_LEN+1];
convertPath(actualName,filename);
return fopen(actualName,mode);
}
网友评论