You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
2.7 KiB

  1. /*
  2. * readLabFile.cpp
  3. *
  4. * Created on: 21.11.2012
  5. * Author: Gereon Kremer
  6. */
  7. #include "parser.h"
  8. #include "readLabFile.h"
  9. #include "src/exceptions/wrong_file_format.h"
  10. #include "src/exceptions/file_IO_exception.h"
  11. #include <cstdlib>
  12. #include <cstdio>
  13. #include <cstring>
  14. #include <clocale>
  15. #include <iostream>
  16. #include <errno.h>
  17. #include <time.h>
  18. #include <sys/stat.h>
  19. #include <sys/mman.h>
  20. #include <fcntl.h>
  21. #include <locale.h>
  22. #include <pantheios/pantheios.hpp>
  23. #include <pantheios/inserters/integer.hpp>
  24. namespace mrmc {
  25. namespace parser {
  26. /*!
  27. * Reads a .lab file and puts the result in a labeling structure.
  28. *
  29. * Labelings created with this method have to be freed with the delete operator.
  30. * @param node_count the number of states.
  31. * @param filename input .lab file's name.
  32. * @return The pointer to the created labeling object.
  33. */
  34. mrmc::models::AtomicPropositionsLabeling * readLabFile(int node_count, const char * filename)
  35. {
  36. /*!
  37. * open file and map to memory
  38. */
  39. struct stat st;
  40. int f = open(filename, O_RDONLY);
  41. if ((f < 0) || (stat(filename, &st) != 0)) {
  42. /*!
  43. *
  44. */
  45. pantheios::log_ERROR("File could not be opened.");
  46. throw mrmc::exceptions::file_IO_exception();
  47. return NULL;
  48. }
  49. char* data = (char*) mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, f, 0);
  50. if (data == (char*)-1)
  51. {
  52. pantheios::log_ERROR("File could not be mapped. Something went wrong with mmap.");
  53. close(f);
  54. throw mrmc::exceptions::file_IO_exception();
  55. return NULL;
  56. }
  57. char* buf = data;
  58. char sep[] = " \n\t";
  59. uint_fast32_t proposition_count = 0;
  60. size_t cnt = 0;
  61. do {
  62. buf += cnt;
  63. cnt = strcspn(buf, sep);
  64. if (cnt > 0) {
  65. if (strncmp(buf, "#DECLARATION", cnt) == 0) continue;
  66. if (strncmp(buf, "#END", cnt) == 0) break;
  67. proposition_count++;
  68. }
  69. else cnt = 1;
  70. } while (cnt > 0);
  71. mrmc::models::AtomicPropositionsLabeling* result = new mrmc::models::AtomicPropositionsLabeling(node_count, proposition_count);
  72. char proposition[128];
  73. buf = data;
  74. cnt = 0;
  75. do {
  76. buf += cnt;
  77. cnt = strcspn(buf, sep);
  78. if (cnt > 0) {
  79. if (strncmp(buf, "#DECLARATION", cnt) == 0) continue;
  80. if (strncmp(buf, "#END", cnt) == 0) break;
  81. strncpy(proposition, buf, cnt);
  82. proposition[cnt] = '\0';
  83. result->addAtomicProposition(proposition);
  84. }
  85. else cnt = 1;
  86. } while (cnt > 0);
  87. buf += 4;
  88. uint_fast32_t node;
  89. while (1) {
  90. node = strtol(buf, &buf, 10);
  91. while (*buf != '\0') {
  92. cnt = strcspn(buf, sep);
  93. if (cnt == 0) buf++;
  94. else break;
  95. }
  96. strncpy(proposition, buf, cnt);
  97. proposition[cnt] = '\0';
  98. result->addAtomicPropositionToState(proposition, node);
  99. buf += cnt;
  100. }
  101. munmap(data, st.st_size);
  102. close(f);
  103. return result;
  104. }
  105. } //namespace parser
  106. } //namespace mrmc