cleanup (a bit)
This commit is contained in:
parent
8e15870aa6
commit
4382ad6d59
100
main.cpp
100
main.cpp
@ -7,16 +7,17 @@
|
|||||||
|
|
||||||
#include <sys/mman.h> // for mmap()
|
#include <sys/mman.h> // for mmap()
|
||||||
#include <sys/stat.h> // for fstat()
|
#include <sys/stat.h> // for fstat()
|
||||||
#include <fcntl.h> // for open()
|
#include <fcntl.h> // for open(), O_RDONLY
|
||||||
#include <unistd.h> // for close()
|
#include <unistd.h> // for close()
|
||||||
#include <errno.h> // for perror()
|
#include <errno.h> // for perror()
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstring>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <set>
|
#include <set>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
#include "fs.hpp"
|
#include "fs.hpp"
|
||||||
#include "memory_string.hpp"
|
#include "memory_string.hpp"
|
||||||
@ -60,8 +61,6 @@ std::string readTill(iterator &start, const iterator &end, std::function<bool(co
|
|||||||
|
|
||||||
template <typename iterator>
|
template <typename iterator>
|
||||||
std::string readBrackets(iterator ¤t, const iterator &end, const char * brackets) {
|
std::string readBrackets(iterator ¤t, const iterator &end, const char * brackets) {
|
||||||
// auto current = begin;
|
|
||||||
|
|
||||||
if (current == end || *current != brackets[0]) {
|
if (current == end || *current != brackets[0]) {
|
||||||
std::cout << brackets[0] << "!=" << *current;
|
std::cout << brackets[0] << "!=" << *current;
|
||||||
return std::string();
|
return std::string();
|
||||||
@ -122,24 +121,9 @@ std::string InputExtractor::macroExpand(const std::string &input) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <functional>
|
|
||||||
typedef std::map<std::string, std::function<void(InputExtractor::List&, std::string)>> CommandList;
|
typedef std::map<std::string, std::function<void(InputExtractor::List&, std::string)>> CommandList;
|
||||||
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
|
|
||||||
#include <fstream>
|
|
||||||
|
|
||||||
bool Exists(std::string path) {
|
|
||||||
std::ifstream file(path);
|
|
||||||
if (!file) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
file.close();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
InputExtractor::List InputExtractor::Include(Path::Path path) {
|
InputExtractor::List InputExtractor::Include(Path::Path path) {
|
||||||
path = Path::Clean(path);
|
path = Path::Clean(path);
|
||||||
List list;
|
List list;
|
||||||
@ -178,11 +162,13 @@ InputExtractor::List InputExtractor::Include(Path::Path path) {
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Substring str((const char *)memptr, (const char*)memptr + fileinfo.st_size);
|
// create iteratable memory object
|
||||||
MemoryString file((char*)memptr, fileinfo.st_size);
|
MemoryString file((char*)memptr, fileinfo.st_size);
|
||||||
|
|
||||||
std::string basedir = Path::Dir(path);
|
std::string basedir = Path::Dir(path);
|
||||||
list = (*this)(basedir, file); // follow include
|
|
||||||
|
// evaluate memory region (aka the file)
|
||||||
|
list = (*this)(basedir, file);
|
||||||
|
|
||||||
// cleanup
|
// cleanup
|
||||||
munmap(memptr, fileinfo.st_size);
|
munmap(memptr, fileinfo.st_size);
|
||||||
@ -192,6 +178,8 @@ InputExtractor::List InputExtractor::Include(Path::Path path) {
|
|||||||
cout << path << "done" << endl;
|
cout << path << "done" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// make a relative filepath absolute using the root filename
|
||||||
|
// and/or the process working directory
|
||||||
Path::Path fixFilename(const Path::Path &file, const Path::Path &root) {
|
Path::Path fixFilename(const Path::Path &file, const Path::Path &root) {
|
||||||
Path::Path temp(file);
|
Path::Path temp(file);
|
||||||
|
|
||||||
@ -208,7 +196,7 @@ Path::Path fixFilename(const Path::Path &file, const Path::Path &root) {
|
|||||||
return Path::Clean(temp);
|
return Path::Clean(temp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// evaluate a tex file searching for input statements
|
||||||
InputExtractor::List InputExtractor::operator()(const Path::Path &file, const MemoryString &str){
|
InputExtractor::List InputExtractor::operator()(const Path::Path &file, const MemoryString &str){
|
||||||
List result;
|
List result;
|
||||||
CommandList IncludeCommands;
|
CommandList IncludeCommands;
|
||||||
@ -324,72 +312,6 @@ int main(int argc, char ** args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cout << "done" << endl;
|
cout << "done" << endl;
|
||||||
|
|
||||||
/*
|
|
||||||
char * filename = args[argc-1];
|
|
||||||
cout << "opening " << filename << "...";
|
|
||||||
|
|
||||||
// try to open file
|
|
||||||
fd = open(filename, O_RDONLY);
|
|
||||||
if (fd == -1) {
|
|
||||||
perror("could not open input file");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
fstat(fd, &filestat);
|
|
||||||
//cout << "file size: " << filestat.st_size << endl;
|
|
||||||
|
|
||||||
// try to mmap file
|
|
||||||
void * memory_area = mmap(NULL, filestat.st_size, PROT_READ, MAP_SHARED, fd, 0);
|
|
||||||
if (memory_area == nullptr) {
|
|
||||||
perror("could not mmap the input");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
cout << "start parsing" << endl;
|
|
||||||
|
|
||||||
MemoryString file((char*)memory_area, filestat.st_size);
|
|
||||||
|
|
||||||
try {
|
|
||||||
InputExtractor::List list = InputExtractor()(file);
|
|
||||||
|
|
||||||
Path::Path outfilename = Path::Basename(Path::Path(filename)) + ".d";
|
|
||||||
|
|
||||||
cout << "writing makedeps file to " << outfilename << "..." << endl;
|
|
||||||
|
|
||||||
// write in makefile style
|
|
||||||
std::ofstream output(outfilename);
|
|
||||||
if (!output) {
|
|
||||||
std::cout << "could not create output file" << std::endl;
|
|
||||||
} else {
|
|
||||||
output << filename << ": ";
|
|
||||||
|
|
||||||
for (auto it = list.begin(); it != list.end(); it++) {
|
|
||||||
if (Path::isRelative(*it)) {
|
|
||||||
if (Path::isRelative(filename)) {
|
|
||||||
*it = Path::Join(fs::cwd(), filename, *it);
|
|
||||||
} else {
|
|
||||||
*it = Path::Join(Path::Path(filename), *it);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cout << "depends: " << *it << endl;
|
|
||||||
output << '\t' << *it << "\t\\\n";
|
|
||||||
}
|
|
||||||
output << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
cout << filename << done;
|
|
||||||
|
|
||||||
} catch(InputExtractor::Exception &e) {
|
|
||||||
cout << e.what() << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
// cleanup
|
|
||||||
munmap(memory_area, filestat.st_size);
|
|
||||||
close(fd);
|
|
||||||
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user