add cwd() function and fix some bugs
This commit is contained in:
parent
e288c12ade
commit
0311fd91ae
2
Makefile
2
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:
|
||||
|
56
main.cpp
56
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 <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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user