FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
serialize.c
1 /*
2 Author: Maximilian Huber
3 Copyright (C) 2018, TNG Technology Consulting GmbH
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "serialize.h"
20 
21 #include "monk.h"
22 #include "license.h"
23 #include "string_operations.h"
24 #include <stdio.h>
25 #include <errno.h>
26 
27 /*
28  * serialization
29  */
30 
31 int serializeToFile(Licenses* licenses, char* filename) {
32  FILE* fp = fopen(filename, "w+");
33  if (fp == NULL) {
34  return 0;
35  }
36  int retCode = serialize(licenses, fp);
37  fclose(fp);
38  return retCode;
39 }
40 
41 int serialize(Licenses* licenses, FILE* fp) {
42  return serializeGArray(licenses->licenses, fp);
43 }
44 
45 int serializeGArray(GArray* licenses, FILE* fp) {
46  int retCode;
47  for (guint i = 0; i < licenses->len; i++) {
48  retCode = serializeOne(license_index(licenses, i), fp);
49  if (retCode == 0) {
50  return retCode;
51  }
52  }
53  return 1;
54 }
55 
56 int serializeOne(License* license, FILE* fp) {
57  return serializeOneMeta(license, fp) &&
58  serializeOneShortname(license, fp) &&
59  serializeOneTokens(license->tokens, fp);
60 }
61 
62 int serializeOneMeta(License* license, FILE* fp) {
63  SerializingMeta meta = { .refId = license->refId,
64  .shortnameLen = strlen(license->shortname),
65  .tokensLen = license->tokens->len };
66 
67  return fwrite(&meta, sizeof(SerializingMeta), 1, fp) == 1;
68 }
69 
70 int serializeOneShortname(License* license, FILE* fp) {
71  return fprintf(fp, "%s", license->shortname) > 0;
72 }
73 
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) {
77  return 0;
78  }
79  }
80  return 1;
81 }
82 
83 /*
84  * deserialization
85  */
86 
87 Licenses* deserializeFromFile(char* filename, unsigned minAdjacentMatches, unsigned maxLeadingDiff) {
88  FILE* fp = fopen(filename, "r");
89  if(fp == NULL) {
90  exit(3);
91  }
92  Licenses* result = deserialize(fp, minAdjacentMatches, maxLeadingDiff);
93  fclose(fp);
94  return result;
95 }
96 
97 Licenses* deserialize(FILE* fp, unsigned minAdjacentMatches, unsigned maxLeadingDiff) {
98  GArray* licenses = g_array_new(TRUE, FALSE, sizeof(License));
99 
100  SerializingMeta meta;
101  while (fread(&meta, sizeof(SerializingMeta), 1, fp) == 1) {
102  License license = { .refId = meta.refId };
103 
104  license.shortname = calloc(1,(size_t) meta.shortnameLen + 1);
105  if(fread(license.shortname, sizeof(char), meta.shortnameLen, fp) != meta.shortnameLen){
106  strerror(errno);
107  }
108 
109  license.tokens = deserializeTokens(fp, meta.tokensLen);
110 
111  g_array_append_vals(licenses, &license, 1);
112  }
113 
114  return buildLicenseIndexes(licenses, minAdjacentMatches, maxLeadingDiff);
115 }
116 
117 GArray* deserializeTokens(FILE* fp, guint tokensLen) {
118  Token* freadResult = malloc(sizeof(Token) * tokensLen);
119  if(fread(freadResult, sizeof(Token), tokensLen, fp) != tokensLen){
120  strerror(errno);
121  }
122 
123  GArray* tokens = g_array_new(FALSE, FALSE, sizeof(Token));
124  g_array_append_vals(tokens,
125  freadResult,
126  tokensLen);
127  free(freadResult);
128 
129  return tokens;
130 }
Definition: monk.h:78
Definition: nomos.h:439
Definition: monk.h:66