Line data Source code
1 : /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 : /*
3 : * tag.c
4 : * Copyright (C) 2011-2012 Akira TAGOH
5 : *
6 : * Authors:
7 : * Akira TAGOH <akira@tagoh.org>
8 : *
9 : * You may distribute under the terms of either the GNU
10 : * Lesser General Public License or the Mozilla Public
11 : * License, as specified in the README file.
12 : */
13 : #ifdef HAVE_CONFIG_H
14 : #include "config.h"
15 : #endif
16 :
17 : #include <stdio.h>
18 : #include <stdlib.h>
19 : #include <locale.h>
20 : #include "langtag.h"
21 : #include "lt-utils.h"
22 :
23 : int
24 0 : main(int argc,
25 : char **argv)
26 : {
27 : lt_tag_t *tag;
28 :
29 0 : setlocale(LC_ALL, "");
30 :
31 0 : lt_db_set_datadir(TEST_DATADIR);
32 0 : lt_db_initialize();
33 0 : tag = lt_tag_new();
34 0 : if (lt_strcmp0(argv[1], "help") == 0) {
35 : help:
36 0 : printf("Usage: %s <command> ...\n"
37 : "commands: canonicalize, dump, from_locale, lookup, match, to_locale, transform\n",
38 : argv[0]);
39 0 : } else if (lt_strcmp0(argv[1], "canonicalize") == 0) {
40 : char *s;
41 :
42 0 : if (lt_tag_parse(tag, argv[2], NULL)) {
43 0 : s = lt_tag_canonicalize(tag, NULL);
44 0 : printf("%s -> %s\n", argv[2], s);
45 0 : free(s);
46 : }
47 0 : } else if (lt_strcmp0(argv[1], "dump") == 0) {
48 0 : if (lt_tag_parse(tag, argv[2], NULL))
49 0 : lt_tag_dump(tag);
50 0 : } else if (lt_strcmp0(argv[1], "from_locale") == 0) {
51 0 : lt_tag_unref(tag);
52 0 : tag = lt_tag_convert_from_locale(NULL);
53 0 : if (tag) {
54 0 : const char *s = lt_tag_get_string(tag);
55 :
56 0 : printf("Tag: %s\n", s);
57 0 : lt_tag_dump(tag);
58 : }
59 0 : } else if (lt_strcmp0(argv[1], "match") == 0) {
60 0 : if (lt_tag_parse(tag, argv[2], NULL)) {
61 0 : if (lt_tag_match(tag, argv[3], NULL))
62 0 : printf("%s matches with %s\n", argv[3], argv[2]);
63 : else
64 0 : printf("%s doesn't match with %s\n", argv[3], argv[2]);
65 : }
66 0 : } else if (lt_strcmp0(argv[1], "lookup") == 0) {
67 0 : if (lt_tag_parse(tag, argv[2], NULL)) {
68 0 : char *result = lt_tag_lookup(tag, argv[3], NULL);
69 0 : if (result)
70 0 : printf("%s\n", result);
71 : else
72 0 : printf("%s doesn't match with %s\n", argv[3], argv[2]);
73 0 : free(result);
74 : }
75 0 : } else if (lt_strcmp0(argv[1], "to_locale") == 0) {
76 : char *l;
77 :
78 0 : if (lt_tag_parse(tag, argv[2], NULL)) {
79 0 : l = lt_tag_convert_to_locale(tag, NULL);
80 0 : printf("%s -> %s\n", argv[2], l);
81 0 : free(l);
82 : }
83 0 : } else if (lt_strcmp0(argv[1], "transform") == 0) {
84 : char *r;
85 :
86 0 : if (lt_tag_parse(tag, argv[2], NULL)) {
87 0 : r = lt_tag_transform(tag, NULL);
88 0 : printf("%s -> %s\n", argv[2], r);
89 0 : free(r);
90 : }
91 : } else {
92 0 : goto help;
93 : }
94 0 : if (tag)
95 0 : lt_tag_unref(tag);
96 0 : lt_db_finalize();
97 :
98 0 : return 0;
99 : }
|