csv_load assumes the first line in a csv file is a key line, defining entries and their number. First line should not contain 'empty column', i.e. two adjacent commas. Each next line should contain the same number of entries. However, empty columns (two adjacent commas) are allowed. \param rig - a pointer to the rig \param infilename - a string with a file name to wri
| 105 | \param infilename - a string with a file name to write to |
| 106 | */ |
| 107 | int csv_load(RIG *rig, const char *infilename) |
| 108 | { |
| 109 | int status = RIG_OK; |
| 110 | FILE *f; |
| 111 | char *key_list[ 64 ]; |
| 112 | char *value_list[ 64 ]; |
| 113 | char keys[ 256 ]; |
| 114 | char line[ 256 ]; |
| 115 | channel_t chan; |
| 116 | |
| 117 | f = fopen(infilename, "r"); |
| 118 | |
| 119 | if (!f) |
| 120 | { |
| 121 | return -1; |
| 122 | } |
| 123 | |
| 124 | /* First read the first line, containing the key */ |
| 125 | if (fgets(keys, sizeof(keys), f) != NULL) |
| 126 | { |
| 127 | |
| 128 | /* fgets stores '\n' in a buffer, get rid of it */ |
| 129 | keys[ strlen(keys) - 1 ] = '\0'; |
| 130 | printf("Read the key: %s\n", keys); |
| 131 | |
| 132 | /* Tokenize the key list */ |
| 133 | if (!tokenize_line(keys, |
| 134 | key_list, |
| 135 | sizeof(key_list) / sizeof(char *), |
| 136 | ',')) |
| 137 | { |
| 138 | fprintf(stderr, |
| 139 | "Invalid (possibly too long or empty) key line, cannot continue.\n"); |
| 140 | fclose(f); |
| 141 | return -1; |
| 142 | } |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | /* File exists, but is empty */ |
| 147 | fclose(f); |
| 148 | return -1; |
| 149 | } |
| 150 | |
| 151 | /* Next, read the file line by line */ |
| 152 | while (fgets(line, sizeof line, f) != NULL) |
| 153 | { |
| 154 | /* Tokenize the line */ |
| 155 | if (!tokenize_line(line, |
| 156 | value_list, |
| 157 | sizeof(value_list) / sizeof(char *), |
| 158 | ',')) |
| 159 | { |
| 160 | |
| 161 | fprintf(stderr, "Invalid (possibly too long or empty) line ignored\n"); |
| 162 | continue; |
| 163 | } |
| 164 |
no test coverage detected