]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/sourcemap.cpp
Minor cleanups and tweaks
[libs/gl.git] / source / glsl / sourcemap.cpp
1 #include <msp/strings/format.h>
2 #include <msp/strings/regex.h>
3 #include <msp/strings/utils.h>
4 #include "sourcemap.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10 namespace SL {
11
12 void SourceMap::set_name(unsigned i, const string &n)
13 {
14         if(source_names.empty())
15                 base_index = i;
16         else if(i<base_index)
17         {
18                 source_names.insert(source_names.begin(), base_index-i, string());
19                 base_index = i;
20         }
21         i -= base_index;
22
23         if(source_names.size()<=i)
24                 source_names.resize(i+1);
25         source_names[i] = n;
26 }
27
28 const string &SourceMap::get_name(unsigned i) const
29 {
30         i -= base_index;
31         if(i<source_names.size())
32                 return source_names[i];
33
34         static string empty;
35         return empty;
36 }
37
38 void SourceMap::merge_from(const SourceMap &other)
39 {
40         if(other.base_index<base_index)
41         {
42                 source_names.insert(source_names.begin(), base_index-other.base_index, string());
43                 base_index = other.base_index;
44         }
45         if(get_count()<other.get_count())
46                 source_names.resize(other.get_count()-base_index);
47
48         for(unsigned i=0; i<other.source_names.size(); ++i)
49                 if(!other.source_names[i].empty())
50                         source_names[i+other.base_index-base_index] = other.source_names[i];
51 }
52
53 string SourceMap::translate_errors(const string &errors) const
54 {
55         static const Regex r_message("^(([0-9]+)\\(([0-9]+)\\) :|ERROR: ([0-9]+):([0-9]+):) (.*)$");
56         vector<string> lines = split(errors, '\n');
57         string translated;
58         for(const string &l: lines)
59         {
60                 RegMatch m = r_message.match(l);
61                 if(m)
62                 {
63                         unsigned index = 0;
64                         unsigned line = 0;
65                         if(m[2])
66                         {
67                                 index = lexical_cast<unsigned>(m[2].str);
68                                 line = lexical_cast<unsigned>(m[3].str);
69                         }
70                         else if(m[4])
71                         {
72                                 index = lexical_cast<unsigned>(m[4].str);
73                                 line = lexical_cast<unsigned>(m[5].str);
74                         }
75                         const char *src = "<unknown>";
76                         if(index<source_names.size() && !source_names[index].empty())
77                                 src = source_names[index].c_str();
78                         translated += format("%s:%d: %s", src, line, m[6].str);
79                 }
80                 else
81                         translated += l;
82                 translated += '\n';
83         }
84
85         return translated;
86 }
87
88 } // namespace SL
89 } // namespace GL
90 } // namespace Msp