EMANE  1.2.1
eelinputparser.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 - Adjacent Link LLC, Bridgewater, New Jersey
3  * Copyright (c) 2010 - DRS CenGen, LLC, Columbia, Maryland
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of DRS CenGen, LLC nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 
35 #include "eelinputparser.h"
38 
40 
42 
43 bool EMANE::Generators::EEL::InputParser::parse(const std::string & sInput,
44  float & fEventTime,
45  std::string &sEventType,
46  std::string &sModuleId,
47  InputArguments & inputArguments)
48 {
49  size_t pos = 0;
50  int iArgumentCount = 0;
51  std::string sArgument;
52 
53  // clear all input parameters
54  fEventTime = 0;
55  sEventType.clear();
56  sModuleId.clear();
57  inputArguments.clear();
58 
59  // strip leading white space
60  pos = sInput.find_first_not_of(" \t");
61 
62  while(!(sArgument = getNextArgument(sInput, pos)).empty())
63  {
64  if(iArgumentCount == 0)
65  {
66  // first arguement is floating point time
67  fEventTime =
68  Utils::ParameterConvert(sArgument).toFloat();
69  }
70  else if(iArgumentCount == 1)
71  {
72  // second argument is module id
73  sModuleId = sArgument;
74  }
75  else if(iArgumentCount == 2)
76  {
77  // third arguement id the event type
78  sEventType = sArgument;
79  }
80  else
81  {
82  inputArguments.push_back(sArgument);
83  }
84 
85  ++iArgumentCount;
86  }
87 
88  return iArgumentCount >= 3;
89 }
90 
91 std::string EMANE::Generators::EEL::InputParser::getNextArgument(const std::string & sInput,
92  size_t & posStart)
93 {
94  std::string sArgument;
95 
96  if(posStart != std::string::npos)
97  {
98  size_t posEnd = sInput.find_first_of(" \t\n#\"",posStart);
99 
100  if(posEnd != std::string::npos)
101  {
102  size_t len = 0;
103 
104  if(sInput.at(posEnd) == '\"')
105  {
106  if(sInput.at(posStart) == '\"')
107  {
108  posEnd = sInput.find_first_of("\"",posEnd + 1);
109 
110  if(posEnd != std::string::npos)
111  {
112  ++posStart;
113  len = posEnd - posStart;
114  }
115  else
116  {
117  // error: started an open string but did not finish it
118  std::stringstream ssDescription;
119  ssDescription<<"Unterminated string: "<<sInput<<std::ends;
120  throw FormatException(ssDescription.str());
121  }
122  }
123  else
124  {
125  // error: found an open string not as the first char of an arguement
126  std::stringstream ssDescription;
127  ssDescription<<"Invalid start of string: "<<sInput<<std::ends;
128  throw FormatException(ssDescription.str());
129  }
130  }
131  else
132  {
133  len = posEnd - posStart;
134  }
135 
136  sArgument = sInput.substr(posStart, len);
137 
138  posStart = sInput.find_first_not_of(" \t\n",posEnd);
139  }
140  else
141  {
142  sArgument = sInput.substr(posStart);
143  posStart = std::string::npos;
144  }
145  }
146 
147  return sArgument;
148 }
std::vector< std::string > InputArguments
float toFloat(float fMin=std::numeric_limits< float >::lowest(), float fMax=std::numeric_limits< float >::max()) const
bool parse(const std::string &sInput, float &fEventTime, std::string &sEventType, std::string &sModuleId, InputArguments &inputArguments)
Parameter conversion class with range checks.