Advertisement
Guest User

Untitled

a guest
Aug 10th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.45 KB | None | 0 0
  1. //============================================================================
  2. // Name : CheckSmth.cpp
  3. // Author :
  4. // Version :
  5. // Copyright : Your copyright notice
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <iostream>
  10. #include <fstream>
  11. #include <string>
  12. #include <stdio.h>
  13. #include <ctype.h>
  14. #include <cctype>
  15.  
  16. struct inst
  17. {
  18. std::string layer;
  19. std::string component;
  20. std::string instructions[10];
  21. static int counter;
  22. int cyclicTime;
  23. };
  24.  
  25. class Parser
  26. {
  27. public:
  28. int checkNesting(std::string tab[], std::string nest)
  29. {
  30. int nestingLevel = 0;
  31. for(int iIndex = 0; iIndex < 20; iIndex++)
  32. {
  33. if(tab[iIndex][0] == '<' && tab[iIndex][1] == '/')
  34. {
  35. nestingLevel--;
  36. }
  37. else if(tab[iIndex][0] == '<')
  38. {
  39. nestingLevel++;
  40. }
  41. if(tab[iIndex] == nest)
  42. {
  43. break;
  44. }
  45. }
  46. return nestingLevel;
  47. }
  48.  
  49. int valid(const char * path)
  50. {
  51. std::string buffer[10];
  52.  
  53.  
  54. std::fstream fileLoaded;
  55. fileLoaded.open("pare.txt", std::ios::in);
  56.  
  57. if(fileLoaded.is_open() == true)
  58. {
  59. std::cout << "File have been opened successfully" << std::endl;
  60.  
  61. for(int i = 0 ; i < 10; i ++)
  62. {
  63. fileLoaded >> buffer[i];
  64. std::cout << buffer[i] << std::endl;
  65. }
  66.  
  67. for(int i = 0 ; i < 10; i ++)
  68. {
  69. int buffLength = buffer[i].length();
  70.  
  71. //=======================================================================
  72. // Start reading a config
  73. //=======================================================================
  74. if(buffer[i] == "!start")
  75. {
  76. std::cout << "Beginning of the config" << std::endl;
  77. }
  78. //=======================================================================
  79. else if(buffer[i][0] == '/' && buffer[i][1] == '/')
  80. {
  81. continue;
  82. }
  83. else if(buffer[i][0] == '<')
  84. {
  85. if(buffer[i][buffLength-1] != '>')
  86. {
  87. std::cout << "Bad build of config, you forgot '>' in your " << i+1 << " line" << std::endl;
  88. }
  89. else
  90. {
  91. std::cout << "GOOD" << std::endl;
  92. }
  93. }
  94.  
  95. else if(buffer[i][0] == '#')
  96. {
  97. int lineLenth = buffer[i].length();
  98.  
  99. if(buffer[i][lineLenth-1] != ';')
  100. {
  101. std::cout << "Bad build of config, you forgot ';' in your " << i+1 << " line" << std::endl;
  102. }
  103. else if(buffer[i][lineLenth-1] == ';')
  104. {
  105. std::cout << "Good try to read commands" << std::endl;
  106. }
  107. }
  108.  
  109. else if(buffer[i] == "!end")
  110. {
  111. std::cout << "End of the config" << std::endl;
  112. break;
  113. }
  114.  
  115. else
  116. {
  117. std::cout << "Bad build of config, your line must start with '<>' or '#'" << std::endl;
  118. }
  119. }
  120.  
  121. }
  122. else
  123. {
  124. std::cout << "File have not been opened successfully" << std::endl;
  125. }
  126. fileLoaded.close();
  127. }
  128.  
  129. int parse(const char * path)
  130. {
  131. std::string buff[20];
  132. inst components[20];
  133. std::string layer = "";
  134. std::string compon = "";
  135. std::string instruction = "";
  136.  
  137. std::fstream fileLoaded;
  138. fileLoaded.open("pare.txt", std::ios::in);
  139.  
  140. for(int i = 0; i < 20; i++)
  141. {
  142. fileLoaded >> buff[i];
  143. if(buff[i][0] == '<' && buff[i][1] == '/')
  144. {
  145.  
  146. }
  147. else if(buff[i][0] == '<')
  148. {
  149. if(this->checkNesting(buff, buff[i]) == 1)
  150. {
  151. buff[i] = buff[i] + '1';
  152. }
  153. if(this->checkNesting(buff, buff[i]) == 2)
  154. {
  155. buff[i] = buff[i] + '2';
  156. }
  157. }
  158. }
  159. for(int i = 0; i < 20; i++)
  160. {
  161. int buffLenght = buff[i].length();
  162. if(buff[i][buffLenght-1] == '1')
  163. {
  164. layer = buff[i];
  165. }
  166. else if(buff[i][buffLenght-1] == '2')
  167. {
  168. compon = buff[i];
  169. //components[i].layer = layer;
  170. //components[i].component = compon;
  171. }
  172. else if(buff[i][0] == '#')
  173. {
  174. instruction = buff[i];
  175. components[i].layer = layer;
  176. components[i].component = compon;
  177. components[i].instructions[inst::counter] = instruction;
  178. inst::counter++;
  179. }
  180. }
  181.  
  182. for(int i = 0; i < 20; i++)
  183. {
  184. for(int j = 0; j < 10; j++)
  185. {
  186. if(components[i].instructions[j] != "")
  187. {
  188.  
  189. std::cout << components[i].layer << components[i].component << components[i].instructions[j] << std::endl;
  190. }
  191. }
  192. }
  193. this->prepare(components);
  194.  
  195. }
  196.  
  197. int prepare(const inst tab[20])
  198. {
  199. for(int i = 0; i < 20; i++)
  200. {
  201. if(tab[i].layer == "<drivers>1")
  202. {
  203. this->driversLayer(tab[i]);
  204. }
  205. else if(tab[i].layer == "<service>1")
  206. {
  207. this->serviceLayer(tab[i]);
  208. }
  209. else if(tab[i].layer == "<rte>1")
  210. {
  211. this->rteLayer(tab[i]);
  212. }
  213. else if(tab[i].layer == "<swc>1")
  214. {
  215. this->swcLayer(tab[i]);
  216. }
  217. else if(tab[i].layer == "<os>1")
  218. {
  219. this->osLayer(tab[i]);
  220. }
  221. else
  222. {
  223. std::cout << "Error in read config in line " << i+1 << "." << std::endl;
  224. }
  225. }
  226. }
  227.  
  228. int driversLayer(inst object)
  229. {
  230. if(object.component == "<udpDriver>2")
  231. {
  232. for(int i = 0; i < 10; i++)
  233. {
  234. if(object.instructions[i][0] == '#')
  235. {
  236. //checkInstr(object.instructions[i]);
  237. }
  238. }
  239. }
  240. }
  241. int rteLayer(inst object)
  242. {
  243.  
  244. }
  245. int serviceLayer(inst object)
  246. {
  247.  
  248. }
  249. int swcLayer(inst object)
  250. {
  251.  
  252. }
  253. int osLayer(inst object)
  254. {
  255.  
  256. }
  257. };
  258.  
  259. int inst::counter = 0;
  260. int main()
  261. {
  262. const char * path = "par.txt";
  263. Parser obj;
  264. obj.parse(path);
  265. char z = '<';
  266. std::cout << isalpha(z);
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement