FileIO.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // Created by wrede on 19.04.16.
  3. //
  4. #ifndef GBMOT_FILEIO_H
  5. #define GBMOT_FILEIO_H
  6. #include <string>
  7. #include <fstream>
  8. #include <vector>
  9. #include "../core/ObjectData.h"
  10. #include "../graph/Definitions.h"
  11. namespace util
  12. {
  13. typedef std::vector<std::vector<std::vector<double>>> Vector3d;
  14. typedef std::vector<std::vector<double>> Vector2d;
  15. /**
  16. * Utility class for file in- and output.
  17. */
  18. class FileIO
  19. {
  20. public:
  21. /**
  22. * Reads a CSV file and stores the values in a 3D array.
  23. * The first dimension is the first value of each row, used as a
  24. * index to bundle multiple rows with the same first value into a
  25. * single vector.
  26. * The second dimension is the row in the row bundle.
  27. * The third dimension is the value in that row.
  28. * @param values The 3D array of values to store the read values in
  29. * @param filename The filename to read from
  30. * @param delimiter The delimiter used to separate the values in the file
  31. */
  32. static void ReadCSV(Vector3d& values,
  33. const std::string& filename,
  34. char delimiter = ';');
  35. /**
  36. * Reads a CSV file and stores the values in a 2D array.
  37. * The first dimension is the row and the second the value in that row.
  38. * @param values The 2D array of values to store the read values in
  39. * @param filename The filename to read from
  40. * @param delimiter The delimiter used to separate the values in the file
  41. */
  42. static void ReadCSV(Vector2d& values,
  43. const std::string& filename,
  44. char delimiter = ';');
  45. /**
  46. * Lists all file names in the given folder.
  47. * @param folder The folder to look into
  48. * @param file_names The vector to store the file names into
  49. * @param sort True, if the files should be sorted alphabetically
  50. */
  51. static void ListFiles(const std::string& folder,
  52. std::vector<std::string>& file_names,
  53. bool sort = true);
  54. /**
  55. * Writes the given graph into a CSV file with an format readable by
  56. * Matlab.
  57. * @param graph The graph to write
  58. * @param file_name The name of the file to write into
  59. * @param delimiter The delimiter to use
  60. */
  61. static void WriteCSVMatlab(DirectedGraph& graph,
  62. const std::string& file_name,
  63. char delimiter = ';');
  64. };
  65. }
  66. #endif //GBMOT_FILEIO_H