PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Courses/AdvancedProgrammingEE/assignment_1/graphviz_ver_0.1/cpp_interface/src/read_file.cc

https://github.com/dilawar/MyPublic
C++ | 332 lines | 211 code | 32 blank | 89 comment | 29 complexity | 329d39efa5cc85f4412a204343b91307 MD5 | raw file
  1. /*
  2. * =====================================================================================
  3. *
  4. * Filename: read_file.cc
  5. *
  6. * Description: See the header file.
  7. *
  8. * Version: 1.0
  9. * Created: Sunday 08 August 2010 12:26:21 IST
  10. * Revision: none
  11. * Compiler: g++/gcc
  12. *
  13. * Author: Dilawar (nuts), dilawar[AT]ee[dot]iitb[dot]ac[dot]in
  14. * Institute: Indian Institute of Technology, Bombay
  15. *
  16. * This material is released under GNU Lesser Public License.
  17. * You are free to copy, distribute or use it for any non-commercial activity.
  18. * But you are not allowed to modify it. If you are a student, you can use its
  19. * part in your work with or without mentioning it.
  20. *
  21. * For additional details, please see the GNU Lesser Public license.
  22. *
  23. * NOTE : No propriety software is used in this material.
  24. * Almost all of the code is written and modified in VIM editor with c-support
  25. * plugin which is more awesome than Kung Fu Panda. Just kidding, no one is more
  26. * awesome than Kung Fu Panda with or without a light saber.
  27. *
  28. * This program is made using a bit for here, a bit from there under the influence
  29. * of a lot of burnt out neurons.
  30. * Report bugs : dilawar.in@gmail.com
  31. * =====================================================================================
  32. */
  33. #include <vector>
  34. #include <cmath>
  35. #include <string>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <cstdlib>
  39. #include <string.h>
  40. #include <iostream>
  41. #include "./../include/read_file.h"
  42. /*
  43. * === FUNCTION ======================================================================
  44. * Name: ReadFile
  45. * Description: Constructor of this class
  46. * =====================================================================================
  47. */
  48. ReadFile::ReadFile()
  49. {
  50. #ifdef DEBUG
  51. std::cout<<"\nReadFile Class: Entering Constructor...\n";
  52. #else /* ----- not DEBUG ----- */
  53. #endif /* ----- not DEBUG ----- */
  54. }
  55. /*
  56. * === FUNCTION ======================================================================
  57. * Name: ~ReadFile
  58. * Description: Destructor
  59. * =====================================================================================
  60. */
  61. ReadFile::~ReadFile()
  62. {
  63. #ifdef DEBUG
  64. std::cout<<"\nReadFile Class: Entering destructor...\n";
  65. #else /* ----- not DEBUG ----- */
  66. #endif /* ----- not DEBUG ----- */
  67. }
  68. /*
  69. * === FUNCTION ======================================================================
  70. * Name: setFilePointer
  71. * Description: Set the class private member FILE* equal to the given arg.
  72. * =====================================================================================
  73. */
  74. void ReadFile::setFilePointer(std::string myFile)
  75. {
  76. #ifdef DEBUG
  77. std::cout<<"\nFuntion ReadFile::setFilePointer ...\n";
  78. std::cout<<"\nPassed argument is:"<<myFile<<"\t"<<myFile.c_str()<<"\n";
  79. #else /* ----- not DEBUG ----- */
  80. #endif /* ----- not DEBUG ----- */
  81. //ReadFile::myFileStream.open(myFile.c_str());
  82. fp = fopen(myFile.c_str(), "r+");
  83. std::cout<<"AD";
  84. if(NULL != fp)
  85. {
  86. #ifdef DEBUG
  87. std::cout<<"\nWe have successfully opened the file... \n";
  88. #else /* ----- not DEBUG ----- */
  89. #endif /* ----- not DEBUG ----- */
  90. }
  91. else
  92. {
  93. std::cout<<"\nFATAL ERROR : Can not open the file. Either file is missing"
  94. <<"\nor I do not have enough privileges to open this file. This"
  95. <<"\nis really embarrassing.\n";
  96. exit(EXIT_FAILURE);
  97. }
  98. }
  99. /*
  100. * === FUNCTION ======================================================================
  101. * Name: checkFileForErrors(FILE* pMyFile)
  102. * Description: this function will check the file for format errors. If error
  103. * found, application will exit. It will continue with warnings. See
  104. * README_FILE_FORMAT for more details.
  105. * =====================================================================================
  106. */
  107. int ReadFile::checkFileForErrors()
  108. {
  109. #ifdef DEBUG
  110. std::cout<<"\nInside ReadFile::checkFileForErrors ... \n";
  111. #else /* ----- not DEBUG ----- */
  112. #endif /* ----- not DEBUG ----- */
  113. //unsigned int lineNum = 1;
  114. //unsigned int countNum = 0;
  115. //std::vector<int> pairOfInt;
  116. //char myLine[100];
  117. char ch;
  118. char aChar[10];
  119. int numNext= 0;
  120. int size = 0;
  121. if(NULL != fp)
  122. {
  123. #ifdef DEBUG
  124. std::cout<<"\n\tFile is open...\n";
  125. #else /* ----- not DEBUG ----- */
  126. #endif /* ----- not DEBUG ----- */
  127. while(( ch = getc(fp)) != EOF)
  128. {
  129. #ifdef DEBUG
  130. std::cout<<"\tch is: "<<ch;
  131. #else /* ----- not DEBUG ----- */
  132. #endif /* ----- not DEBUG ----- */
  133. if(isdigit(ch))
  134. {
  135. numNext++;
  136. {
  137. aChar[numNext-1] = ch;
  138. // std::cout<<"aChar :"<<aChar[numNext-1];
  139. // printf("%c and %d", aChar[numNext-1],aChar[numNext-1]);
  140. // vecOfPairs.push_back(ch - 48);
  141. }
  142. //printf("NumNext: %d", numNext);
  143. }
  144. else{
  145. //printf("\nEntering else loop..\n");
  146. int p = 0;
  147. for(int i =1; i <= numNext; i++)
  148. {
  149. p = p + (aChar[i-1]-48)*pow(10, numNext-i);
  150. }
  151. if( p != 0) // Avoid non-digit zero being pushed into vector.
  152. {
  153. vecOfPairs.push_back(p);
  154. }
  155. numNext = 0;
  156. }
  157. }
  158. if(size % 2 != 0)
  159. {
  160. std::cout<<""
  161. <<"Note :"
  162. <<"\n\tIn given input file. One or more pair are not complete. I recommend"
  163. <<"\n\tthat you check your file. I am ignoring this pair and continuing...\n";
  164. }
  165. #if 0
  166. for(unsigned int i = 0; i < strlen(myLine); i++)
  167. {
  168. std::cout<<"AA";
  169. if(isdigit(myLine[i]))
  170. {
  171. std::cout<<"AA";
  172. vecOfPairs.push_back(myLine[i] - 48);
  173. std::cout<<"Push at "<<i<<": num "<<myLine[i]-48;
  174. countNum++;
  175. }
  176. }
  177. if(2 != countNum)
  178. {
  179. std::cerr<<"\nError is input file.\n";
  180. return EXIT_FAILURE;
  181. }
  182. vecOfPairs.push_back(pairOfInt[0]);
  183. vecOfPairs.push_back(pairOfInt[1]);
  184. lineNum++;
  185. }
  186. }
  187. // Remove last two entries from the vector.
  188. vecOfPairs.erase(vecOfPairs.end());
  189. vecOfPairs.erase(vecOfPairs.end());
  190. #endif
  191. fclose(fp);
  192. return EXIT_SUCCESS;
  193. }
  194. else
  195. {
  196. std::cerr<<"\n\tFile is not open...\n";
  197. return EXIT_FAILURE;
  198. }
  199. }
  200. #if 0
  201. /*
  202. * === FUNCTION ======================================================================
  203. * Name: checkLine
  204. * Description: utility to check if the line read from a file is OK!
  205. * =====================================================================================
  206. */
  207. std::vector<int> ReadFile::checkLine(std::string thisLine, int lineNum)
  208. {
  209. #ifdef DEBUG1
  210. std::cout<<"\nInside ReadFile::checkLine...\n"<<thisLine<<"and number is "<<lineNum;
  211. #endif /* ----- not DEBUG ----- */
  212. unsigned int i = 0;
  213. unsigned int countNum = 0;
  214. bool ifComment = false;
  215. std::vector<int> pairInt;
  216. #if 0
  217. //before making is #if 1, change the thisLine to myLine is function args.
  218. char* thisLine;
  219. thisLine = new char[myLine.size()+1];
  220. strcpy(thisLine, myLine.c_str());
  221. #endif
  222. for(i = 0; i <= thisLine.length(); i++)
  223. {
  224. //std::cout<<"thisLine[0]"<<thisLine[0];
  225. #ifdef DEBUG1
  226. std::cout<<"\nValues found.\n";
  227. #else /* ----- not DEBUG ----- */
  228. #endif /* ----- not DEBUG ----- */
  229. #if 0
  230. if(isspace(thisLine[i]) == true /*|| thisLine[i] == '\t'*/)
  231. {
  232. i++;
  233. }
  234. #endif
  235. if(isdigit(thisLine[i]))
  236. {
  237. countNum++;
  238. pairInt.push_back(thisLine[i] - 48);
  239. #ifdef DEBUG1
  240. std::cout<<"\nValue is: "<<thisLine[i];
  241. #else /* ----- not DEBUG ----- */
  242. #endif /* ----- not DEBUG ----- */
  243. }
  244. }
  245. if(countNum != 2 && countNum != 0)
  246. {
  247. std::cout<<"\nError in input file at line: "<<lineNum;
  248. exit(EXIT_FAILURE);
  249. }
  250. return pairInt;
  251. }
  252. #endif
  253. /*
  254. * === FUNCTION ======================================================================
  255. * Name: copyFileIntoVector
  256. * Description: see header file
  257. * =====================================================================================
  258. */
  259. void ReadFile::copyFileIntoAVector()
  260. {
  261. #ifdef DEBUG1
  262. std::cout<<"\nInside ReadFile::copyFileIntoAVector ...\n";
  263. #else /* ----- not DEBUG ----- */
  264. #endif /* ----- not DEBUG ----- */
  265. if(NULL != fp)
  266. {
  267. if(EXIT_SUCCESS == checkFileForErrors())
  268. {
  269. #ifdef DEBUG
  270. std::cout<<"\n\tData in Vec is :\n";
  271. for(std::vector<unsigned long int>::size_type i = 0; i < vecOfPairs.size(); i++)
  272. {
  273. std::cout<<"\t"<<vecOfPairs[i];
  274. }
  275. std::cout<<'\n';
  276. #else /* ----- not DEBUG ----- */
  277. #endif /* ----- not DEBUG ----- */
  278. }
  279. }
  280. else
  281. {
  282. std::cout<<"\n\tI am sorry. It must have not happened. I am messed up!"
  283. <<"\n\tContact dilawar@ee.iitb.ac.in \n";
  284. }
  285. }
  286. /*
  287. * === FUNCTION ======================================================================
  288. * Name: copyVecToVec
  289. * Description: Interface to other object
  290. * =====================================================================================
  291. */
  292. std::vector<int> ReadFile::copyVecToVec()
  293. {
  294. std::vector<int> thisVec;
  295. for(std::vector<int>::size_type i = 0; i < vecOfPairs.size(); i++)
  296. {
  297. thisVec.push_back(vecOfPairs[i]);
  298. }
  299. return thisVec;
  300. }