Skip to content

Commit 24ecb80

Browse files
committed
Implemented new API functions:
* uint32_t getFileSize(const char * iPath); * bool folderExists(const char * iPath); * void splitPath(const std::string & iPath, std::vector<std::string> & oElements);
1 parent a2d52d5 commit 24ecb80

File tree

2 files changed

+72
-7
lines changed

2 files changed

+72
-7
lines changed

src/common/filesystemfunc.cpp

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,17 @@
1818
namespace filesystem
1919
{
2020

21-
long getFileSize(FILE * f)
21+
uint32_t getFileSize(const char * iPath)
22+
{
23+
FILE * f = fopen(iPath, "rb");
24+
if (!f)
25+
return 0;
26+
uint32_t size = getFileSize(f);
27+
fclose(f);
28+
return size;
29+
}
30+
31+
uint32_t getFileSize(FILE * f)
2232
{
2333
if (f == NULL)
2434
return 0;
@@ -45,15 +55,24 @@ namespace filesystem
4555
return filename;
4656
}
4757

48-
bool fileExists(const char * iFilePath)
58+
bool fileExists(const char * iPath)
4959
{
50-
FILE * f = fopen(iFilePath, "rb");
60+
FILE * f = fopen(iPath, "rb");
5161
if (!f)
5262
return false;
5363
fclose(f);
5464
return true;
5565
}
5666

67+
bool folderExists(const char * iPath)
68+
{
69+
std::string localFolder = getCurrentFolder();
70+
bool success = (_chdir(iPath) == 0);
71+
if (success)
72+
_chdir(localFolder.c_str());
73+
return success;
74+
}
75+
5776
std::string getTemporaryFileName()
5877
{
5978
char rndvalue[1024];
@@ -107,10 +126,35 @@ namespace filesystem
107126
}
108127
}
109128

129+
void splitPath(const std::string & iPath, std::vector<std::string> & oElements)
130+
{
131+
oElements.clear();
132+
std::string s = iPath;
133+
std::string accumulator;
134+
for(unsigned int i = 0; i<s.size(); i++)
135+
{
136+
const char & c = s[i];
137+
if (c == getPathSeparator() && accumulator.size() > 0)
138+
{
139+
oElements.push_back(accumulator);
140+
accumulator = "";
141+
}
142+
else
143+
accumulator += c;
144+
}
145+
if (accumulator.size() > 0)
146+
{
147+
oElements.push_back(accumulator);
148+
accumulator = "";
149+
}
150+
}
151+
110152
char getPathSeparator()
111153
{
112154
#ifdef _WIN32
113155
return '\\';
156+
#else
157+
return '/';
114158
#endif
115159
}
116160

src/common/filesystemfunc.h

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,24 @@
22

33
#include <stdint.h>
44
#include <string>
5+
#include <vector>
56

67
namespace filesystem
78
{
89

10+
///<summary>
11+
///Returns the size of the given file path in bytes.
12+
///</summary>
13+
///<param name="f">An valid file path.</param>
14+
///<return>Returns the size of the given file path in bytes.<return>
15+
uint32_t getFileSize(const char * iPath);
16+
917
///<summary>
1018
///Returns the size of the given FILE* in bytes.
1119
///</summary>
1220
///<param name="f">An valid FILE pointer.</param>
1321
///<return>Returns the size of the given FILE* in bytes.<return>
14-
long getFileSize(FILE * f);
22+
uint32_t getFileSize(FILE * f);
1523

1624
///<summary>
1725
///Returns the filename of the given path.
@@ -23,9 +31,16 @@ namespace filesystem
2331
///<summary>
2432
///Determine if a file exists.
2533
///</summary>
26-
///<param name="iFilePath">An valid file path.</param>
34+
///<param name="iPath">An valid file path.</param>
2735
///<return>Returns true when the file exists. Returns false otherwise.<return>
28-
bool fileExists(const char * iFilePath);
36+
bool fileExists(const char * iPath);
37+
38+
///<summary>
39+
///Determine if a folder exists.
40+
///</summary>
41+
///<param name="iPath">An valid folder path.</param>
42+
///<return>Returns true when the folder exists. Returns false otherwise.<return>
43+
bool folderExists(const char * iPath);
2944

3045
///<summary>
3146
///Returns the file name of a tempporary file.
@@ -52,9 +67,15 @@ namespace filesystem
5267
///<param name="iPath">The input path to split.</param>
5368
///<param name="oFolder">The output folder of the given path.</param>
5469
///<param name="oFile">The output file of the given path.</param>
55-
///<return>Returns a the folder and the filename of the given path.<return>
5670
void splitPath(const std::string & iPath, std::string & oFolder, std::string & oFilename);
5771

72+
///<summary>
73+
///Splits a path into each element.
74+
///</summary>
75+
///<param name="iPath">The input path to split.</param>
76+
///<param name="oElements">The output list which contains all path elements.</param>
77+
void splitPath(const std::string & iPath, std::vector<std::string> & oElements);
78+
5879
///<summary>
5980
///Gets the character that represents the path separator.
6081
///</summary>

0 commit comments

Comments
 (0)