add cwd() function and fix some bugs

This commit is contained in:
Julian Daube 2017-10-08 03:24:20 +02:00
parent e288c12ade
commit 0311fd91ae
2 changed files with 52 additions and 6 deletions

View File

@ -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:

View File

@ -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 <unistd.h>
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 <fstream>
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 <experimental/filesystem>
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;
}