Skip to content

Commit c30749c

Browse files
committed
Convert old style .tle files to new ones (only Qt)
1 parent a2d59f6 commit c30749c

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

src/aio.c

+80
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,78 @@
4949
#define IS_REF(s) (!strcmp (CSTD_CAST (s),REF_DATA) || !strcmp (CSTD_CAST (s),ALT_REF_DATA))
5050
#define IS_FILE(s) (!strcmp (CSTD_CAST (s),FILE_DATA) || !strcmp (CSTD_CAST (s),ALT_FILE_DATA))
5151

52+
// A standard solution to replace a substring in a char array.
53+
// Taken from https://stackoverflow.com/a/779960
54+
55+
// You must free the result if result is non-NULL.
56+
char *str_replace(char *orig, char *rep, char *with) {
57+
char *result; // the return string
58+
char *ins; // the next insert point
59+
char *tmp; // varies
60+
int len_rep; // length of rep (the string to remove)
61+
int len_with; // length of with (the string to replace rep with)
62+
int len_front; // distance between rep and end of last rep
63+
int count; // number of replacements
64+
65+
// sanity checks and initialization
66+
if (!orig || !rep)
67+
return NULL;
68+
len_rep = strlen(rep);
69+
if (len_rep == 0)
70+
return NULL; // empty rep causes infinite loop during count
71+
if (!with)
72+
with = "";
73+
len_with = strlen(with);
74+
75+
// count the number of replacements needed
76+
ins = orig;
77+
for (count = 0; tmp = strstr(ins, rep); ++count) {
78+
ins = tmp + len_rep;
79+
}
80+
81+
tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1);
82+
83+
if (!result)
84+
return NULL;
85+
86+
// first time through the loop, all the variable are set correctly
87+
// from here on,
88+
// tmp points to the end of the result string
89+
// ins points to the next occurrence of rep in orig
90+
// orig points to the remainder of orig after "end of rep"
91+
while (count--) {
92+
ins = strstr(orig, rep);
93+
len_front = ins - orig;
94+
tmp = strncpy(tmp, orig, len_front) + len_front;
95+
tmp = strcpy(tmp, with) + len_with;
96+
orig += len_front + len_rep; // move to next "end of rep"
97+
}
98+
strcpy(tmp, orig);
99+
return result;
100+
}
101+
102+
/* Convert input to the new format.
103+
* input:
104+
* text - the content written by using the old style.
105+
* output:
106+
* the content by using the new style.
107+
*/
108+
static xmlChar *
109+
aio_convert_old_new (xmlChar * text)
110+
{
111+
char * old_text = (char *) text;
112+
char * new_text;
113+
114+
#define REPLACEMENTS 11
115+
char from[REPLACEMENTS][5] = {"&", "|", "~", "$", "%", "@", "#", "!", "^", ":", ">"};
116+
char to[REPLACEMENTS][5] = {"∧", "∨", "¬", "→", "↔", "∀", "∃", "⊤", "⊥", "∈", "∅"};
117+
for (int i = 0; i < REPLACEMENTS; i++) {
118+
new_text = str_replace(old_text, from[i], to[i]);
119+
old_text = new_text;
120+
}
121+
return (xmlChar *) new_text;
122+
}
123+
52124
/* Gets the first attribute from an xml stream.
53125
* input:
54126
* xml - the xml stream to get the attribute from.
@@ -71,7 +143,11 @@ aio_get_first_attribute (xmlTextReader * xml, xmlChar ** name)
71143
return NULL;
72144

73145
buffer = xmlTextReaderValue (xml);
146+
#if defined(QT_CORE_LIB)
147+
return aio_convert_old_new(buffer);
148+
#else
74149
return buffer;
150+
#endif
75151
}
76152

77153
/* Gets the next attribute from an xml stream.
@@ -96,7 +172,11 @@ aio_get_next_attribute (xmlTextReader * xml, xmlChar ** name)
96172
return NULL;
97173

98174
buffer = xmlTextReaderValue (xml);
175+
#if defined(QT_CORE_LIB)
176+
return aio_convert_old_new(buffer);
177+
#else
99178
return buffer;
179+
#endif
100180
}
101181

102182
///* Write the line number of a sentence object to an XML stream.

0 commit comments

Comments
 (0)