-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
72 lines (64 loc) · 2.13 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/// @file test.c
///
/// @copyright Copyright (c) Mail.Ru Group, 2016. All rights reserved. MIT License.
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "petrovich.h"
static void print_usage(void)
{
fprintf(stderr, "Usage: petr_test <type> <gender> name\n"
" type: first | middle | last\n"
" gender: male | female\n");
}
int main(int argc, char **argv)
{
if (argc != 4) {
fprintf(stderr, "Invalid number of arguments\n");
goto err;
}
const char *kind_str = argv[1];
petr_name_kind_t kind;
if (strcmp(kind_str, "first") == 0) {
kind = NAME_FIRST;
} else if (strcmp(kind_str, "middle") == 0) {
kind = NAME_MIDDLE;
} else if (strcmp(kind_str, "last") == 0) {
kind = NAME_LAST;
} else {
fprintf(stderr, "Invalid name kind\n");
goto err;
}
petr_gender_t gender;
const char *gender_str = argv[2];
if (strcmp(gender_str, "male") == 0) {
gender = GEND_MALE;
} else if (strcmp(gender_str, "female") == 0) {
gender = GEND_FEMALE;
} else {
fprintf(stderr, "Invalid gender\n");
goto err;
}
const char *name_str = argv[3];
size_t name_len = strlen(name_str);
petr_context_t *ctx;
int rc = petr_init_from_file("rules.yml", &ctx);
if (rc != 0) {
fprintf(stderr, "Failed to read the rules");
return 1;
}
char buf[1024];
for (int dest_case = CASE_NOMINATIVE; dest_case <= CASE_PREPOSITIONAL; dest_case++) {
size_t res_size;
int rc = petr_inflect(ctx, name_str, name_len, kind, gender, dest_case, buf, sizeof(buf), &res_size);
if (rc == 0)
printf("%.*s\n", (int)res_size, buf);
else
printf("ERROR\n");
}
petr_free_context(ctx);
return 0;
err:
print_usage();
return 1;
}