Debian Codegraph @

src/SBFunction.cpp

raw
1 // SBFunction - container for StoryBoard function
2
3 // For all support, instructions and copyright go to:
4 // http://e2guardian.org/
5 // Released under the GPL v2, with the OpenSSL exception described in the README file.
6
7 // INCLUDES
8
9 #ifdef HAVE_CONFIG_H
10 #include "e2config.h"
11 #endif
12 #include "ListContainer.hpp"
13 #include "SBFunction.hpp"
14 #include "OptionContainer.hpp"
15 #include "RegExp.hpp"
16 #include "Logger.hpp"
17
18 #include <algorithm>
19 #include <cstdlib>
20 #include <cstdio>
21 #include <ctime>
22 #include <unistd.h>
23 #include <iostream>
24 #include <fstream>
25 #include <sys/stat.h>
26 #include <sys/time.h>
27 #include <list>
28 #include <vector>
29
30 // GLOBALS
31
32 extern OptionContainer o;
33
34 // DEFINES
35
36 // Constructor - set default values
37 SBFunction::SBFunction()
38 {
39 }
40
41 // delete the memory block when the class is destryed
42 SBFunction::~SBFunction()
43 {
44 reset();
45 }
46
47 // clear & reset all values
48 void SBFunction::reset() {
49 }
50
51 bool SBFunction::start(String & sname, unsigned int id, unsigned int& line_no, String filename) {
52 name = sname;
53 fn_id = id;
54 file_lineno = line_no;
55 file_name = filename;
56 comm_dq.clear();
57 return true;
58 }
59
60 bool SBFunction::end() {
61 return true;
62 }
63
64 bool SBFunction::addline(String command, String params, String action, unsigned int line_no) {
65 com_rec rec;
66 if (command == "if") {
67 rec.action_id = SB_COM_IF;
68 rec.isif = true;
69 } else if (command == "ifnot") {
70 rec.action_id = SB_COM_IFNOT;
71 rec.isif = false;
72 } else {
73 E2LOGGER_error("StoryBoard error: Invalid command ", command, "at line ", String(line_no), " of ", file_name);
74 return false;
75 }
76 rec.file_lineno = line_no;
77 // process params
78 DEBUG_story("CLine ", params, " Action ", action);
79
80 String state;
81 String temp;
82 String temp2;
83 if (params.contains(",")) {
84 state = params.before(",");
85 DEBUG_story("CLine state is ", state);
86 temp = params.after(",");
87 } else {
88 state = params;
89 }
90 state.removeWhiteSpace();
91 rec.state = getStateID(state);
92 if (rec.state == 0) {
93 E2LOGGER_error("StoryBoard error: Invalid state ", state, " at line ", String(line_no), " of ", file_name);
94 return false;
95 }
96
97 if ( temp.length() ) {
98 if (temp.contains(",")) {
99 temp2 = temp.before(",");
100 temp = temp.after(",");
101 } else {
102 temp2 = temp;
103 temp = "";
104 }
105 temp2.removeWhiteSpace();
106 rec.list_name = temp2;
107 DEBUG_story("CLine list is ", temp2);
108 }
109 if ( temp.length() ) {
110 if (temp.contains(",")) {
111 temp2 = temp.before(",");
112 temp = temp.after(",");
113 } else {
114 temp2 = temp;
115 temp = "";
116 }
117 temp2.removeWhiteSpace();
118 rec.mess_no = temp2.toInteger();
119 DEBUG_story("CLine mess_no is ", temp2);
120 }
121 if ( temp.length() ) {
122 if (temp.contains(",")) {
123 temp2 = temp.before(",");
124 temp = temp.after(",");
125 } else {
126 temp2 = temp;
127 temp = "";
128 }
129 temp2.removeWhiteSpace();
130 rec.log_mess_no = temp2.toInteger();
131 DEBUG_story("CLine log_mess_no is ", temp2);
132 }
133 if ( temp.length() ) {
134 if (temp.contains(",")) {
135 temp2 = temp.before(",");
136 temp = temp.after(",");
137 } else {
138 temp2 = temp;
139 temp = "";
140 }
141 temp2.removeWhiteSpace();
142 if (temp2 == "optional")
143 rec.optional = true;
144 DEBUG_story("CLine optional is true");
145 }
146 // check list and get list_ID - needs ListMeta object - done in StoryBook::readfile
147
148 rec.return_after_action = false;
149 rec.return_after_action_is_true = false;
150
151 if (action.startsWith("return ")) {
152 rec.return_after_action = true;
153 action = action.after("return ");
154 } else if (action.startsWith("returnif ")) {
155 rec.return_after_action_is_true = true;
156 action = action.after("returnif ");
157 };
158
159 action.removeWhiteSpace();
160
161 rec.action_name = action; // will check this and get action_id later as function may not yet be defined.
162 comm_dq.push_back(rec);
163 return true;
164 }
165
166 unsigned int SBFunction::getStateID(String & state) {
167 unsigned int i = 0;
168 while (i < SB_STATE_MAP_SIZE) {
169 if ( state == state_map[i]) {
170 return ++i;
171 }
172 ++i;
173 }
174 return 0;
175 }
176
177 unsigned int SBFunction::getBIFunctID(String &action) { // get built-in function id
178 unsigned int i = 0;
179 while (i < SB_FUNC_MAP_SIZE) {
180 if ( action == bi_funct_map.at(i)) {
181 return ++i + SB_BI_FUNC_BASE;
182 }
183 ++i;
184 }
185 return 0;
186 }
187
188 String SBFunction::getState(unsigned int id) { // get condition statement from state_id
189 if (--id < SB_STATE_MAP_SIZE)
190 return state_map[id];
191 else
192 return String(id);
193 };
194
195 String SBFunction::getBIFunct(unsigned int &id) { // get built-in function (action) from funct_id
196 if (id > 5000) {
197 unsigned int i = id - 5001;
198 if (i < SB_FUNC_MAP_SIZE)
199 return bi_funct_map[i];
200 }
201 return String(id);
202 };
203
204 String SBFunction::getName() {
205 return name;
206 }