| 1119 | } |
| 1120 | |
| 1121 | int get_num_elements_in_file (const char *file_name, int *num_elements) |
| 1122 | { |
| 1123 | FILE *fp; |
| 1124 | fp = fopen (file_name, "r"); |
| 1125 | if (fp == NULL) |
| 1126 | { |
| 1127 | data_logger->error ("Couldn't read file {}", file_name); |
| 1128 | return (int)BrainFlowExitCodes::INVALID_ARGUMENTS_ERROR; |
| 1129 | } |
| 1130 | |
| 1131 | char buf[4096]; |
| 1132 | int total_rows = 0; |
| 1133 | |
| 1134 | // count rows |
| 1135 | char c; |
| 1136 | for (c = getc (fp); !feof (fp); c = getc (fp)) |
| 1137 | { |
| 1138 | if (c == '\n') |
| 1139 | { |
| 1140 | total_rows++; |
| 1141 | } |
| 1142 | } |
| 1143 | if (total_rows == 0) |
| 1144 | { |
| 1145 | *num_elements = 0; |
| 1146 | fclose (fp); |
| 1147 | data_logger->error ("Empty file {}", file_name); |
| 1148 | return (int)BrainFlowExitCodes::EMPTY_BUFFER_ERROR; |
| 1149 | } |
| 1150 | |
| 1151 | fseek (fp, 0, SEEK_SET); |
| 1152 | while (fgets (buf, sizeof (buf), fp) != NULL) |
| 1153 | { |
| 1154 | std::string tsv_string (buf); |
| 1155 | std::stringstream ss (tsv_string); |
| 1156 | std::vector<std::string> splitted; |
| 1157 | std::string tmp; |
| 1158 | char sep = '\t'; |
| 1159 | if (tsv_string.find ('\t') == std::string::npos) |
| 1160 | { |
| 1161 | sep = ','; |
| 1162 | } |
| 1163 | while (std::getline (ss, tmp, sep)) |
| 1164 | { |
| 1165 | if (tmp != "\n") |
| 1166 | { |
| 1167 | splitted.push_back (tmp); |
| 1168 | } |
| 1169 | } |
| 1170 | *num_elements = (int)splitted.size () * total_rows; |
| 1171 | fclose (fp); |
| 1172 | return (int)BrainFlowExitCodes::STATUS_OK; |
| 1173 | } |
| 1174 | *num_elements = 0; |
| 1175 | fclose (fp); |
| 1176 | data_logger->error ("File contents", file_name); |
| 1177 | return (int)BrainFlowExitCodes::EMPTY_BUFFER_ERROR; |
| 1178 | } |