X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=laserbrain_demo;a=blobdiff_plain;f=src%2Fdatamap.cc;h=dcbae788b0cc2fe3d7b0ea6f8b0154a701ceab86;hp=19aaf44c95e788ef85f42fe753dd708810d55c5a;hb=6ef619c6d92c698728576a4ec1c798a0f716d9a4;hpb=572bf1ef8d54ef5a7cba7cdf38515cb16c4c312c diff --git a/src/datamap.cc b/src/datamap.cc index 19aaf44..dcbae78 100644 --- a/src/datamap.cc +++ b/src/datamap.cc @@ -1,13 +1,10 @@ #include #include #include -#include -#include -#include -//#include +#include #include "datamap.h" -#ifdef WIN32 +#if defined(WIN32) || defined(__WIN32__) #include #else #include @@ -15,23 +12,28 @@ static char *clean_line(char *s); -//static std::vector> dmap; -static std::vector> dmap; -static std::map cache; -static std::string root; +DataMap::DataMap() +{ + strip_paths = false; +} -void datamap_reset() +void DataMap::clear() { dmap.clear(); cache.clear(); } -void datamap_set_path(const char *path) +void DataMap::set_path(const char *path) { root = std::string(path); } -bool datamap_load_map(const char *fname) +void DataMap::set_strip(bool s) +{ + strip_paths = s; +} + +bool DataMap::load_map(const char *fname) { std::string path = root.empty() ? fname : root + std::string("/") + fname; fname = path.c_str(); @@ -49,7 +51,7 @@ bool datamap_load_map(const char *fname) return false; } - datamap_reset(); + clear(); char *line; int nline = 0; @@ -83,25 +85,30 @@ bool datamap_load_map(const char *fname) err: fprintf(stderr, "error while parsing %s, invalid line %d: %s\n", fname, nline, line); - datamap_reset(); + clear(); fclose(fp); return false; } -void datamap_map(const char *re, const char *path) +void DataMap::map(const char *match, const char *path) { - //std::pair mapping; - //mapping.first = std::regex(re); std::pair mapping; - mapping.first = std::string(re); + mapping.first = std::string(match); mapping.second = std::string(path); dmap.push_back(std::move(mapping)); } -int datamap_lookup(const char *in, char *buf, int bsz) +int DataMap::lookup(const char *in, char *buf, int bsz) const { std::string res; + if(strip_paths) { + const char *ptr = strrchr(in, '/'); + if(ptr) { + in = ptr + 1; + } + } + char *inbuf = (char*)alloca(strlen(in) + 1); strcpy(inbuf, in); in = clean_line(inbuf); @@ -112,7 +119,7 @@ int datamap_lookup(const char *in, char *buf, int bsz) res = it->second; } else { // try matching with the available mappings - res = std::string(in); + res = root.empty() ? std::string(in) : root + "/" + std::string(in); int num = dmap.size(); for(int i=0; i