Skip to content

Instantly share code, notes, and snippets.

@ei-grad
Created June 14, 2014 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ei-grad/b9bdcb4ec3b77575b235 to your computer and use it in GitHub Desktop.
Save ei-grad/b9bdcb4ec3b77575b235 to your computer and use it in GitHub Desktop.
Convert Paradox DB table to CSV
// sudo apt-get install pxlib
// gcc -std=c99 -lpx px2csv.c -o px2csv
#include <paradox.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
FILE *out;
int ret;
int numrecords, numfields;
pxdoc_t *pxdoc;
char * buf;
PX_boot();
pxdoc = PX_new();
if (argc == 3) {
PX_open_file(pxdoc, argv[1]);
out = fopen(argv[2], "w");
} else {
fprintf(stderr, "usage: px2csv <infile.db> <outfile.csv>\n");
exit(1);
}
numrecords = PX_get_num_records(pxdoc);
numfields = PX_get_num_fields(pxdoc);
fprintf(stderr, "num records: %d\n", numrecords);
for (int i=0; i<numrecords; i++) {
struct px_val ** record;
record = PX_retrieve_record(pxdoc, i);
for (int j=0; j < numfields; j++) {
if (record == NULL) {
fprintf(stderr, "PX_retrieve_record failed for record %d in field %d\n", i, j);
} else if ((*record)->isnull == 1) {
fprintf(out, "NULL");
} else {
switch ((*record)->type) {
case pxfAlpha:
fprintf(out, "%s", (*record)->value.str.val);
break;
case pxfShort:
case pxfLong:
case pxfDate:
case pxfTime:
case pxfLogical:
case pxfAutoInc:
fprintf(out, "%ld", (*record)->value.lval);
break;
case pxfTimestamp:
case pxfNumber:
case pxfCurrency:
fprintf(out, "%lf", (*record)->value.dval);
break;
default:
fprintf(out, "<unknown_type:%d>", (*record)->type);
break;
}
}
record++;
if (j < numfields - 1) fprintf(out, ":");
}
fprintf(out, "\n");
}
fclose(out);
PX_close(pxdoc);
PX_delete(pxdoc);
PX_shutdown();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment