43
43
#ifdef SIMPLECPP_WINDOWS
44
44
#include < windows.h>
45
45
#undef ERROR
46
- #else
47
- #include < unistd.h>
48
46
#endif
49
47
50
48
#if __cplusplus >= 201103L
@@ -149,11 +147,6 @@ static unsigned long long stringToULL(const std::string &s)
149
147
return ret;
150
148
}
151
149
152
- static bool startsWith (const std::string &s, const std::string &p)
153
- {
154
- return (s.size () >= p.size ()) && std::equal (p.begin (), p.end (), s.begin ());
155
- }
156
-
157
150
static bool endsWith (const std::string &s, const std::string &e)
158
151
{
159
152
return (s.size () >= e.size ()) && std::equal (e.rbegin (), e.rend (), s.rbegin ());
@@ -2718,46 +2711,6 @@ static bool isCpp17OrLater(const simplecpp::DUI &dui)
2718
2711
return !std_ver.empty () && (std_ver >= " 201703L" );
2719
2712
}
2720
2713
2721
-
2722
- static std::string currentDirectoryOSCalc () {
2723
- #ifdef SIMPLECPP_WINDOWS
2724
- TCHAR NPath[MAX_PATH];
2725
- GetCurrentDirectory (MAX_PATH, NPath);
2726
- return NPath;
2727
- #else
2728
- const std::size_t size = 1024 ;
2729
- char the_path[size];
2730
- getcwd (the_path, size);
2731
- return the_path;
2732
- #endif
2733
- }
2734
-
2735
- static const std::string& currentDirectory () {
2736
- static const std::string curdir = simplecpp::simplifyPath (currentDirectoryOSCalc ());
2737
- return curdir;
2738
- }
2739
-
2740
- static std::string toAbsolutePath (const std::string& path) {
2741
- if (path.empty ()) {
2742
- return path;// preserve error file path that is indicated by an empty string
2743
- }
2744
- if (!isAbsolutePath (path)) {
2745
- return currentDirectory () + " /" + path;
2746
- }
2747
- // otherwise
2748
- return path;
2749
- }
2750
-
2751
- static std::pair<std::string, bool > extractRelativePathFromAbsolute (const std::string& absolutepath) {
2752
- static const std::string prefix = currentDirectory () + " /" ;
2753
- if (startsWith (absolutepath, prefix)) {
2754
- const std::size_t size = prefix.size ();
2755
- return std::make_pair (absolutepath.substr (size, absolutepath.size () - size), true );
2756
- }
2757
- // otherwise
2758
- return std::make_pair (" " , false );
2759
- }
2760
-
2761
2714
static std::string openHeader (std::ifstream &f, const simplecpp::DUI &dui, const std::string &sourcefile, const std::string &header, bool systemheader);
2762
2715
static void simplifyHasInclude (simplecpp::TokenList &expr, const simplecpp::DUI &dui)
2763
2716
{
@@ -3176,12 +3129,9 @@ static std::string openHeader(std::ifstream &f, const std::string &path)
3176
3129
3177
3130
static std::string getRelativeFileName (const std::string &sourcefile, const std::string &header)
3178
3131
{
3179
- std::string path;
3180
3132
if (sourcefile.find_first_of (" \\ /" ) != std::string::npos)
3181
- path = sourcefile.substr (0 , sourcefile.find_last_of (" \\ /" ) + 1U ) + header;
3182
- else
3183
- path = header;
3184
- return simplecpp::simplifyPath (path);
3133
+ return simplecpp::simplifyPath (sourcefile.substr (0 , sourcefile.find_last_of (" \\ /" ) + 1U ) + header);
3134
+ return simplecpp::simplifyPath (header);
3185
3135
}
3186
3136
3187
3137
static std::string openHeaderRelative (std::ifstream &f, const std::string &sourcefile, const std::string &header)
@@ -3191,7 +3141,7 @@ static std::string openHeaderRelative(std::ifstream &f, const std::string &sourc
3191
3141
3192
3142
static std::string getIncludePathFileName (const std::string &includePath, const std::string &header)
3193
3143
{
3194
- std::string path = toAbsolutePath ( includePath) ;
3144
+ std::string path = includePath;
3195
3145
if (!path.empty () && path[path.size ()-1U ]!=' /' && path[path.size ()-1U ]!=' \\ ' )
3196
3146
path += ' /' ;
3197
3147
return path + header;
@@ -3200,9 +3150,9 @@ static std::string getIncludePathFileName(const std::string &includePath, const
3200
3150
static std::string openHeaderIncludePath (std::ifstream &f, const simplecpp::DUI &dui, const std::string &header)
3201
3151
{
3202
3152
for (std::list<std::string>::const_iterator it = dui.includePaths .begin (); it != dui.includePaths .end (); ++it) {
3203
- std::string path = openHeader (f, getIncludePathFileName (*it, header));
3204
- if (!path .empty ())
3205
- return path ;
3153
+ std::string simplePath = openHeader (f, getIncludePathFileName (*it, header));
3154
+ if (!simplePath .empty ())
3155
+ return simplePath ;
3206
3156
}
3207
3157
return " " ;
3208
3158
}
@@ -3212,76 +3162,49 @@ static std::string openHeader(std::ifstream &f, const simplecpp::DUI &dui, const
3212
3162
if (isAbsolutePath (header))
3213
3163
return openHeader (f, header);
3214
3164
3165
+ std::string ret;
3166
+
3215
3167
if (systemheader) {
3216
- // always return absolute path for systemheaders
3217
- return toAbsolutePath ( openHeaderIncludePath (f, dui, header)) ;
3168
+ ret = openHeaderIncludePath (f, dui, header);
3169
+ return ret ;
3218
3170
}
3219
3171
3220
- std::string ret;
3221
-
3222
3172
ret = openHeaderRelative (f, sourcefile, header);
3223
3173
if (ret.empty ())
3224
- return toAbsolutePath ( openHeaderIncludePath (f, dui, header)); // in a similar way to system headers
3174
+ return openHeaderIncludePath (f, dui, header);
3225
3175
return ret;
3226
3176
}
3227
3177
3228
- static std::string findPathInMapBothRelativeAndAbsolute (const std::map<std::string, simplecpp::TokenList *> &filedata, const std::string& path) {
3229
- // here there are two possibilities - either we match this from absolute path or from a relative one
3230
- if (filedata.find (path) != filedata.end ()) {// try first to respect the exact match
3231
- return path;
3232
- }
3233
- // otherwise - try to use the normalize to the correct representation
3234
- if (isAbsolutePath (path)) {
3235
- const std::pair<std::string, bool > relativeExtractedResult = extractRelativePathFromAbsolute (path);
3236
- if (relativeExtractedResult.second ) {
3237
- const std::string relativePath = relativeExtractedResult.first ;
3238
- if (filedata.find (relativePath) != filedata.end ()) {
3239
- return relativePath;
3240
- }
3241
- }
3242
- } else {
3243
- const std::string absolutePath = toAbsolutePath (path);
3244
- if (filedata.find (absolutePath) != filedata.end ())
3245
- return absolutePath;
3246
- }
3247
- // otherwise
3248
- return " " ;
3249
- }
3250
-
3251
- static std::string getFileIdPath (const std::map<std::string, simplecpp::TokenList *> &filedata, const std::string &sourcefile, const std::string &header, const simplecpp::DUI &dui, bool systemheader)
3178
+ static std::string getFileName (const std::map<std::string, simplecpp::TokenList *> &filedata, const std::string &sourcefile, const std::string &header, const simplecpp::DUI &dui, bool systemheader)
3252
3179
{
3253
3180
if (filedata.empty ()) {
3254
3181
return " " ;
3255
3182
}
3256
3183
if (isAbsolutePath (header)) {
3257
- const std::string simplifiedHeaderPath = simplecpp::simplifyPath (header);
3258
- return (filedata.find (simplifiedHeaderPath) != filedata.end ()) ? simplifiedHeaderPath : " " ;
3184
+ return (filedata.find (header) != filedata.end ()) ? simplecpp::simplifyPath (header) : " " ;
3259
3185
}
3260
3186
3261
3187
if (!systemheader) {
3262
- const std::string relativeOrAbsoluteFilename = getRelativeFileName (sourcefile, header);// unknown if absolute or relative, but always simplified
3263
- const std::string match = findPathInMapBothRelativeAndAbsolute (filedata, relativeOrAbsoluteFilename);
3264
- if (!match.empty ()) {
3265
- return match;
3266
- }
3188
+ const std::string relativeFilename = getRelativeFileName (sourcefile, header);
3189
+ if (filedata.find (relativeFilename) != filedata.end ())
3190
+ return relativeFilename;
3267
3191
}
3268
3192
3269
3193
for (std::list<std::string>::const_iterator it = dui.includePaths .begin (); it != dui.includePaths .end (); ++it) {
3270
- const std::string match = findPathInMapBothRelativeAndAbsolute (filedata, simplecpp::simplifyPath (getIncludePathFileName (*it, header)));
3271
- if (!match.empty ()) {
3272
- return match;
3273
- }
3194
+ std::string s = simplecpp::simplifyPath (getIncludePathFileName (*it, header));
3195
+ if (filedata.find (s) != filedata.end ())
3196
+ return s;
3274
3197
}
3275
3198
3276
3199
if (systemheader && filedata.find (header) != filedata.end ())
3277
- return header;// system header that its file wasn't found in the included paths but alreasy in the filedata - return this as is
3200
+ return header;
3278
3201
3279
3202
return " " ;
3280
3203
}
3281
3204
3282
3205
static bool hasFile (const std::map<std::string, simplecpp::TokenList *> &filedata, const std::string &sourcefile, const std::string &header, const simplecpp::DUI &dui, bool systemheader)
3283
3206
{
3284
- return !getFileIdPath (filedata, sourcefile, header, dui, systemheader).empty ();
3207
+ return !getFileName (filedata, sourcefile, header, dui, systemheader).empty ();
3285
3208
}
3286
3209
3287
3210
std::map<std::string, simplecpp::TokenList*> simplecpp::load (const simplecpp::TokenList &rawtokens, std::vector<std::string> &filenames, const simplecpp::DUI &dui, simplecpp::OutputList *outputList)
@@ -3637,7 +3560,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
3637
3560
3638
3561
const bool systemheader = (inctok->str ()[0 ] == ' <' );
3639
3562
const std::string header (realFilename (inctok->str ().substr (1U , inctok->str ().size () - 2U )));
3640
- std::string header2 = getFileIdPath (filedata, rawtok->location .file (), header, dui, systemheader);
3563
+ std::string header2 = getFileName (filedata, rawtok->location .file (), header, dui, systemheader);
3641
3564
if (header2.empty ()) {
3642
3565
// try to load file..
3643
3566
std::ifstream f;
0 commit comments