OSDN Git Service

fix copyright year mistake.
[bbk/bchan.git] / src / traydata.c
1 /*
2  * traydata.c
3  *
4  * Copyright (c) 2010-2011 project bchan
5  *
6  * This software is provided 'as-is', without any express or implied
7  * warranty. In no event will the authors be held liable for any damages
8  * arising from the use of this software.
9  *
10  * Permission is granted to anyone to use this software for any purpose,
11  * including commercial applications, and to alter it and redistribute it
12  * freely, subject to the following restrictions:
13  *
14  * 1. The origin of this software must not be misrepresented; you must not
15  *    claim that you wrote the original software. If you use this software
16  *    in a product, an acknowledgment in the product documentation would be
17  *    appreciated but is not required.
18  *
19  * 2. Altered source versions must be plainly marked as such, and must not be
20  *    misrepresented as being the original software.
21  *
22  * 3. This notice may not be removed or altered from any source
23  *    distribution.
24  *
25  */
26
27 #include    "traydata.h"
28 #include    "layoutarray.h"
29 #include    "tadlib.h"
30
31 #include        <basic.h>
32 #include        <bstdlib.h>
33 #include        <bstdio.h>
34 #include        <tcode.h>
35
36 #ifdef BCHAN_CONFIG_DEBUG
37 # define DP(arg) printf arg
38 # define DP_ER(msg, err) printf("%s (%d/%x)\n", msg, err>>16, err)
39 #else
40 # define DP(arg) /**/
41 # define DP_ER(msg, err) /**/
42 #endif
43
44 struct dattraydata_t_ {
45         datlayoutarray_t *layoutarray;
46 };
47
48 LOCAL W datlayout_res_totraytextdata_write_zenkaku(TC *str)
49 {
50         TADSEG *seg = (TADSEG*)str;
51
52         if (str == NULL) {
53                 return 10;
54         }
55
56         seg->id = 0xFF00|TS_TFONT;
57         seg->len = 6;
58         *(UH*)(str + 2) = 3 << 8;
59         *(RATIO*)(str + 3) = 0x0101;
60         *(RATIO*)(str + 4) = 0x0101;
61
62         return 10;
63 }
64
65 LOCAL W datlayout_res_totraytextdata(datlayout_res_t *res, B *data, W data_len)
66 {
67         W i = 0, num;
68         TC *str = (TC*)data;
69
70         if (data != NULL) {
71                 num = res->index + 1;
72                 i += datlayout_res_totraytextdata_write_zenkaku(str + i) / 2;
73                 i += tadlib_UW_to_str(num, str + i, data_len - i / 2);
74                 str[i++] = TK_KSP;
75                 i += tadlib_remove_TA_APPL(res->parser_res->name, res->parser_res->name_len, str + i, data_len - i / 2) / 2;
76                 str[i++] = TK_KSP;
77                 i += datlayout_res_totraytextdata_write_zenkaku(str + i) / 2;
78                 str[i++] = TK_LABR;
79                 i += tadlib_remove_TA_APPL(res->parser_res->mail, res->parser_res->mail_len, str + i, data_len - i / 2) / 2;
80                 i += datlayout_res_totraytextdata_write_zenkaku(str + i) / 2;
81                 str[i++] = TK_RABR;
82                 str[i++] = TK_KSP;
83                 i += tadlib_remove_TA_APPL(res->parser_res->date, res->parser_res->date_len, str + i, data_len - i / 2) / 2;
84                 str[i++] = TK_NL;
85                 i += datlayout_res_totraytextdata_write_zenkaku(str + i) / 2;
86                 i += tadlib_remove_TA_APPL(res->parser_res->body, res->parser_res->body_len, str + i, data_len - i / 2) / 2;
87                 str[i++] = TK_NL;
88         } else {
89                 num = res->index + 1;
90                 i += datlayout_res_totraytextdata_write_zenkaku(NULL) / 2;
91                 i += tadlib_UW_to_str(num, NULL, 0);
92                 i++; /* TK_KSP */
93                 i += tadlib_remove_TA_APPL_calcsize(res->parser_res->name, res->parser_res->name_len) / 2;
94                 i++; /* TK_KSP */
95                 i += datlayout_res_totraytextdata_write_zenkaku(NULL) / 2;
96                 i++; /* TO_LABR */
97                 i += tadlib_remove_TA_APPL_calcsize(res->parser_res->mail, res->parser_res->mail_len) / 2;
98                 i += datlayout_res_totraytextdata_write_zenkaku(NULL) / 2;
99                 i++; /* TO_RABR */
100                 i++; /* TK_KSP */
101                 i += tadlib_remove_TA_APPL_calcsize(res->parser_res->date, res->parser_res->date_len) / 2;
102                 i++; /* TK_NL */
103                 i += datlayout_res_totraytextdata_write_zenkaku(NULL) / 2;
104                 i += tadlib_remove_TA_APPL_calcsize(res->parser_res->body, res->parser_res->body_len) / 2;
105                 i++; /* TK_NL */
106         }
107
108         return i * sizeof(TC);
109 }
110
111 EXPORT W dattraydata_resindextotraytextdata(dattraydata_t *traydata, W n, B *data, W data_len)
112 {
113         datlayout_res_t *res;
114         Bool exist;
115         exist = datlayoutarray_getresbyindex(traydata->layoutarray, n, &res);
116         if (exist == False) {
117                 return -1; /* TODO */
118         }
119         return datlayout_res_totraytextdata(res, data, data_len);
120 }
121
122 LOCAL Bool dattraydata_isrescopytotray(datlayout_res_t *layout_res)
123 {
124         Bool ok;
125
126         ok = datlayout_res_isenableindexNG(layout_res);
127         if (ok == True) {
128                 return False;
129         }
130         ok = datlayout_res_isenableidNG(layout_res);
131         if (ok == True) {
132                 return False;
133         }
134         ok = datlayout_res_isenablewordNG(layout_res);
135         if (ok == True) {
136                 return False;
137         }
138
139         return True;
140 }
141
142 EXPORT W dattraydata_idtotraytextdata(dattraydata_t *traydata, TC *id, W id_len, B *data, W data_len)
143 {
144         W i, len, result, sum = 0;
145         Bool haveid, ok;
146         datlayout_res_t *res;
147
148         len = datlayoutarray_length(traydata->layoutarray);
149         for (i = 0; i < len; i++) {
150                 datlayoutarray_getresbyindex(traydata->layoutarray, i, &res);
151
152                 haveid = datlayout_res_issameid(res, id, id_len);
153                 if (haveid == False) {
154                         continue;
155                 }
156                 ok = dattraydata_isrescopytotray(res);
157                 if (ok == False) {
158                         continue;
159                 }
160                 if (data == NULL) {
161                         result = datlayout_res_totraytextdata(res, NULL, 0);
162                         if (result < 0) {
163                                 return result;
164                         }
165                 } else {
166                         result = datlayout_res_totraytextdata(res, data + sum, data_len - sum);
167                         if (result < 0) {
168                                 return result;
169                         }
170                 }
171                 sum += result;
172         }
173
174         return sum;
175 }
176
177 EXPORT dattraydata_t* dattraydata_new(datlayoutarray_t *layoutarray)
178 {
179         dattraydata_t *traydata;
180
181         traydata = (dattraydata_t *)malloc(sizeof(dattraydata_t));
182         if (traydata == NULL) {
183                 return NULL;
184         }
185         traydata->layoutarray = layoutarray;
186
187         return traydata;
188 }
189
190 EXPORT VOID dattraydata_delete(dattraydata_t *traydata)
191 {
192         free(traydata);
193 }