19 #include "serialize.h" 23 #include "string_operations.h" 31 int serializeToFile(
Licenses* licenses,
char* filename) {
32 FILE* fp = fopen(filename,
"w+");
36 int retCode = serialize(licenses, fp);
41 int serialize(
Licenses* licenses, FILE* fp) {
42 return serializeGArray(licenses->licenses, fp);
45 int serializeGArray(GArray* licenses, FILE* fp) {
47 for (guint i = 0; i < licenses->len; i++) {
48 retCode = serializeOne(license_index(licenses, i), fp);
57 return serializeOneMeta(license, fp) &&
58 serializeOneShortname(license, fp) &&
59 serializeOneTokens(license->tokens, fp);
64 .shortnameLen = strlen(license->shortname),
65 .tokensLen = license->tokens->len };
70 int serializeOneShortname(
License* license, FILE* fp) {
71 return fprintf(fp,
"%s", license->shortname) > 0;
74 int serializeOneTokens(GArray* tokens, FILE* fp) {
75 for (guint i = 0; i < tokens->len; i++) {
76 if (fwrite(tokens_index(tokens,i),
sizeof(
Token), 1, fp) != 1) {
87 Licenses* deserializeFromFile(
char* filename,
unsigned minAdjacentMatches,
unsigned maxLeadingDiff) {
88 FILE* fp = fopen(filename,
"r");
92 Licenses* result = deserialize(fp, minAdjacentMatches, maxLeadingDiff);
97 Licenses* deserialize(FILE* fp,
unsigned minAdjacentMatches,
unsigned maxLeadingDiff) {
98 GArray* licenses = g_array_new(TRUE, FALSE,
sizeof(
License));
102 License license = { .refId = meta.refId };
104 license.shortname = calloc(1,(
size_t) meta.shortnameLen + 1);
105 if(fread(license.shortname,
sizeof(
char), meta.shortnameLen, fp) != meta.shortnameLen){
109 license.tokens = deserializeTokens(fp, meta.tokensLen);
111 g_array_append_vals(licenses, &license, 1);
114 return buildLicenseIndexes(licenses, minAdjacentMatches, maxLeadingDiff);
117 GArray* deserializeTokens(FILE* fp, guint tokensLen) {
118 Token* freadResult = malloc(
sizeof(
Token) * tokensLen);
119 if(fread(freadResult,
sizeof(
Token), tokensLen, fp) != tokensLen){
123 GArray* tokens = g_array_new(FALSE, FALSE,
sizeof(
Token));
124 g_array_append_vals(tokens,