diff --git a/Makefile b/Makefile index 352da1c..33f298d 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ OUTPUT:= preparse all test: debug ./$(OUTPUT) test.tex -debug: CXXFLAGS:= -g +debug: CXXFLAGS:= -g -std=c++11 -O0 debug: $(OUTPUT) clean: diff --git a/main.cpp b/main.cpp index 7031b9b..8b2e6d3 100644 --- a/main.cpp +++ b/main.cpp @@ -222,7 +222,7 @@ std::string Basedir(std::string path) { it--; } - return std::string(path.begin(), it+1); + return std::string(path.begin(), it); } std::string Name(std::string path) { @@ -242,9 +242,39 @@ std::string Basename(std::string path) { it--; } + if (it == temp.begin()) { + return path; + } + return std::string(temp.begin(), it); } +inline bool PathRelative(std::string path) { + return path.size() && path[0] != '/'; +} + +#include + +std::string cwd() { + ssize_t size = 100, nsize; + + while(1) { + char buffer[size]; + if ((nsize = readlink("/proc/self/cwd", buffer, size)) < size) { + buffer[size+1] = 0; + return std::string(buffer); + } + size = nsize + 100; + } + +// +// buffer[rsize+1] = 0; +// +// std::string result; +// result.assign(buffer); +// return result; +} + #include bool Exists(std::string path) { @@ -285,9 +315,11 @@ InputExtractor::List Include(std::string path) { std::string basedir = Basedir(path); list = InputExtractor()(str); - // add basedir to list + + // add basedir to list for all relative paths for (auto it = list.begin(); it != list.end(); it++) { - *it = basedir + '/' + *it; + if (PathRelative(*it)) + *it = basedir + '/' + *it; } // cleanup munmap(memptr, fileinfo.st_size); @@ -368,9 +400,11 @@ InputExtractor::List InputExtractor::operator()(const Substring &input){ return result; } +#include + int main(int argc, char ** args) { // find all the files the given tex files depend on - + cout << cwd() << std::endl; int fd = 0; struct stat filestat; @@ -407,8 +441,20 @@ int main(int argc, char ** args) { std::cout << "could not create output file" << std::endl; } else { output << "filename: "; + for (auto it = list.begin(); it != list.end(); it++) { - output << *it << "\\\\\n"; + output << '\t'; + if (PathRelative(*it)) { + if (PathRelative(filename)) { + output << cwd(); + } else { + output << Basename(filename); + } + output << "/" << *it; + } else { + output << *it; + } + output << "\\\\\n"; } output << endl; }