- buf[file_size] = 0;
- fclose(fp);
-
- // Preliminary memory allocation
- *item_list = calloc(current_max_size, sizeof(char*));
- tmp_str = malloc(MAX_TMP_STR);
- if (tmp_str == NULL || *item_list == NULL) {
- SAFE_FREE(*item_list);
- SAFE_FREE(buf);
- SAFE_FREE(tmp_str);
- media_content_error("Out of Memory");
- return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
- }
- // Here we create format string for sscanf(...) that allows to get a line from buffer
- char format[25];
- snprintf(format, 25, "%%%d[^\n]", MAX_TMP_STR);
-
- // This cycle gets lines one by one from buffer till the end of buffer. Empty line ("\n") must be treated specifically
- while ((sscanf(tmp_buf, format, tmp_str) == 1) || (*tmp_buf == '\n')) {
- if (*tmp_buf == '\n') {// Check if there is an empty line, skip '\n' symbol
- tmp_buf += 1;
-
- if (tmp_buf < (buf + file_size))
- continue; // We are still in buffer
- else
- break; // Empty line was in the end of buffer
- }
-
- tmp_str_len = strlen(tmp_str); // Save the length of line
-
- if (tmp_str[0] != '#') { // Check that the line is not a comment
- if (!(current_index < (current_max_size - 1))) { // Check if we have completely filled record array
- // Expand array size and relocate the array (records will be copied into new one)
- current_max_size += PLAYLIST_ARRAY_EXPAND;
- char **tmp_ptr = calloc(current_max_size, sizeof(char*));
- if (tmp_ptr == NULL) {
- __media_playlist_destroy_import_item(*item_list, current_index);
- SAFE_FREE(buf);
- SAFE_FREE(tmp_str);
- media_content_error("Out of Memory");
- return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
- }
- memmove(tmp_ptr, *item_list, sizeof(char*) * current_index);
- SAFE_FREE(*item_list);
- *item_list = tmp_ptr;
- }
-
- // Save new file path (current string in tmp_str)
- (*item_list)[current_index] = malloc(tmp_str_len + 1);
- if ((*item_list)[current_index] == NULL) {
- __media_playlist_destroy_import_item(*item_list, current_index);
- SAFE_FREE(buf);
- SAFE_FREE(tmp_str);
- media_content_error("Out of Memory");
- return MEDIA_CONTENT_ERROR_OUT_OF_MEMORY;
- }
- memmove((*item_list)[current_index], tmp_str, tmp_str_len + 1);
-
- // Increase the index of buffer
- current_index += 1;
- }