-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileUtils.cpp
160 lines (148 loc) · 3.31 KB
/
FileUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// $Id: FileUtils.cpp,v 1.4 2009/06/24 18:02:38 samn Exp $
#include "stdafx.h"
#include "FileUtils.h"
#include "StringUtils.h"
#include <stdio.h>
#include <io.h>
using namespace std;
std::string GetFileExt(std::string& strPath)
{
char drive[_MAX_DRIVE],
dir[_MAX_DIR],
fname[_MAX_FNAME],
ext[_MAX_EXT];
_splitpath(strPath.c_str(),drive,dir,fname,ext);
std::string strExt = ext;
return strExt;
}
CString GetFileExt(CString& strPath)
{
char drive[_MAX_DRIVE],
dir[_MAX_DIR],
fname[_MAX_FNAME],
ext[_MAX_EXT];
_splitpath(strPath.GetBuffer(),drive,dir,fname,ext);
CString strExt(ext);
return strExt;
}
std::string GetFileName(std::string& strPath,bool bGetExt)
{
char drive[_MAX_DRIVE],
dir[_MAX_DIR],
fname[_MAX_FNAME],
ext[_MAX_EXT];
_splitpath(strPath.c_str(),drive,dir,fname,ext);
std::string strFname(fname);
if(bGetExt)
{ std::string strExt(ext);
strFname += ext;
}
return strFname;
}
CString GetFileName(CString& strPath,bool bGetExt)
{
char drive[_MAX_DRIVE],
dir[_MAX_DIR],
fname[_MAX_FNAME],
ext[_MAX_EXT];
_splitpath(strPath.GetBuffer(),drive,dir,fname,ext);
CString strFname(fname);
if(bGetExt)
{ CString strExt(ext);
strFname += ext;
}
return strFname;
}
CString GetDir(CString& strFilePath)
{
char drive[_MAX_DRIVE],
dir[_MAX_DIR],
fname[_MAX_FNAME],
ext[_MAX_EXT];
_splitpath(strFilePath.GetBuffer(),drive,dir,fname,ext);
CString strDir;
strDir.Format("%s%s",drive,dir);
return strDir;
}
CString GetFilePath(CString& strPath,bool bGetExt)
{
char drive[_MAX_DRIVE],
dir[_MAX_DIR],
fname[_MAX_FNAME],
ext[_MAX_EXT];
_splitpath(strPath.GetBuffer(),drive,dir,fname,ext);
CString strFname(fname);
if(bGetExt)
strFname.Format("%s%s%s%s",drive,dir,fname,ext);
else
strFname.Format("%s%s%s",drive,dir,fname);
return strFname;
}
bool GetLines(FILE* fp,std::vector<std::string>& vstr)
{
if(!fp) return false;
char line[1024];
while(!feof(fp))
{
line[0]=0;
fgets(line,1024,fp);
Chop(line); //get rid of newlines
if(line[0])
vstr.push_back(line);
}
return true;
}
bool GetLines(char* fname,vector<string>& vstr)
{
FILE* fp = fopen(fname,"r");
if(!fp) return false;
bool b = GetLines(fp,vstr);
fclose(fp);
return b;
}
bool FileExists(const char* fname)
{
FILE* fp = fopen(fname,"r");
if(fp)
{
fclose(fp);
return true;
}
return false;
}
long FileSize(const char* fname)
{
FILE* fp = fopen(fname,"rb");
if(!fp) return 0;
long sz;
fseek(fp,0,SEEK_END);
sz = ftell(fp);
fclose(fp);
return sz;
}
//path is directory, ext is file type extension, vfiles will have full path of each file found
//returns true on success
bool GetFilesInDir(std::string& path,std::string& ext,std::vector<std::string>& vfiles)
{
vfiles.clear();
_finddata_t c_file;
long hFile;
std::string srch(path);
if(srch[srch.size()-1]!='\\')
srch+="\\";
srch+="*";
if(ext[0]!='.')
srch+=".";
srch+=ext;
if ( (hFile = _findfirst(srch.c_str(), &c_file)) == -1L )
return false;
else
{ do
{
if(!strcmp(c_file.name,".") && !strcmp(c_file.name,".."))
vfiles.push_back(std::string(c_file.name));
} while ( _findnext(hFile, &c_file) == 0 );
_findclose(hFile);
}
return vfiles.size()!=0;
}