DESC.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* Copyright (C) 2014 Carlos Aguilar Melchor, Joris Barrier, Marc-Olivier Killijian
  2. * This file is part of XPIR.
  3. *
  4. * XPIR is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * XPIR is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with XPIR. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "DESC.hpp"
  18. //#define NO_CATALOG //Use this option for performance tests (only with auto-choice)
  19. /**
  20. * Class constructor.
  21. * Param :
  22. * - messageListener& messageListeners_ : message listerner reference to display error message.
  23. **/
  24. DESC::DESC(messageListener& messageListeners_):
  25. messageListeners(messageListeners_),
  26. maxFileSize(0)
  27. {
  28. ; // This semicolon is necessary under some uncertain circumstances .... e.g. OSX/gcc4.8.1
  29. }
  30. /**
  31. * Generate a menu from a char* buffer.
  32. * Param :
  33. * - char* receiveBuffer : buffer to parse.
  34. **/
  35. void DESC::makeMenu(char* receivedBuffer)
  36. {
  37. maxFileSize = 0;
  38. uint64_t fileCount = 0;
  39. char fileName[FILENAME_MAX_BYTE_SIZE + 1];
  40. char tmpSize[FILENAME_MAX_BYTE_SIZE + 1];
  41. stringstream ss (receivedBuffer);
  42. unsigned int catalogType;
  43. #ifdef DEBUG
  44. std::cout << "DESC: catalog received : ..."<<std::endl<<receivedBuffer<<std::endl;
  45. #endif
  46. if (ss.good ())
  47. {
  48. //The first line of the catalog is the catalog type
  49. ss >> catalogType;
  50. if(catalogType==0) {
  51. #ifdef DEBUG
  52. std::cout << "DESC: catalog type 0 : "<<catalogType<<std::endl;
  53. #endif
  54. ss >> fileCount;
  55. //The rest of catalog alternates filename lines and size lines.
  56. ss.getline (fileName, FILENAME_MAX_BYTE_SIZE);
  57. nbFiles = 0;
  58. for (uint64_t i = 0; i < fileCount; i++)
  59. {
  60. ss.getline (fileName, FILENAME_MAX_BYTE_SIZE);
  61. ss.getline (tmpSize, FILENAME_MAX_BYTE_SIZE);
  62. if (ss.gcount () > 0)
  63. {
  64. fileList.push_back(fileName);
  65. fileSize.push_back(atoi(tmpSize));
  66. nbFiles++;
  67. #ifdef DEBUG
  68. std::cout<<"DESC: "<<i<<"-"<<fileName<<"-"<<tmpSize<<std::endl;
  69. #endif
  70. if(maxFileSize < fileSize.back())
  71. maxFileSize = fileSize.back();
  72. }
  73. }
  74. } else if(catalogType==1) {
  75. #ifdef DEBUG
  76. std::cout << "DESC: catalog type 0 : "<<catalogType<<std::endl;
  77. #endif
  78. ss >> fileCount;
  79. ss.getline (tmpSize, FILENAME_MAX_BYTE_SIZE);
  80. ss.getline (tmpSize, FILENAME_MAX_BYTE_SIZE);
  81. maxFileSize=atoi(tmpSize);
  82. #ifndef NO_CATALOG
  83. for (uint64_t i = 0; i < fileCount; i++)
  84. {
  85. fileList.push_back(std::to_string(i));
  86. fileSize.push_back(maxFileSize);
  87. }
  88. #endif
  89. nbFiles = fileCount;
  90. } else {
  91. MessageEvent event(ERROR,"Catalog type unknown "+catalogType, __FUNCTION__);
  92. messageListeners(event);
  93. }
  94. } else {
  95. MessageEvent event(ERROR,"Impossible to read catalog from the server.", __FUNCTION__);
  96. messageListeners(event);
  97. }
  98. }
  99. /**
  100. * Get file name from a given index.
  101. * Param :
  102. * - int index : file index.
  103. *
  104. * Return :
  105. * - std::string : return fileName if exist and "None" else.
  106. **/
  107. string DESC::getFileName(uint64_t index)
  108. {
  109. #ifndef NO_CATALOG
  110. return (index < nbFiles) ? fileList[index] : "None";
  111. #else
  112. return (index < nbFiles) ? std::to_string(index) : "None";
  113. #endif
  114. }
  115. /**
  116. * Get file size frome a given index.
  117. * Param :
  118. * - int index : file index.
  119. *
  120. * Return :
  121. * - int : return fileSize of exist and 0 else.
  122. *
  123. **/
  124. uint64_t DESC::getFileSize(uint64_t index)
  125. {
  126. #ifndef NO_CATALOG
  127. return (index < fileSize.size()) ? fileSize[index] : -1;
  128. #else
  129. return (index < nbFiles) ? maxFileSize : -1;
  130. #endif
  131. }
  132. /**
  133. * Get number of files.
  134. * Return :
  135. * - unsigned int : number of files.
  136. **/
  137. uint64_t DESC::getFilesNum()
  138. {
  139. return nbFiles;
  140. }
  141. /**
  142. * Get the biggest file.
  143. * Return :
  144. * - int : biggest file size.
  145. *
  146. **/
  147. uint64_t DESC::getMaxFileSize()
  148. {
  149. return maxFileSize;
  150. }
  151. /**
  152. * Get file list.
  153. * Return :
  154. * - const std::vector<string>& : constant reference to the fileList attribute.
  155. **/
  156. const vector<string>& DESC::getFileList()
  157. {
  158. return fileList;
  159. }
  160. DESC::~DESC()
  161. {
  162. fileList.clear();
  163. fileSize.clear();
  164. }