do not allocate translit. contexts, use initialized static vars

This commit is contained in:
Vlasta Vesely 2018-05-20 08:29:33 +02:00
parent ae59e9c14f
commit 84b79160c8
7 changed files with 23 additions and 35 deletions

View file

@ -163,13 +163,12 @@ static const transliteration_filter_t filters[] = {
NULL NULL
}; };
struct transliteration_context *transliteration_context_iast_czech_alloc() static const struct transliteration_context context = {
.table = table,
.filters = filters
};
const struct transliteration_context *get_iast_czech_transliteration_context()
{ {
struct transliteration_context *context; return &context;
context = malloc(sizeof(*context));
context->table = table;
context->filters = filters;
return context;
} }

View file

@ -5,6 +5,6 @@
#include "transliteration.h" #include "transliteration.h"
struct transliteration_context *transliteration_context_iast_czech_alloc(); const struct transliteration_context *get_iast_czech_transliteration_context();
#endif /* __IAST_CZECH_H */ #endif /* __IAST_CZECH_H */

16
iast.c
View file

@ -1,7 +1,6 @@
/* SPDX-License-Identifier: GPL-2.0 */ /* SPDX-License-Identifier: GPL-2.0 */
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "iast.h" #include "iast.h"
#include "transliteration.h" #include "transliteration.h"
@ -103,13 +102,12 @@ static const transliteration_filter_t filters[] = {
NULL NULL
}; };
struct transliteration_context *transliteration_context_iast_alloc() static const struct transliteration_context context = {
.table = table,
.filters = filters
};
const struct transliteration_context *get_iast_transliteration_context()
{ {
struct transliteration_context *context; return &context;
context = malloc(sizeof(*context));
context->table = table;
context->filters = filters;
return context;
} }

2
iast.h
View file

@ -5,6 +5,6 @@
#include "transliteration.h" #include "transliteration.h"
struct transliteration_context *transliteration_context_iast_alloc(); const struct transliteration_context *get_iast_transliteration_context();
#endif /* __IAST_H */ #endif /* __IAST_H */

12
main.c
View file

@ -36,7 +36,6 @@ const char *usage_str =
static void usage() static void usage()
{ {
fprintf(stdout, "%s\n", usage_str); fprintf(stdout, "%s\n", usage_str);
exit(0);
} }
static char *stdin_read() static char *stdin_read()
@ -67,7 +66,7 @@ int main(int argc, const char **argv)
const char *arg; const char *arg;
const char *queue[argc]; const char *queue[argc];
char *input, *output; char *input, *output;
struct transliteration_context *context; const struct transliteration_context *context;
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
arg = argv[i]; arg = argv[i];
@ -85,6 +84,7 @@ int main(int argc, const char **argv)
continue; continue;
case 'h': case 'h':
usage(); usage();
goto out;
} }
fprintf(stderr, "error: unknown option '%s'\n", arg); fprintf(stderr, "error: unknown option '%s'\n", arg);
@ -104,15 +104,15 @@ int main(int argc, const char **argv)
} }
context = (flags & FLAG_CZECH) context = (flags & FLAG_CZECH)
? transliteration_context_iast_czech_alloc() ? get_iast_czech_transliteration_context()
: transliteration_context_iast_alloc(); : get_iast_transliteration_context();
if (flags & FLAG_STDIN) { if (flags & FLAG_STDIN) {
input = stdin_read(); input = stdin_read();
if (input == NULL) { if (input == NULL) {
fprintf(stderr, "[iast] failed to read from STDIN.\n"); fprintf(stderr, "[iast] failed to read from STDIN.\n");
retval = -1; retval = -1;
goto drop_context; goto out;
} }
output = transliterate_devanagari_to_latin(input, context); output = transliterate_devanagari_to_latin(input, context);
@ -127,8 +127,6 @@ int main(int argc, const char **argv)
free(output); free(output);
} }
drop_context:
transliteration_context_drop(context);
out: out:
return retval; return retval;
} }

View file

@ -48,7 +48,7 @@ static void apply_transliteration_filters(struct syllable *head,
} }
char *transliterate_devanagari_to_latin(const char *text, char *transliterate_devanagari_to_latin(const char *text,
struct transliteration_context *context) const struct transliteration_context *context)
{ {
unsigned int length = strlen(text); unsigned int length = strlen(text);
const char *ptr = text; const char *ptr = text;
@ -90,8 +90,3 @@ char *transliterate_devanagari_to_latin(const char *text,
return tmp; return tmp;
} }
void transliteration_context_drop(struct transliteration_context *context)
{
free(context);
}

View file

@ -23,9 +23,7 @@ struct transliteration_context {
}; };
char *transliterate_devanagari_to_latin(const char *text, char *transliterate_devanagari_to_latin(const char *text,
struct transliteration_context *context); const struct transliteration_context *context);
void transliteration_context_drop(struct transliteration_context *context);
static inline int is_devanagari(unsigned int code) static inline int is_devanagari(unsigned int code)
{ {