1 /******************************************************************************
2 * Copyright (c) 2017-2018 Texas Instruments Incorporated - http://www.ti.com/
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Texas Instruments Incorporated nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
27 *****************************************************************************/
29 #include <boost/spirit/include/qi.hpp>
30 #include <boost/spirit/include/phoenix_operator.hpp>
32 #include <string>
33 #include <fstream>
34 #include <iostream>
36 #include "configuration.h"
38 namespace qi = boost::spirit::qi;
39 namespace ascii = boost::spirit::ascii;
40 namespace ph = boost::phoenix;
42 using namespace tidl;
44 template <typename Iterator>
45 struct ConfigParser : qi::grammar<Iterator, ascii::space_type>
46 {
47 ConfigParser(Configuration &x) : ConfigParser::base_type(entry)
48 {
49 using qi::int_;
50 using qi::lit;
51 using qi::lexeme;
52 using ascii::char_;
53 using qi::_1;
55 //TODO: Ignore blank lines and comments
56 path %= lexeme[+(char_ - '"')];
58 // Discard '"'
59 q_path = qi::omit[*char_('"')] >> path >> qi::omit[*char_('"')];
61 entry %=
62 lit("numFrames") >> '=' >> int_[ph::ref(x.numFrames) = _1] |
63 lit("preProcType") >> '=' >> int_[ph::ref(x.preProcType) = _1] |
64 lit("inWidth") >> '=' >> int_[ph::ref(x.inWidth) = _1] |
65 lit("inHeight") >> '=' >> int_[ph::ref(x.inHeight) = _1] |
66 lit("inNumChannels") >> '=' >> int_[ph::ref(x.inNumChannels) = _1] |
68 lit("inData") >> "=" >> q_path[ph::ref(x.inData) = _1] |
69 lit("outData") >> "=" >> q_path[ph::ref(x.outData) = _1] |
70 lit("netBinFile") >> "=" >> q_path[ph::ref(x.netBinFile) = _1] |
72 lit("paramsBinFile") >> "=" >> q_path[ph::ref(x.paramsBinFile) = _1]
73 ;
74 }
76 qi::rule<Iterator, std::string(), ascii::space_type> path;
77 qi::rule<Iterator, std::string(), ascii::space_type> q_path;
78 qi::rule<Iterator, ascii::space_type> entry;
79 };
81 bool Configuration::ReadFromFile(const std::string &file_name)
82 {
83 std::ifstream IFS(file_name);
85 if (!IFS.good())
86 return false;
88 typedef ConfigParser<std::string::const_iterator> ConfigParser;
90 ConfigParser G(*this);
91 std::string str;
93 bool result = true;
95 while (getline(IFS, str))
96 {
97 result = phrase_parse(str.cbegin(), str.cend(), G, ascii::space);
99 if (!result)
100 {
101 std::cout << "Parsing failed at: " << str << std::endl;
102 break;
103 }
104 }
106 IFS.close();
108 // If read failed, return false
109 if (!result)
110 return false;
112 // If validate fails, return false
113 if (!Validate())
114 return false;
116 return true;
117 }