+}
+
+
+enum capture_config_section {
+ CAPTURE_CONFIG_NO_SECTION,
+ CAPTURE_CONFIG_EXCLUSION_SECTION,
+ CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
+ CAPTURE_CONFIG_IGNORE_SECTION,
+};
+
+enum {
+ CAPTURE_CONFIG_INVALID_SECTION,
+ CAPTURE_CONFIG_CHANGED_SECTION,
+ CAPTURE_CONFIG_SAME_SECTION,
+};
+
+static int
+check_config_section(tchar *line, size_t len,
+ enum capture_config_section *cur_section)
+{
+ while (istspace(*line))
+ line++;
+
+ if (*line != T('['))
+ return CAPTURE_CONFIG_SAME_SECTION;
+
+ line++;
+ tchar *endbrace = tstrrchr(line, T(']'));
+ if (!endbrace)
+ return CAPTURE_CONFIG_SAME_SECTION;
+
+ if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
+ *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
+ } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
+ *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
+ } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
+ *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
+ tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
+ "of capture config file\n"),
+ stderr);
+ } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
+ *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
+ tfputs(T("WARNING: Ignoring [AlignmentList] section "
+ "of capture config file\n"),
+ stderr);
+ } else {
+ imagex_error(T("Invalid capture config file section \"%"TS"\""),
+ line - 1);
+ return CAPTURE_CONFIG_INVALID_SECTION;
+ }
+ return CAPTURE_CONFIG_CHANGED_SECTION;
+}
+
+
+static bool
+pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
+ tchar *pat)
+{
+ if (pat_list->num_pats == pat_list->num_allocated_pats) {
+ tchar **pats;
+ size_t num_allocated_pats = pat_list->num_pats + 8;
+
+ pats = realloc(pat_list->pats,
+ num_allocated_pats * sizeof(pat_list->pats[0]));
+ if (!pats) {
+ imagex_error(T("Out of memory!"));
+ return false;
+ }
+ pat_list->pats = pats;
+ pat_list->num_allocated_pats = num_allocated_pats;
+ }
+ pat_list->pats[pat_list->num_pats++] = pat;
+ return true;
+}
+
+static bool
+parse_capture_config_line(tchar *line, size_t len,
+ enum capture_config_section *cur_section,
+ struct wimlib_capture_config *config)
+{
+ tchar *filename;
+ int ret;
+
+ ret = check_config_section(line, len, cur_section);
+ if (ret == CAPTURE_CONFIG_INVALID_SECTION)
+ return false;
+ if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
+ return true;
+
+ switch (*cur_section) {
+ case CAPTURE_CONFIG_NO_SECTION:
+ imagex_error(T("Line \"%"TS"\" is not in a section "
+ "(such as [ExclusionList]"), line);
+ return false;
+ case CAPTURE_CONFIG_EXCLUSION_SECTION:
+ if (parse_filename(&line, &len, &filename) != PARSE_FILENAME_SUCCESS)
+ return false;
+ return pattern_list_add_pattern(&config->exclusion_pats,
+ filename);
+ case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
+ if (parse_filename(&line, &len, &filename) != PARSE_FILENAME_SUCCESS)
+ return false;
+ return pattern_list_add_pattern(&config->exclusion_exception_pats,
+ filename);
+ case CAPTURE_CONFIG_IGNORE_SECTION:
+ return true;
+ }
+ return false;
+}
+
+static int
+parse_capture_config(tchar **contents_p, size_t nchars,
+ struct wimlib_capture_config *config)
+{
+ ssize_t nlines;
+ tchar *p;
+ size_t i;
+ enum capture_config_section cur_section;
+
+ memset(config, 0, sizeof(*config));
+
+ nlines = text_file_count_lines(contents_p, &nchars);
+ if (nlines < 0)
+ return -1;
+
+ cur_section = CAPTURE_CONFIG_NO_SECTION;
+ p = *contents_p;
+ for (i = 0; i < nlines; i++) {
+ tchar *endp = tmemchr(p, T('\n'), nchars);
+ size_t len = endp - p + 1;
+ *endp = T('\0');
+ if (!is_comment_line(p, len))
+ if (!parse_capture_config_line(p, len, &cur_section, config))
+ return -1;
+ p = endp + 1;
+
+ }
+ return 0;