1 /* Copyright (c) 2013 - The libcangjie authors.
2 *
3 * This file is part of libcangjie.
4 *
5 * libcangjie is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * libcangjie is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with libcangjie. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "cangjiechar.h"
24 #include "cangjieerrors.h"
25
26
cangjie_char_new(CangjieChar ** c,const char * chchar,const char * code,uint32_t frequency)27 int cangjie_char_new(CangjieChar **c,
28 const char *chchar,
29 const char *code,
30 uint32_t frequency) {
31 CangjieChar *tmp = calloc(1, sizeof(CangjieChar));
32 if (tmp == NULL) {
33 return CANGJIE_NOMEM;
34 }
35
36 // Copy at most 4 bytes, it's a single UTF-8 encoded character
37 strncpy(tmp->chchar, chchar, 4);
38
39 // Copy at most 5 bytes, that's the longest a Cangjie input code can be
40 strncpy(tmp->code, code, 5);
41
42 tmp->frequency = frequency;
43
44 *c = tmp;
45
46 return CANGJIE_OK;
47 }
48
cangjie_char_free(CangjieChar * c)49 int cangjie_char_free(CangjieChar *c) {
50 free(c);
51
52 return CANGJIE_OK;
53 }
54