window程序移植到linux(二)

1.

windows程序中文件查找函数 

SetCurrentDirectory函数;

FindFirstFile函数;

GetFullPathName函数;

CStringList& CFileSearchEngine::SearchFile(
        CString strPath, 
        CStringList &listFileName,
        CString strFileName)
{
char szFullPathName[MAX_PATH];
/*static*/ WIN32_FIND_DATAfindData;


SetCurrentDirectory(strPath);


HANDLE hFindHandle  =  FindFirstFile(strFileName, &findData);
if ((hFindHandle !=  INVALID_HANDLE_VALUE))
{
if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
GetFullPathName(findData.cFileName, MAX_PATH, szFullPathName, NULL);
            if(!(::GetFileAttributes(findData.cFileName) & FILE_ATTRIBUTE_DIRECTORY))
            {
                //文件过滤
                if(m_strFilter=="")
                    listFileName.AddTail(szFullPathName);
                else if(_MultiMatching( (LPCTSTR)GetFileName(szFullPathName), (LPCTSTR)m_strFilter))
                    listFileName.AddTail(szFullPathName);
            }
}
while (FindNextFile(hFindHandle, &findData) !=  0)
{
if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
GetFullPathName(findData.cFileName, MAX_PATH, szFullPathName, NULL);
                if(!(::GetFileAttributes(findData.cFileName) & FILE_ATTRIBUTE_DIRECTORY))
                {
                    //文件过滤
                    if(m_strFilter=="")
                        listFileName.AddTail(szFullPathName);
                    else if(_MultiMatching( (LPCTSTR)GetFileName(szFullPathName), (LPCTSTR)m_strFilter))
                        listFileName.AddTail(szFullPathName);
                }
}
}
FindClose(hFindHandle);
}


hFindHandle  =  FindFirstFile("*", &findData);
if ((hFindHandle !=  INVALID_HANDLE_VALUE))
{
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (findData.cFileName[0] !=  '.')
{
SearchFile(findData.cFileName, listFileName,strFileName);
SetCurrentDirectory("..");
}
}
while (FindNextFile(hFindHandle, &findData) !=  0)
{
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (findData.cFileName[0] !=  '.')
{
SearchFile(findData.cFileName, listFileName,strFileName);
SetCurrentDirectory("..");
}
}
}
FindClose(hFindHandle);
}


return listFileName;
}

在linux下实现文件查找

CStringList& CFileSearchEngine::SearchFile(
        CStdString strPath,
        CStringList &listFileName,
        CStdString strFileName)
{
CStdString szFullPathName;
chdir(strPath);


DIR              *pDir ;
struct dirent    *file  ;
if(!(pDir = opendir(strPath)))
{
CStdString str;
str.Format("error opendir %s!!!\n",strPath);
AxNote(1,str);
}
while((file = readdir(pDir)) != NULL)
{
if (file->d_type & DT_DIR)
{
if (strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0)
continue;
}
if(strcmp(file->d_name,strFileName) == 0)
{
szFullPathName.Format("%s/%s",strPath,file->d_name);
struct stat status;
stat(szFullPathName,&status);
if(!S_ISDIR(status.st_mode))
{
//文件过滤
if (m_strFilter == "")
listFileName.push_back(szFullPathName);
else if (_MultiMatching((LPCTSTR) file->d_name,(LPCTSTR) m_strFilter))
listFileName.push_back(szFullPathName);
}
}


 }
int pos = strPath.ReverseFind('/');
strPath.Delete(pos, strPath.length() - pos);
if (strPath != "")                           //这里是从最里面的文件夹往外层搜索直到根目录递归调用
{
SearchFile(strPath,listFileName,strFileName);
}
closedir(pDir);
return listFileName;
}

2.windows程序中_splitpath函数在linux中的实现(转自网上资料)

  1. void _splitpath(const char *path, char *drive, char *dir, char *fname, char *ext)  
  2. {  
  3.     char *p_whole_name;  
  4.   
  5.     drive[0] = '\0';  
  6.     if (NULL == path)  
  7.     {  
  8.         dir[0] = '\0';  
  9.         fname[0] = '\0';  
  10.         ext[0] = '\0';  
  11.         return;  
  12.     }  
  13.   
  14.     if ('/' == path[strlen(path)])  
  15.     {  
  16.         strcpy(dir, path);  
  17.         fname[0] = '\0';  
  18.         ext[0] = '\0';  
  19.         return;  
  20.     }  
  21.   
  22.     p_whole_name = rindex(path, '/');  
  23.     if (NULL != p_whole_name)  
  24.     {  
  25.         p_whole_name++;  
  26.         _split_whole_name(p_whole_name, fname, ext);  
  27.   
  28.         snprintf(dir, p_whole_name - path, "%s", path);  
  29.     }  
  30.     else  
  31.     {  
  32.         _split_whole_name(path, fname, ext);  
  33.         dir[0] = '\0';  
  34.     }  
  35. }  
  36.   
  37. static void _split_whole_name(const char *whole_name, char *fname, char *ext)  
  38. {  
  39.     char *p_ext;  
  40.   
  41.     p_ext = rindex(whole_name, '.');  
  42.     if (NULL != p_ext)  
  43.     {  
  44.         strcpy(ext, p_ext);  
  45.         snprintf(fname, p_ext - whole_name + 1, "%s", whole_name);  
  46.     }  
  47.     else  
  48.     {  
  49.         ext[0] = '\0';  
  50.         strcpy(fname, whole_name);  
  51.     }  
  52. }  

3.wndows程序中获取磁盘剩余空间大小

函数:BOOL ret = GetDiskFreeSpaceEx(
drive,
(PULARGE_INTEGER)&qwFreeBytesToCaller,
(PULARGE_INTEGER)&qwTotalBytes,
(PULARGE_INTEGER)&qwFreeBytes
);

linux下可以使用 

 int statfs(const char *path, struct statfs *buf);
       int fstatfs(int fd, struct statfs *buf);

头文件    #include <sys/vfs.h>    /* or <sys/statfs.h> */
 

struct statfs {
long f_type; /* 文件系统类型 */
long f_bsize; /* 经过优化的传输块大小 */
long f_blocks; /* 文件系统数据块总数 */
long f_bfree; /* 可用块数 */
long f_bavail; /* 非超级用户可获取的块数 */
long f_files; /* 文件结点总数 */
long f_ffree; /* 可用文件结点数 */
fsid_t f_fsid; /* 文件系统标识 */
long f_namelen; /* 文件名的最大长度 */
};





















  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值