+#define CSV_LAST (-1)
+#define CSV_UNDEFINED (-2)
+#define CSV_SPECIAL(X) (-3 - (X))
+#define CSV_IS_SPECIAL(X) ((X) <= -3)
+
+static int
+csv_export_common(FILE *out, struct db_enumerator e,
+ int fields[], void (*special_func)(FILE *, int, int))
+{
+ int i;
+
+ db_enumerate_items(e) {
+ for(i = 0; fields[i] != CSV_LAST; i++) {
+ if(fields[i] == CSV_UNDEFINED)
+ fprintf(out, "\"\"");
+ else if(CSV_IS_SPECIAL(fields[i])) {
+ if(special_func)
+ (*special_func)(out, e.item, fields[i]);
+ } else
+ /*fprintf(out,(
+ strchr(safe_str(database[e.item][fields[i]]), ',') ||
+ strchr(safe_str(database[e.item][fields[i]]), '\"')) ?
+ "\"%s\"" : "%s",
+ safe_str(database[e.item][fields[i]])
+ );*/
+ fprintf(out, "\"%s\"",
+ safe_str(database[e.item][fields[i]]));
+
+ if(fields[i + 1] != CSV_LAST)
+ fputc(',', out);
+ }
+ fputc('\n', out);
+ }
+
+ return 0;
+}
+