43
43
#ifdef SIMPLECPP_WINDOWS
44
44
#include < windows.h>
45
45
#undef ERROR
46
+ #else
47
+ #include < unistd.h>
46
48
#endif
47
49
48
50
#if __cplusplus >= 201103L
@@ -147,6 +149,11 @@ static unsigned long long stringToULL(const std::string &s)
147
149
return ret;
148
150
}
149
151
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
+
150
157
static bool endsWith (const std::string &s, const std::string &e)
151
158
{
152
159
return (s.size () >= e.size ()) && std::equal (e.rbegin (), e.rend (), s.rbegin ());
@@ -2681,6 +2688,46 @@ static bool isCpp17OrLater(const simplecpp::DUI &dui)
2681
2688
return !std_ver.empty () && (std_ver >= " 201703L" );
2682
2689
}
2683
2690
2691
+
2692
+ static std::string currentDirectoryOSCalc () {
2693
+ #ifdef SIMPLECPP_WINDOWS
2694
+ TCHAR NPath[MAX_PATH];
2695
+ GetCurrentDirectory (MAX_PATH, NPath);
2696
+ return NPath;
2697
+ #else
2698
+ const std::size_t size = 1024 ;
2699
+ char the_path[size];
2700
+ getcwd (the_path, size);
2701
+ return the_path;
2702
+ #endif
2703
+ }
2704
+
2705
+ static const std::string& currentDirectory () {
2706
+ static const std::string curdir = simplecpp::simplifyPath (currentDirectoryOSCalc ());
2707
+ return curdir;
2708
+ }
2709
+
2710
+ static std::string toAbsolutePath (const std::string& path) {
2711
+ if (path.empty ()) {
2712
+ return path;// preserve error file path that is indicated by an empty string
2713
+ }
2714
+ if (!isAbsolutePath (path)) {
2715
+ return currentDirectory () + " /" + path;
2716
+ }
2717
+ // otherwise
2718
+ return path;
2719
+ }
2720
+
2721
+ static std::pair<std::string, bool > extractRelativePathFromAbsolute (const std::string& absolutepath) {
2722
+ static const std::string prefix = currentDirectory () + " /" ;
2723
+ if (startsWith (absolutepath, prefix)) {
2724
+ const std::size_t size = prefix.size ();
2725
+ return std::make_pair (absolutepath.substr (size, absolutepath.size () - size), true );
2726
+ }
2727
+ // otherwise
2728
+ return std::make_pair (" " , false );
2729
+ }
2730
+
2684
2731
static std::string openHeader (std::ifstream &f, const simplecpp::DUI &dui, const std::string &sourcefile, const std::string &header, bool systemheader);
2685
2732
static void simplifyHasInclude (simplecpp::TokenList &expr, const simplecpp::DUI &dui)
2686
2733
{
@@ -3099,9 +3146,12 @@ static std::string openHeader(std::ifstream &f, const std::string &path)
3099
3146
3100
3147
static std::string getRelativeFileName (const std::string &sourcefile, const std::string &header)
3101
3148
{
3149
+ std::string path;
3102
3150
if (sourcefile.find_first_of (" \\ /" ) != std::string::npos)
3103
- return simplecpp::simplifyPath (sourcefile.substr (0 , sourcefile.find_last_of (" \\ /" ) + 1U ) + header);
3104
- return simplecpp::simplifyPath (header);
3151
+ path = sourcefile.substr (0 , sourcefile.find_last_of (" \\ /" ) + 1U ) + header;
3152
+ else
3153
+ path = header;
3154
+ return simplecpp::simplifyPath (path);
3105
3155
}
3106
3156
3107
3157
static std::string openHeaderRelative (std::ifstream &f, const std::string &sourcefile, const std::string &header)
@@ -3111,7 +3161,7 @@ static std::string openHeaderRelative(std::ifstream &f, const std::string &sourc
3111
3161
3112
3162
static std::string getIncludePathFileName (const std::string &includePath, const std::string &header)
3113
3163
{
3114
- std::string path = includePath;
3164
+ std::string path = toAbsolutePath ( includePath) ;
3115
3165
if (!path.empty () && path[path.size ()-1U ]!=' /' && path[path.size ()-1U ]!=' \\ ' )
3116
3166
path += ' /' ;
3117
3167
return path + header;
@@ -3120,9 +3170,9 @@ static std::string getIncludePathFileName(const std::string &includePath, const
3120
3170
static std::string openHeaderIncludePath (std::ifstream &f, const simplecpp::DUI &dui, const std::string &header)
3121
3171
{
3122
3172
for (std::list<std::string>::const_iterator it = dui.includePaths .begin (); it != dui.includePaths .end (); ++it) {
3123
- std::string simplePath = openHeader (f, getIncludePathFileName (*it, header));
3124
- if (!simplePath .empty ())
3125
- return simplePath ;
3173
+ std::string path = openHeader (f, getIncludePathFileName (*it, header));
3174
+ if (!path .empty ())
3175
+ return path ;
3126
3176
}
3127
3177
return " " ;
3128
3178
}
@@ -3132,49 +3182,76 @@ static std::string openHeader(std::ifstream &f, const simplecpp::DUI &dui, const
3132
3182
if (isAbsolutePath (header))
3133
3183
return openHeader (f, header);
3134
3184
3135
- std::string ret;
3136
-
3137
3185
if (systemheader) {
3138
- ret = openHeaderIncludePath (f, dui, header);
3139
- return ret ;
3186
+ // always return absolute path for systemheaders
3187
+ return toAbsolutePath ( openHeaderIncludePath (f, dui, header)) ;
3140
3188
}
3141
3189
3190
+ std::string ret;
3191
+
3142
3192
ret = openHeaderRelative (f, sourcefile, header);
3143
3193
if (ret.empty ())
3144
- return openHeaderIncludePath (f, dui, header);
3194
+ return toAbsolutePath ( openHeaderIncludePath (f, dui, header)); // in a similar way to system headers
3145
3195
return ret;
3146
3196
}
3147
3197
3148
- 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)
3198
+ static std::string findPathInMapBothRelativeAndAbsolute (const std::map<std::string, simplecpp::TokenList *> &filedata, const std::string& path) {
3199
+ // here there are two possibilities - either we match this from absolute path or from a relative one
3200
+ if (filedata.find (path) != filedata.end ()) {// try first to respect the exact match
3201
+ return path;
3202
+ }
3203
+ // otherwise - try to use the normalize to the correct representation
3204
+ if (isAbsolutePath (path)) {
3205
+ const std::pair<std::string, bool > relativeExtractedResult = extractRelativePathFromAbsolute (path);
3206
+ if (relativeExtractedResult.second ) {
3207
+ const std::string relativePath = relativeExtractedResult.first ;
3208
+ if (filedata.find (relativePath) != filedata.end ()) {
3209
+ return relativePath;
3210
+ }
3211
+ }
3212
+ } else {
3213
+ const std::string absolutePath = toAbsolutePath (path);
3214
+ if (filedata.find (absolutePath) != filedata.end ())
3215
+ return absolutePath;
3216
+ }
3217
+ // otherwise
3218
+ return " " ;
3219
+ }
3220
+
3221
+ 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)
3149
3222
{
3150
3223
if (filedata.empty ()) {
3151
3224
return " " ;
3152
3225
}
3153
3226
if (isAbsolutePath (header)) {
3154
- return (filedata.find (header) != filedata.end ()) ? simplecpp::simplifyPath (header) : " " ;
3227
+ const std::string simplifiedHeaderPath = simplecpp::simplifyPath (header);
3228
+ return (filedata.find (simplifiedHeaderPath) != filedata.end ()) ? simplifiedHeaderPath : " " ;
3155
3229
}
3156
3230
3157
3231
if (!systemheader) {
3158
- const std::string relativeFilename = getRelativeFileName (sourcefile, header);
3159
- if (filedata.find (relativeFilename) != filedata.end ())
3160
- return relativeFilename;
3232
+ const std::string relativeOrAbsoluteFilename = getRelativeFileName (sourcefile, header);// unknown if absolute or relative, but always simplified
3233
+ const std::string match = findPathInMapBothRelativeAndAbsolute (filedata, relativeOrAbsoluteFilename);
3234
+ if (!match.empty ()) {
3235
+ return match;
3236
+ }
3161
3237
}
3162
3238
3163
3239
for (std::list<std::string>::const_iterator it = dui.includePaths .begin (); it != dui.includePaths .end (); ++it) {
3164
- std::string s = simplecpp::simplifyPath (getIncludePathFileName (*it, header));
3165
- if (filedata.find (s) != filedata.end ())
3166
- return s;
3240
+ const std::string match = findPathInMapBothRelativeAndAbsolute (filedata, simplecpp::simplifyPath (getIncludePathFileName (*it, header)));
3241
+ if (!match.empty ()) {
3242
+ return match;
3243
+ }
3167
3244
}
3168
3245
3169
3246
if (systemheader && filedata.find (header) != filedata.end ())
3170
- return header;
3247
+ return header;// system header that its file wasn't found in the included paths but alreasy in the filedata - return this as is
3171
3248
3172
3249
return " " ;
3173
3250
}
3174
3251
3175
3252
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)
3176
3253
{
3177
- return !getFileName (filedata, sourcefile, header, dui, systemheader).empty ();
3254
+ return !getFileIdPath (filedata, sourcefile, header, dui, systemheader).empty ();
3178
3255
}
3179
3256
3180
3257
std::map<std::string, simplecpp::TokenList*> simplecpp::load (const simplecpp::TokenList &rawtokens, std::vector<std::string> &filenames, const simplecpp::DUI &dui, simplecpp::OutputList *outputList)
@@ -3530,7 +3607,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
3530
3607
3531
3608
const bool systemheader = (inctok->str ()[0 ] == ' <' );
3532
3609
const std::string header (realFilename (inctok->str ().substr (1U , inctok->str ().size () - 2U )));
3533
- std::string header2 = getFileName (filedata, rawtok->location .file (), header, dui, systemheader);
3610
+ std::string header2 = getFileIdPath (filedata, rawtok->location .file (), header, dui, systemheader);
3534
3611
if (header2.empty ()) {
3535
3612
// try to load file..
3536
3613
std::ifstream f;
0 commit comments