aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ecgen.c3
-rw-r--r--src/exhaustive/exhaustive.c6
-rw-r--r--src/invalid/invalid.c9
-rw-r--r--src/io/output.c90
-rw-r--r--src/io/output.h88
5 files changed, 151 insertions, 45 deletions
diff --git a/src/ecgen.c b/src/ecgen.c
index 987a495..f482fcd 100644
--- a/src/ecgen.c
+++ b/src/ecgen.c
@@ -60,8 +60,7 @@ bool init(void) {
pari_sp ltop = avma;
pari_CATCH(e_FILE) {}
pari_TRY { ellmodulareqn(2, -1, -1); }
- pari_ENDCATCH
- avma = ltop;
+ pari_ENDCATCH avma = ltop;
// open outfile
output_init(&cfg);
diff --git a/src/exhaustive/exhaustive.c b/src/exhaustive/exhaustive.c
index 9202c7a..8c1e0b9 100644
--- a/src/exhaustive/exhaustive.c
+++ b/src/exhaustive/exhaustive.c
@@ -3,6 +3,7 @@
* Copyright (C) 2017 J08nY
*/
#include "exhaustive.h"
+#include <io/config.h>
#include "io/output.h"
#include "math/arg.h"
#include "math/curve.h"
@@ -205,6 +206,7 @@ int exhaustive_do(config_t *cfg) {
exhaustive_ainit(argss, cfg);
exhaustive_uinit(unrolls, cfg);
+ output_o_begin(cfg);
for (unsigned long i = 0; i < cfg->count; ++i) {
curve_t *curve = curve_new();
if (!exhaustive_gen_retry(curve, cfg, generators, argss, unrolls,
@@ -213,8 +215,12 @@ int exhaustive_do(config_t *cfg) {
return 1;
}
output_o(curve, cfg);
+ if (i != cfg->count - 1) {
+ output_o_separator(cfg);
+ }
curve_free(&curve);
}
+ output_o_end(cfg);
exhaustive_quit(argss);
return 0;
diff --git a/src/invalid/invalid.c b/src/invalid/invalid.c
index 20a7063..18224e8 100644
--- a/src/invalid/invalid.c
+++ b/src/invalid/invalid.c
@@ -155,6 +155,9 @@ static size_t invalid_curves(curve_t *curve, config_t *cfg, pari_ulong *primes,
curves[i] = curve_new_copy(invalid);
}
output_o(curves[i], cfg);
+ if (ncurves != nprimes - 1) {
+ output_o_separator(cfg);
+ }
ncurves++;
count++;
}
@@ -241,6 +244,9 @@ static size_t invalid_curves_threaded(curve_t *curve, config_t *cfg,
for (size_t i = 0; i < nprimes; ++i) {
if (old_states[i] != states[i] && states[i] == STATE_GENERATED) {
output_o(local_curves[i], cfg);
+ if (generated != nprimes) {
+ output_o_separator(cfg);
+ }
old_states[i] = states[i];
}
}
@@ -285,7 +291,9 @@ int invalid_do(config_t *cfg) {
curve_free(&curve);
return 1;
}
+ output_o_begin(cfg);
output_o(curve, cfg);
+ output_o_separator(cfg);
// now, generate primes upto order^2
pari_ulong *primes;
@@ -316,6 +324,7 @@ int invalid_do(config_t *cfg) {
ncurves = invalid_curves_threaded(curve, cfg, primes, nprimes, curves,
invalid_gen, unrolls);
}
+ output_o_end(cfg);
for (size_t i = 0; i < ncurves; ++i) {
curve_free(&curves[i]);
diff --git a/src/io/output.c b/src/io/output.c
index fd83965..6fe8802 100644
--- a/src/io/output.c
+++ b/src/io/output.c
@@ -11,6 +11,16 @@
FILE *out;
FILE *verbose;
+char *output_malloc(const char *what) {
+ char *s = pari_malloc(sizeof(char) * (strlen(what) + 1));
+ if (!s) {
+ perror("Couldn't malloc.");
+ exit(1);
+ }
+ strcpy(s, what);
+ return s;
+}
+
char *output_scsv(curve_t *curve, const config_t *cfg) {
pari_sp ltop = avma;
GEN vector = curve_params(curve);
@@ -48,15 +58,11 @@ char *output_scsv(curve_t *curve, const config_t *cfg) {
return result;
}
-void output_fcsv(FILE *out, curve_t *curve, const config_t *cfg) {
- char *string = output_scsv(curve, cfg);
- fprintf(out, "%s\n", string);
- free(string);
-}
+char *output_scsv_separator(const config_t *cfg) { return output_malloc("\n"); }
-void output_csv(curve_t *curve, const config_t *cfg) {
- output_fcsv(out, curve, cfg);
-}
+char *output_scsv_begin(const config_t *cfg) { return NULL; }
+
+char *output_scsv_end(const config_t *cfg) { return output_malloc("\n"); }
static JSON_Value *output_jjson(curve_t *curve, const config_t *cfg) {
pari_sp ltop = avma;
@@ -176,16 +182,56 @@ char *output_sjson(curve_t *curve, const config_t *cfg) {
return result;
}
-void output_fjson(FILE *out, curve_t *curve, const config_t *cfg) {
- char *s = output_sjson(curve, cfg);
- fprintf(out, "%s\n", s);
- json_free_serialized_string(s);
+char *output_sjson_separator(const config_t *cfg) {
+ return output_malloc(",\n");
}
-void output_json(curve_t *curve, const config_t *cfg) {
- output_fjson(out, curve, cfg);
+char *output_sjson_begin(const config_t *cfg) { return output_malloc("[\n"); }
+
+char *output_sjson_end(const config_t *cfg) { return output_malloc("]\n"); }
+
+void output_f(FILE *out, curve_t *curve, const config_t *cfg) {
+ char *s = output_s(curve, cfg);
+ if (s) {
+ fprintf(out, "%s", s);
+ pari_free(s);
+ }
}
+void output_o(curve_t *curve, const config_t *cfg) {
+ output_f(out, curve, cfg);
+}
+
+void output_f_separator(FILE *out, const config_t *cfg) {
+ char *s = output_s_separator(cfg);
+ if (s) {
+ fprintf(out, "%s", s);
+ pari_free(s);
+ }
+}
+
+void output_o_separator(const config_t *cfg) { output_f_separator(out, cfg); }
+
+void output_f_begin(FILE *out, const config_t *cfg) {
+ char *s = output_s_begin(cfg);
+ if (s) {
+ fprintf(out, "%s", s);
+ pari_free(s);
+ }
+}
+
+void output_o_begin(const config_t *cfg) { output_f_begin(out, cfg); }
+
+void output_f_end(FILE *out, const config_t *cfg) {
+ char *s = output_s_end(cfg);
+ if (s) {
+ fprintf(out, "%s", s);
+ pari_free(s);
+ }
+}
+
+void output_o_end(const config_t *cfg) { output_f_end(out, cfg); }
+
void output_init(const config_t *cfg) {
json_set_allocation_functions(pari_malloc, pari_free);
@@ -212,16 +258,20 @@ void output_init(const config_t *cfg) {
setvbuf(verbose, NULL, _IONBF, 0);
switch (cfg->format) {
- case FORMAT_JSON:
+ case FORMAT_JSON: {
output_s = &output_sjson;
- output_f = &output_fjson;
- output_o = &output_json;
+ output_s_separator = &output_sjson_separator;
+ output_s_begin = &output_sjson_begin;
+ output_s_end = &output_sjson_end;
break;
- case FORMAT_CSV:
+ }
+ case FORMAT_CSV: {
output_s = &output_scsv;
- output_f = &output_fcsv;
- output_o = &output_csv;
+ output_s_separator = &output_scsv_separator;
+ output_s_begin = &output_scsv_begin;
+ output_s_end = &output_scsv_end;
break;
+ }
}
}
diff --git a/src/io/output.h b/src/io/output.h
index bf85e60..52719c3 100644
--- a/src/io/output.h
+++ b/src/io/output.h
@@ -20,20 +20,25 @@
*/
char *output_scsv(curve_t *curve, const config_t *cfg);
-/**
- *
- * @param out
- * @param curve
- * @param config
- */
-void output_fcsv(FILE *out, curve_t *curve, const config_t *cfg);
+char *output_scsv_separator(const config_t *cfg);
+
+char *output_scsv_begin(const config_t *cfg);
+
+char *output_scsv_end(const config_t *cfg);
/**
*
* @param curve
* @param config
+ * @return
*/
-void output_csv(curve_t *curve, const config_t *cfg);
+char *output_sjson(curve_t *curve, const config_t *cfg);
+
+char *output_sjson_separator(const config_t *cfg);
+
+char *output_sjson_begin(const config_t *cfg);
+
+char *output_sjson_end(const config_t *cfg);
/**
*
@@ -41,7 +46,7 @@ void output_csv(curve_t *curve, const config_t *cfg);
* @param config
* @return
*/
-char *output_sjson(curve_t *curve, const config_t *cfg);
+char *(*output_s)(curve_t *curve, const config_t *cfg);
/**
*
@@ -49,37 +54,74 @@ char *output_sjson(curve_t *curve, const config_t *cfg);
* @param curve
* @param config
*/
-void output_fjson(FILE *out, curve_t *curve, const config_t *cfg);
+void output_f(FILE *out, curve_t *curve, const config_t *cfg);
/**
*
* @param curve
* @param config
*/
-void output_json(curve_t *curve, const config_t *cfg);
+void output_o(curve_t *curve, const config_t *cfg);
/**
- *
- * @param curve
- * @param config
+ * @brief
+ * @param cfg
* @return
*/
-char *(*output_s)(curve_t *curve, const config_t *cfg);
+char *(*output_s_separator)(const config_t *cfg);
/**
- *
+ * @brief
* @param out
- * @param curve
- * @param config
+ * @param cfg
*/
-void (*output_f)(FILE *out, curve_t *curve, const config_t *cfg);
+void output_f_separator(FILE *out, const config_t *cfg);
/**
- *
- * @param curve
- * @param config
+ * @brief
+ * @param cfg
+ */
+void output_o_separator(const config_t *cfg);
+
+/**
+ * @brief
+ * @param cfg
+ * @return
+ */
+char *(*output_s_begin)(const config_t *cfg);
+
+/**
+ * @brief
+ * @param out
+ * @param cfg
+ */
+void output_f_begin(FILE *out, const config_t *cfg);
+
+/**
+ * @brief
+ * @param cfg
+ */
+void output_o_begin(const config_t *cfg);
+
+/**
+ * @brief
+ * @param cfg
+ * @return
+ */
+char *(*output_s_end)(const config_t *cfg);
+
+/**
+ * @brief
+ * @param out
+ * @param cfg
+ */
+void output_f_end(FILE *out, const config_t *cfg);
+
+/**
+ * @brief
+ * @param cfg
*/
-void (*output_o)(curve_t *curve, const config_t *cfg);
+void output_o_end(const config_t *cfg);
/**
*