| 1009 | } |
| 1010 | |
| 1011 | int read_file (double *data, int *num_rows, int *num_cols, const char *file_name, int num_elements) |
| 1012 | { |
| 1013 | if (num_elements <= 0) |
| 1014 | { |
| 1015 | data_logger->error ("Nummber or elements must be greater than 0."); |
| 1016 | return (int)BrainFlowExitCodes::INVALID_ARGUMENTS_ERROR; |
| 1017 | } |
| 1018 | FILE *fp; |
| 1019 | fp = fopen (file_name, "r"); |
| 1020 | if (fp == NULL) |
| 1021 | { |
| 1022 | data_logger->error ("Couldn't read file {}", file_name); |
| 1023 | return (int)BrainFlowExitCodes::INVALID_ARGUMENTS_ERROR; |
| 1024 | } |
| 1025 | |
| 1026 | char buf[4096]; |
| 1027 | // rows and cols in tsv file, in data array its transposed! |
| 1028 | int total_rows = 0; |
| 1029 | int total_cols = 0; |
| 1030 | |
| 1031 | // count rows |
| 1032 | char c; |
| 1033 | for (c = getc (fp); !feof (fp); c = getc (fp)) |
| 1034 | { |
| 1035 | if (c == '\n') |
| 1036 | { |
| 1037 | total_rows++; |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | fseek (fp, 0, SEEK_SET); |
| 1042 | int current_row = 0; |
| 1043 | int cur_pos = 0; |
| 1044 | while (fgets (buf, sizeof (buf), fp) != NULL) |
| 1045 | { |
| 1046 | std::string tsv_string (buf); |
| 1047 | std::stringstream ss (tsv_string); |
| 1048 | std::vector<std::string> splitted; |
| 1049 | std::string tmp; |
| 1050 | char sep = '\t'; |
| 1051 | if (tsv_string.find ('\t') == std::string::npos) |
| 1052 | { |
| 1053 | sep = ','; |
| 1054 | } |
| 1055 | while (std::getline (ss, tmp, sep)) |
| 1056 | { |
| 1057 | if (tmp != "\n") |
| 1058 | { |
| 1059 | splitted.push_back (tmp); |
| 1060 | } |
| 1061 | } |
| 1062 | if ((total_cols != 0) && (total_cols != (int)splitted.size ())) |
| 1063 | { |
| 1064 | data_logger->error ("some rows have more cols than others, invalid input file"); |
| 1065 | fclose (fp); |
| 1066 | return (int)BrainFlowExitCodes::INVALID_ARGUMENTS_ERROR; |
| 1067 | } |
| 1068 | total_cols = (int)splitted.size (); |