source: rtems-tools/rtemstoolkit/pkgconfig.cpp @ 3618a62

Last change on this file since 3618a62 was 87e0e76, checked in by Chris Johns <chrisj@…>, on Sep 13, 2014 at 2:09:16 AM

Refactor code into the RTEMS Toolkit.

  • Property mode set to 100644
File size: 3.8 KB
Line 
1/*
2 * Copyright (c) 2011-2014, 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 <rld.h>
22#include <pkgconfig.h>
23
24namespace pkgconfig
25{
26  package::package (const std::string& name)
27  {
28    load (name);
29  }
30
31  package::package ()
32  {
33  }
34
35  void
36  package::load (const std::string& name)
37  {
38    std::ifstream in (name.c_str (), std::ios::in);
39
40    while (!in.eof ())
41    {
42      char buffer[1024];
43
44      in.getline (buffer, sizeof (buffer));
45
46      std::string line (buffer);
47      size_t      hash;
48
49      hash = line.find ('#');
50      if (hash != std::string::npos)
51        line.erase(hash);
52
53      if (line.size () > 0)
54      {
55        size_t eq = line.find_first_of ('=');
56        size_t dd = line.find_first_of (':');
57
58        size_t d = std::string::npos;
59        bool   def = false;
60
61        if ((eq != std::string::npos) && (dd != std::string::npos))
62        {
63          if (eq < dd)
64          {
65            d = eq;
66            def = true;
67          }
68          else
69          {
70            d = dd;
71            def = false;
72          }
73        }
74        else if (eq != std::string::npos)
75        {
76          d = eq;
77          def = true;
78        }
79        else if (dd != std::string::npos)
80        {
81          d = dd;
82          def = false;
83        }
84
85        if (d != std::string::npos)
86        {
87          std::string lhs = rld::tolower (line.substr (0, d));
88          std::string rhs = line.substr (d + 1);
89          if (def)
90            defines[lhs] = rhs;
91          else
92            fields[lhs] = rhs;
93        }
94      }
95    }
96
97    in.close ();
98  }
99
100  bool
101  package::get (const std::string& label, std::string& result)
102  {
103    result.erase ();
104
105    std::string     ll = rld::tolower (label);
106    table::iterator ti = fields.find (ll);
107
108    if (ti == fields.end ())
109      return false;
110
111    /*
112     * Take a copy so we can expand the macros in it.
113     */
114    std::string s = ti->second;
115
116    /*
117     * Loop until there is nothing more to expand.
118     */
119    bool expanded = true;
120    while (expanded)
121    {
122      /*
123       * Need to perform a regular expression search for '\$\{[^\}]+\}'. This
124       * means look for every '${' then accept any character that is not a '}'
125       * and finish with a '}'.
126       */
127      expanded = false;
128      size_t p = 0;
129      while (p < s.length ())
130      {
131        /*
132         * Find the start and end of the label.
133         */
134        size_t ms = s.find ("${", p);
135        if (ms != std::string::npos)
136        {
137          size_t me = s.find ('}', ms);
138          if (me != std::string::npos)
139          {
140            std::string     ml = rld::tolower(s.substr (ms + 2, me - ms - 2));
141            table::iterator di = defines.find (ml);
142            if (di != defines.end ())
143            {
144              s = rld::find_replace (s, s.substr (ms, me - ms + 1),  di->second);
145              expanded = true;
146            }
147            p = me + 1;
148          }
149          else
150          {
151            p = ms + 2;
152          }
153        }
154        else
155        {
156          p = s.length ();
157        }
158      }
159    }
160
161    result = rld::trim (s);
162
163    return true;
164  }
165}
Note: See TracBrowser for help on using the repository browser.