YorKnEz

C++ script for counting OpenBoard pages

Dec 27th, 2021
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string>
  4. #include <cstring>
  5. #include <filesystem>
  6.  
  7. using namespace std;
  8.  
  9. namespace fs = filesystem;
  10.  
  11. int main() {
  12. // total pages
  13. int pages = 0;
  14.  
  15. // current path is .
  16. string path = ".",
  17. path2, file;
  18.  
  19. // needed for searching substrings
  20. size_t found, size;
  21.  
  22. // auxiliary strings
  23. char f[100], metadata[1000];
  24.  
  25. // looping through openboard documents
  26. for (const auto & entry : fs::directory_iterator(path)) {
  27. path2 = entry.path();
  28.  
  29. // cout << path2 << '\n';
  30.  
  31. // check if current entry is an openboard document
  32. found = path2.find("OpenBoard");
  33.  
  34. // if not then go to the next item
  35. if (found == string::npos) continue;
  36.  
  37. // loop through openboard document's files
  38. for (const auto & entry2 : fs::directory_iterator(path2)) {
  39. file = entry2.path();
  40.  
  41. // search the metadata.rdf file
  42. found = file.find("metadata.rdf");
  43.  
  44. if (found != string::npos) {
  45. // cout << " " << file << '\n';
  46. size = file.size();
  47.  
  48. // if we find it, put the location of the file in an auxiliary string
  49. for (int i = 2; i < size; i++) {
  50. f[i-2] = file[i];
  51. }
  52.  
  53. f[size-2] = '\0';
  54.  
  55. // cout << " " << f << '\n';
  56.  
  57. // open the file
  58. freopen(f, "r", stdin);
  59.  
  60. bool found = false;
  61.  
  62. // find the page-count
  63. while (cin.getline(metadata, 1000) && !found) {
  64. // cout << metadata << '\n';
  65.  
  66. // search for page-count
  67. char *p = strstr(metadata, "page-count");
  68.  
  69. // if we find it we form the number of pages
  70. if (p) {
  71. int n = 0;
  72. int k = p - metadata + 11;
  73. found = true;
  74.  
  75. while ('0' <= metadata[k] && metadata[k] <= '9') {
  76. n = n * 10 + (metadata[k] - '0');
  77. k++;
  78. }
  79.  
  80. cout << n << '\n';
  81.  
  82. // add the pages to the total
  83. pages += n;
  84. }
  85. }
  86.  
  87. // close current file
  88. fclose(stdin);
  89. }
  90. }
  91. }
  92.  
  93. cout << pages << '\n';
  94.  
  95. return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment