• R/O
  • SSH
  • HTTPS

bchan: 提交


Commit MetaInfo

修訂515 (tree)
時間2012-07-08 17:57:40
作者ornse01

Log Message

implement tray reader for external bbs url obj.

Change Summary

差異

--- bchanl/trunk/src/util.c (revision 514)
+++ bchanl/trunk/src/util.c (revision 515)
@@ -30,10 +30,13 @@
3030 #include <tcode.h>
3131 #include <tstring.h>
3232 #include <errcode.h>
33+#include <btron/btron.h>
3334 #include <btron/hmi.h>
3435
3536 #include "util.h"
3637 #include <tad/traydata_iterator.h>
38+#include <tad/taditerator.h>
39+#include <tad/tadstack.h>
3740
3841 #ifdef BCHANL_CONFIG_DEBUG
3942 # define DP(arg) printf arg
@@ -145,6 +148,66 @@
145148 traydata_iterator_finalize(&iter->base);
146149 }
147150
151+struct traydata_vobjiterator_t_ {
152+ traydata_iterator_t base;
153+ VLINK buf;
154+};
155+typedef struct traydata_vobjiterator_t_ traydata_vobjiterator_t;
156+
157+LOCAL VOID traydata_vobjiterator_initialize(traydata_vobjiterator_t *iter, TRAYREC *rec, W recnum)
158+{
159+ traydata_iterator_initialize(&iter->base, rec, recnum);
160+}
161+
162+LOCAL Bool traydata_vobjiterator_getnext(traydata_vobjiterator_t *iter, VLINK **lnk)
163+{
164+ traydata_iterator_result result;
165+ Bool cont;
166+ W writelen = 0, cpsize;
167+
168+ for (;;) {
169+ cont = traydata_iterator_next(&iter->base, &result);
170+ if (cont == False) {
171+ break;
172+ }
173+ if (result.type == TRAYDATA_ITERATOR_RESULTTYPE_VOBJREC_CONT) {
174+ if (writelen + result.val.vobj.chunk_data_len > sizeof(VLINK)) {
175+ cpsize = sizeof(VLINK) - writelen;
176+ } else {
177+ cpsize = result.val.vobj.chunk_data_len;
178+ }
179+ memcpy(((UB*)&iter->buf) + writelen, result.val.vobj.chunk_data, cpsize);
180+ writelen += cpsize;
181+ if (cpsize >= sizeof(VLINK)) {
182+ *lnk = &iter->buf;
183+ return True;
184+ }
185+ continue;
186+ }
187+ if (result.type == TRAYDATA_ITERATOR_RESULTTYPE_VOBJREC) {
188+ if (writelen + result.val.vobj.chunk_data_len > sizeof(VLINK)) {
189+ cpsize = sizeof(VLINK) - writelen;
190+ } else {
191+ cpsize = result.val.vobj.chunk_data_len;
192+ }
193+ memcpy(((UB*)&iter->buf) + writelen, result.val.vobj.chunk_data, cpsize);
194+ writelen += cpsize;
195+ if (cpsize >= sizeof(VLINK)) {
196+ *lnk = &iter->buf;
197+ return True;
198+ }
199+ return False;
200+ }
201+ }
202+
203+ return False;
204+}
205+
206+LOCAL VOID traydata_vobjiterator_finalize(traydata_vobjiterator_t *iter)
207+{
208+ traydata_iterator_finalize(&iter->base);
209+}
210+
148211 EXPORT W tray_popstring(TC *str, W len)
149212 {
150213 TRAYREC *data;
@@ -213,3 +276,168 @@
213276 }
214277 return False;
215278 }
279+
280+LOCAL W tray_getfirstvobj(VLINK *lnk)
281+{
282+ TRAYREC *data;
283+ W size, recs, err;
284+ traydata_vobjiterator_t iter;
285+ VLINK *lnk0 = NULL;
286+ Bool cont;
287+
288+ err = tget_dat(NULL, 0, &size, -1);
289+ if (err < 0) {
290+ DP_ER("tget_dat: get size error", err);
291+ return err;
292+ }
293+ if (err == 0) {
294+ /* tray is empty */
295+ return -1; /* TODO */
296+ }
297+ data = (TRAYREC*)malloc(size);
298+ if (data == NULL) {
299+ return -1; /* TODO */
300+ }
301+ recs = tget_dat(data, size, NULL, -1);
302+ if (recs < 0) {
303+ DP_ER("tget_dat: get data error", recs);
304+ free(data);
305+ return err;
306+ }
307+
308+ traydata_vobjiterator_initialize(&iter, data, recs);
309+ for (;;) {
310+ cont = traydata_vobjiterator_getnext(&iter, &lnk0);
311+ if (cont == False) {
312+ break;
313+ }
314+ if (lnk0 != NULL) {
315+ *lnk = *lnk0;
316+ break;
317+ }
318+ }
319+ traydata_vobjiterator_finalize(&iter);
320+
321+ free(data);
322+
323+ if (lnk0 == NULL) {
324+ return -1;
325+ }
326+
327+ return 0;
328+}
329+
330+LOCAL W tray_getextbbsinf_readfiestline(UB *buf, W buflen, TC *url, W url_len)
331+{
332+ tadstack_t stack;
333+ taditerator_t iter;
334+ taditerator_result result;
335+ TADSTACK_RESULT stk_result;
336+ W len = 0;
337+
338+ tadstack_initialize(&stack);
339+ taditerator_initialize(&iter, (TC*)buf, buflen/sizeof(TC));
340+
341+ for (;;) {
342+ taditerator_next2(&iter, &result);
343+ if (result.type == TADITERATOR_RESULTTYPE_END) {
344+ break;
345+ }
346+ stk_result = TADSTACK_RESULT_OK;
347+ if (result.type == TADITERATOR_RESULTTYPE_CHARCTOR) {
348+
349+ stk_result = tadstack_inputcharactor(&stack, result.segment);
350+ if (stk_result == TADSTACK_RESULT_OK) {
351+ if (result.segment == TK_NL) {
352+ break;
353+ }
354+ if ((url != NULL)&&(len < url_len)) {
355+ url[len] = result.segment;
356+ }
357+ len++;
358+ }
359+ } else if (result.type == TADITERATOR_RESULTTYPE_SEGMENT) {
360+ stk_result = tadstack_inputvsegment(&stack, result.segment, result.data, result.segsize);
361+ }
362+ if (stk_result == TADSTACK_RESULT_FORMAT_ERROR) {
363+ len = -1;
364+ break;
365+ }
366+ }
367+
368+ taditerator_finalize(&iter);
369+ tadstack_finalize(&stack);
370+
371+ return len;
372+}
373+
374+LOCAL W tray_getextbbsinf_readfile(VLINK *vlnk, TC *url, W url_len)
375+{
376+ W fd, err, size;
377+ UB *buf;
378+
379+ fd = opn_fil((LINK*)vlnk, F_READ, NULL);
380+ if (fd < 0) {
381+ return fd;
382+ }
383+ err = fnd_rec(fd, F_TOPEND, RM_TADDATA, 0, NULL);
384+ if (err < 0) {
385+ cls_fil(fd);
386+ return err;
387+ }
388+ err = rea_rec(fd, 0, NULL, 0, &size, NULL);
389+ if (err < 0) {
390+ cls_fil(fd);
391+ return err;
392+ }
393+ buf = malloc(size*sizeof(UB));
394+ if (buf == NULL) {
395+ cls_fil(fd);
396+ return -1; /* TODO */
397+ }
398+ err = rea_rec(fd, 0, buf, size, NULL, NULL);
399+ if (err < 0) {
400+ free(buf);
401+ cls_fil(fd);
402+ return err;
403+ }
404+ cls_fil(fd);
405+
406+ err = tray_getextbbsinf_readfiestline(buf, size, url, url_len);
407+
408+ free(buf);
409+
410+ return err;
411+}
412+
413+EXPORT W tray_getextbbsinfo(TC *name, W *name_len, TC *url, W *url_len)
414+{
415+ VLINK lnk;
416+ W err;
417+ TC fname[L_FNM + 1];
418+
419+ err = tray_getfirstvobj(&lnk);
420+ if (err < 0) {
421+ return err;
422+ }
423+
424+ err = fil_sts((LINK*)&lnk, fname, NULL, NULL);
425+ if (err < 0) {
426+ return err;
427+ }
428+ if (name != NULL) {
429+ tc_strncpy(name, fname, *name_len);
430+ } else if (name_len != NULL) {
431+ *name_len = tc_strlen(fname);
432+ }
433+
434+ err = tray_getextbbsinf_readfile(&lnk, url, *url_len);
435+ if (err < 0) {
436+ return err;
437+ }
438+ if (url_len != NULL) {
439+ *url_len = err;
440+ }
441+
442+ return 0;
443+}
--- bchanl/trunk/src/util.h (revision 514)
+++ bchanl/trunk/src/util.h (revision 515)
@@ -43,5 +43,6 @@
4343 IMPORT W tray_popstring(TC *str, W len);
4444 IMPORT W tray_deletedata();
4545 IMPORT Bool tray_isempty();
46+IMPORT W tray_getextbbsinfo(TC *name, W *name_len, TC *url, W *url_len);
4647
4748 #endif
Show on old repository browser