gKit3
Loading...
Searching...
No Matches
files.cpp
1
2#ifndef _MSC_VER
3 #include <sys/stat.h>
4#else
5 #include <sys/types.h>
6 #include <sys/stat.h>
7#endif
8
9#include <string>
10#include <algorithm>
11
12#include "files.h"
13
14
16bool exists( const std::string& filename )
17{
18#ifndef _MSC_VER
19 struct stat info;
20 if(stat(filename.c_str(), &info) < 0)
21 return false;
22
23 // verifie aussi que c'est bien un fichier standard
24 return S_ISREG(info.st_mode);
25
26#else
27 struct _stat64 info;
28 if(_stat64(filename.c_str(), &info) < 0)
29 return false;
30
31 // verifie aussi que c'est bien un fichier standard
32 return (info.st_mode & _S_IFREG);
33#endif
34}
35
37size_t timestamp( const std::string& filename )
38{
39#ifndef _MSC_VER
40 struct stat info;
41 if(stat(filename.c_str(), &info) < 0)
42 return 0;
43
44 // verifie aussi que c'est bien un fichier standard
45 if(S_ISREG(info.st_mode))
46 return size_t(info.st_mtime);
47
48#else
49 struct _stat64 info;
50 if(_stat64(filename.c_str(), &info) < 0)
51 return 0;
52
53 // verifie aussi que c'est bien un fichier standard
54 if(info.st_mode & _S_IFREG)
55 return size_t(info.st_mtime);
56#endif
57
58 return 0;
59}
60
61
68std::string pathname( const std::string& filename )
69{
70 std::string path= filename;
71#ifndef WIN32
72 std::replace(path.begin(), path.end(), '\\', '/'); // linux, macos : remplace les \ par /.
73 size_t slash = path.find_last_of( '/' );
74 if(slash != std::string::npos)
75 return path.substr(0, slash +1); // inclus le slash
76 else
77 return "./";
78#else
79 std::replace(path.begin(), path.end(), '/', '\\'); // windows : remplace les / par \.
80 size_t slash = path.find_last_of( '\\' );
81 if(slash != std::string::npos)
82 return path.substr(0, slash +1); // inclus le slash
83 else
84 return ".\\";
85#endif
86}
87
89std::string normalize_filename( const std::string& filename )
90{
91 std::string path= filename;
92#ifndef WIN32
93 std::replace(path.begin(), path.end(), '\\', '/'); // linux, macos : remplace les \ par /.
94#else
95 std::replace(path.begin(), path.end(), '/', '\\'); // windows : remplace les / par \.
96#endif
97
98 return path;
99}
100
101
107//~ const char *relative_filename( const std::string& filename, const std::string& path )
108std::string relative_filename( const std::string& filename, const std::string& path )
109{
110 unsigned i= 0;
111 while(i< filename.length() && i < path.length() && filename[i] == path[i])
112 i++;
113
114 return filename.substr(i);
115}
116
117
118std::string absolute_filename( const std::string& path, const std::string& filename )
119{
120 if(filename[0] == '.' || filename[0] == '/')
121 return normalize_filename(filename);
122 else
123 return normalize_filename(path + filename);
124}