fork download
  1. struct stringdata
  2. {
  3. string tag, value;
  4. }
  5.  
  6. void readLine(ifstream &inFile, stringdata &data)
  7. {
  8. string dataLine;
  9. int start, readLength;
  10. bool validData = false;
  11.  
  12. data.value = "";
  13.  
  14. while (!validData && inFile && !inFile.eof()) //Checks if valid data has been read, the file is still good, and not at the end of the file.
  15. {
  16. getline(inFile, dataLine);
  17.  
  18. if (dataLine.find('=') != string::npos && dataLine != "")
  19. {
  20. while (dataLine.at(0) == ' ' && dataLine != "") //Checks for leading whitespace and empty strings
  21. {
  22. dataLine = dataLine.substr(1, dataLine.length());
  23. }
  24.  
  25. if (dataLine.at(0) != '#') // Leading comment, ignore line
  26. {
  27. start = dataLine.find('=');
  28. char positionChar;
  29.  
  30. do //Finds a non whitespace character to the left of the equal sign, giving us the tag
  31. {
  32. start--;
  33. positionChar = dataLine.at(start);
  34. }while (positionChar == ' ');
  35.  
  36. data.tag = dataLine.substr(0, start + 1);
  37.  
  38. start = dataLine.find('=');
  39.  
  40. do //Finds the start of the value
  41. {
  42. start++;
  43. positionChar = dataLine.at(start);
  44. }while (positionChar == ' ');
  45.  
  46. while (dataLine.at(dataLine.length() - 1) == ' ') //Checks for and cleans up trailing whitespace
  47. {
  48. dataLine = dataLine.substr(0, dataLine.length() - 1);
  49. }
  50.  
  51. if (dataLine.find('#') != string::npos) //If a comment tag is found somewhere in the string
  52. {
  53. readLength = dataLine.find('#'); //Find the comment
  54.  
  55. do //and read backwards from the comment to the value
  56. {
  57. readLength--;
  58. positionChar = dataLine.at(readLength);
  59. }while (positionChar == ' ');
  60.  
  61. readLength = (readLength - start) + 1; //Calculate the length of the value for substringing
  62. }
  63. else
  64. readLength = dataLine.length() - start + 1; //Calculate the length of the value for substringing
  65.  
  66. data.value = dataLine.substr(start, readLength);
  67.  
  68. validData = true;
  69. }
  70. }
  71. else if (dataLine.find(ECHO) != string::npos)
  72. {
  73. while (dataLine.at(0) == ' ' && dataLine != "") //Checks for leading whitespace and empty strings
  74. {
  75. dataLine = dataLine.substr(1, dataLine.length());
  76. }
  77. if (dataLine.at(0) != '#')
  78. echoDebug = true;
  79. }
  80. else if (dataLine.find('<') != string::npos && dataLine.find('>') != string::npos ) //Header tag
  81. {
  82. while (dataLine.at(0) == ' ' && dataLine != "") //Checks for leading whitespace and empty strings
  83. {
  84. dataLine = dataLine.substr(1, dataLine.length());
  85. }
  86.  
  87. if (dataLine.at(0) != '#')
  88. {
  89. data.tag = dataLine.substr(dataLine.find('<'), dataLine.find('>') - dataLine.find('<') + 1);
  90. data.value = "";
  91. validData = true;
  92. }
  93. // if (echoDebug)
  94. // cout << "Tag: " << data.tag << endl;
  95.  
  96. }
  97. else if (dataLine.find('=') == string::npos && dataLine != "") //Missing a "=", blank lines are ignored
  98. {
  99. while (dataLine.at(0) == ' ' && dataLine != "") //Checks for leading whitespace and empty strings
  100. {
  101. dataLine = dataLine.substr(1, dataLine.length());
  102. }
  103.  
  104. if (dataLine.at(0) != '#')
  105. cout << "Line \"" << dataLine << "\" is missing something important. (Make sure you have an '=' somewhere)" << endl;
  106. }
  107. else if (!inFile && !inFile.eof()) //File is bad and not just at the end of the file
  108. {
  109. cout << "File read error. Please make sure you are using a valid file" << endl
  110. << "Last line read: \"" << dataLine << "\"." << endl
  111. << "Now terminating" << endl;
  112. exit(1); //Terminate program as invalid data is crashable
  113. }
  114. }
  115. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty