1 | /* |
---|
2 | * Copyright (c) 2011, Chris Johns <chrisj@rtems.org> |
---|
3 | * |
---|
4 | * Permission to use, copy, modify, and/or distribute this software for any |
---|
5 | * purpose with or without fee is hereby granted, provided that the above |
---|
6 | * copyright notice and this permission notice appear in all copies. |
---|
7 | * |
---|
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
---|
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
---|
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
---|
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
---|
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
---|
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
---|
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
15 | */ |
---|
16 | |
---|
17 | #include <algorithm> |
---|
18 | #include <fstream> |
---|
19 | #include <string> |
---|
20 | |
---|
21 | #include <pkgconfig.h> |
---|
22 | |
---|
23 | namespace pkgconfig |
---|
24 | { |
---|
25 | void tolower (std::string& str) |
---|
26 | { |
---|
27 | std::transform (str.begin (), str.end (), str.begin (), ::tolower); |
---|
28 | } |
---|
29 | |
---|
30 | package::package (void) |
---|
31 | { |
---|
32 | } |
---|
33 | |
---|
34 | void |
---|
35 | package::load (const std::string& name) |
---|
36 | { |
---|
37 | std::ifstream in (name.c_str (), std::ios::in); |
---|
38 | |
---|
39 | while (!in.eof ()) |
---|
40 | { |
---|
41 | char buffer[1024]; |
---|
42 | |
---|
43 | in.getline (buffer, sizeof (buffer)); |
---|
44 | |
---|
45 | std::string line (buffer); |
---|
46 | size_t hash; |
---|
47 | |
---|
48 | hash = line.find ('#'); |
---|
49 | if (hash != std::string::npos) |
---|
50 | line.erase(hash); |
---|
51 | |
---|
52 | if (line.size () > 0) |
---|
53 | { |
---|
54 | size_t eq = line.find_first_of ('='); |
---|
55 | size_t dd = line.find_first_of (':'); |
---|
56 | |
---|
57 | size_t d = std::string::npos; |
---|
58 | bool def = false; |
---|
59 | |
---|
60 | if ((eq != std::string::npos) && (dd != std::string::npos)) |
---|
61 | { |
---|
62 | if (eq < dd) |
---|
63 | { |
---|
64 | d = eq; |
---|
65 | def = true; |
---|
66 | } |
---|
67 | else |
---|
68 | { |
---|
69 | d = dd; |
---|
70 | def = false; |
---|
71 | } |
---|
72 | } |
---|
73 | else if (eq != std::string::npos) |
---|
74 | { |
---|
75 | d = eq; |
---|
76 | def = true; |
---|
77 | } |
---|
78 | else if (dd != std::string::npos) |
---|
79 | { |
---|
80 | d = dd; |
---|
81 | def = false; |
---|
82 | } |
---|
83 | |
---|
84 | if (d != std::string::npos) |
---|
85 | { |
---|
86 | std::string lhs = line.substr (0, d); |
---|
87 | std::string rhs = line.substr (d + 1); |
---|
88 | |
---|
89 | tolower (lhs); |
---|
90 | |
---|
91 | if (def) |
---|
92 | defines[lhs] = rhs; |
---|
93 | else |
---|
94 | fields[lhs] = rhs; |
---|
95 | } |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | in.close (); |
---|
100 | } |
---|
101 | |
---|
102 | bool |
---|
103 | package::get (const std::string& label, std::string& result) |
---|
104 | { |
---|
105 | result.erase (); |
---|
106 | |
---|
107 | std::string ll = label; |
---|
108 | tolower (ll); |
---|
109 | |
---|
110 | table::iterator ti = fields.find (ll); |
---|
111 | |
---|
112 | if (ti == fields.end ()) |
---|
113 | return false; |
---|
114 | |
---|
115 | /* |
---|
116 | * Take a copy so we can expand the macros in it. |
---|
117 | */ |
---|
118 | std::string s = ti->second; |
---|
119 | |
---|
120 | /* |
---|
121 | * Loop until there is nothing more to expand. |
---|
122 | */ |
---|
123 | bool expanded = true; |
---|
124 | while (expanded) |
---|
125 | { |
---|
126 | /* |
---|
127 | * Need to perform a regular expression search for '\$\{[^\}]+\}'. This |
---|
128 | * means look for every '${' then accept any character that is not a '}' |
---|
129 | * and finish with a '}'. |
---|
130 | */ |
---|
131 | size_t p = 0; |
---|
132 | while (p < s.length ()) |
---|
133 | { |
---|
134 | /* |
---|
135 | * Find the start and end of the label. |
---|
136 | */ |
---|
137 | size_t ms = s.find ("${", p); |
---|
138 | if (ms != std::string::npos) |
---|
139 | { |
---|
140 | size_t me = s.find ('}', ms); |
---|
141 | if (me != std::string::npos) |
---|
142 | { |
---|
143 | std::string ml = s.substr (ms, me); |
---|
144 | |
---|
145 | } |
---|
146 | } |
---|
147 | else |
---|
148 | { |
---|
149 | p = s.length (); |
---|
150 | } |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | return true; |
---|
155 | } |
---|
156 | } |
---|