semi-ported to msys2
[laserbrain_demo] / src / datamap.cc
index 626c47f..dcbae78 100644 (file)
@@ -1,13 +1,10 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <vector>
-#include <map>
-#include <string>
-#include <regex>
+#include <algorithm>
 #include "datamap.h"
 
-#ifdef WIN32
+#if defined(WIN32) || defined(__WIN32__)
 #include <malloc.h>
 #else
 #include <alloca.h>
 
 static char *clean_line(char *s);
 
-static std::vector<std::pair<std::regex, std::string>> dmap;
-static std::map<std::string, std::string> cache;
-static std::string root;
+DataMap::DataMap()
+{
+       strip_paths = false;
+}
 
-void datamap_reset()
+void DataMap::clear()
 {
-       root.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;
@@ -64,8 +66,10 @@ bool datamap_load_map(const char *fname)
                }
                *colon = 0;
 
-               std::pair<std::regex, std::string> pair;
-               pair.first = std::regex(line);
+               //std::pair<std::regex, std::string> pair;
+               //pair.first = std::regex(line);
+               std::pair<std::string, std::string> pair;
+               pair.first = std::string(line);
 
                char *value = clean_line(colon + 1);
                if(!value || !*value) {
@@ -81,23 +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<std::regex, std::string> mapping;
-       mapping.first = std::regex(re);
+       std::pair<std::string, std::string> mapping;
+       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);
@@ -108,15 +119,17 @@ int datamap_lookup(const char *in, char *buf, int bsz)
                res = it->second;
        } else {
                // try matching with the available mappings
+               res = root.empty() ? std::string(in) : root + "/" + std::string(in);
+
                int num = dmap.size();
                for(int i=0; i<num; i++) {
-                       if(std::regex_match(in, dmap[i].first)) {
+                       //if(std::regex_search(in, dmap[i].first)) {
+                       if(strstr(in, dmap[i].first.c_str())) {
                                res = root.empty() ? dmap[i].second : root + "/" + dmap[i].second;
                                cache[in] = res;        // add it to the cache
                                break;
                        }
                }
-               return 0;
        }
 
        // copy result in buf, truncating if necessary and return the size of the
@@ -124,14 +137,14 @@ int datamap_lookup(const char *in, char *buf, int bsz)
        if(buf) {
                int n = std::min(bsz - 1, (int)res.length());
                memcpy(buf, res.c_str(), n);
-               buf[bsz - 1] = 0; // make sure it's null-terminated even if it got truncated
+               buf[n] = 0; // make sure it's null-terminated even if it got truncated
        }
        return res.length() + 1;
 }
 
-int datamap_path_size(const char *in)
+int DataMap::path_size(const char *in) const
 {
-       return datamap_lookup(in, 0, 0);
+       return lookup(in, 0, 0);
 }
 
 static char *clean_line(char *s)