OSDN Git Service

add port number handling to bbsmnparser_item_t.
authorornse01 <ornse01@users.sourceforge.jp>
Thu, 12 Mar 2015 16:16:58 +0000 (16:16 +0000)
committerornse01 <ornse01@users.sourceforge.jp>
Thu, 12 Mar 2015 16:16:58 +0000 (16:16 +0000)
git-svn-id: http://svn.sourceforge.jp/svnroot/bchan/bchanl/trunk@654 20a0b8eb-f62a-4a12-8fe1-b598822500fb

src/bbsmenuparser.c
src/bbsmenuparser.h
src/main.c
src/test_bbsmenuparser.c

index edac4d5..1eac4b1 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * bbsmenuparser.c
  *
- * Copyright (c) 2009-2012 project bchan
+ * Copyright (c) 2009-2015 project bchan
  *
  * This software is provided 'as-is', without any express or implied
  * warranty. In no event will the authors be held liable for any damages
@@ -262,21 +262,42 @@ LOCAL W bbsmnparser_parsechar(bbsmnparser_t *parser, UB ch, bbsmnparser_item_t *
        return 0; /* TODO */
 }
 
-EXPORT VOID bbsmnparser_item_gethostboard(bbsmnparser_item_t *item, UB **host, W *host_len, UB **board, W *board_len)
+EXPORT VOID bbsmnparser_item_gethostboard(bbsmnparser_item_t *item, UB **host, W *host_len, UH *port, UB **board, W *board_len)
 {
        W i = 0;
-       UB *host0 = NULL, *board0 = NULL;
-       W host_len0 = 0, board_len0 = 0;
+       UB *host0 = NULL, *port0 = NULL, *board0 = NULL;
+       W host_len0 = 0, port_len0 = 0, board_len0 = 0;
+       Bool port_exist = False;
 
        host0 = item->url + 7;
        for (i=7; i < item->url_len; i++) {
                if (item->url[i] == '/') {
                        break;
                }
+               if (item->url[i] == ':') {
+                       port_exist = True;
+                       break;
+               }
                host_len0++;
        }
-
        i++;
+
+       port0 = item->url + i;
+       if (port_exist != False) {
+               for (; i < item->url_len; i++) {
+                       if (item->url[i] == '/') {
+                               break;
+                       }
+                       port_len0++;
+               }
+               i++;
+       }
+       if (port_len0 > 0) {
+               *port = atoi(port0);
+       } else {
+               *port = 80;
+       }
+
        board0 = item->url + i;
        for (; i < item->url_len; i++) {
                if (item->url[i] == '/') {
index 0643fe1..3bfb4b8 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * bbsmenuparser.h
  *
- * Copyright (c) 2009-2012 project bchan
+ * Copyright (c) 2009-2015 project bchan
  *
  * This software is provided 'as-is', without any express or implied
  * warranty. In no event will the authors be held liable for any damages
@@ -43,7 +43,7 @@ typedef struct bbsmnparser_t_ bbsmnparser_t;
 
 IMPORT VOID bbsmnparser_item_delete(bbsmnparser_item_t *item);
 IMPORT Bool bbsmnparser_item_checkboradurl(bbsmnparser_item_t *item);
-IMPORT VOID bbsmnparser_item_gethostboard(bbsmnparser_item_t *item, UB **host, W *host_len, UB **board, W *board_len);
+IMPORT VOID bbsmnparser_item_gethostboard(bbsmnparser_item_t *item, UB **host, W *host_len, UH *port, UB **board, W *board_len);
 
 IMPORT bbsmnparser_t* bbsmnparser_new(bbsmncache_t *cache);
 IMPORT VOID bbsmnparser_delete(bbsmnparser_t *parser);
index 4fd8e44..0ecf126 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * main.c
  *
- * Copyright (c) 2009-2014 project bchan
+ * Copyright (c) 2009-2015 project bchan
  *
  * This software is provided 'as-is', without any express or implied
  * warranty. In no event will the authors be held liable for any damages
@@ -614,6 +614,7 @@ LOCAL VOID bchanl_bbsmenuwindow_click(bchanl_t *bchanl, PNT pos)
        W fnd;
        UB *host, *board;
        W host_len, board_len;
+       UH port;
        TC *title;
        W title_len;
 
@@ -625,7 +626,7 @@ LOCAL VOID bchanl_bbsmenuwindow_click(bchanl_t *bchanl, PNT pos)
        if (item->category != NULL) {
                return;
        }
-       bbsmnparser_item_gethostboard(item, &host, &host_len, &board, &board_len);
+       bbsmnparser_item_gethostboard(item, &host, &host_len, &port, &board, &board_len);
        subject = bchanl_subjecthash_search(bchanl->subjecthash, host, host_len, board, board_len);
        if (subject == NULL) {
                DP(("not found by subject hash"));
@@ -734,8 +735,9 @@ LOCAL W bchanl_bbsmenu_appenditemtohash(bchanl_bbsmenu_t *bchanl, bbsmnparser_it
        W err;
        UB *host, *board;
        W host_len, board_len;
+       UH port;
 
-       bbsmnparser_item_gethostboard(item, &host, &host_len, &board, &board_len);
+       bbsmnparser_item_gethostboard(item, &host, &host_len, &port, &board, &board_len);
        err = bchanl_subjecthash_append(bchanl->subjecthash, host, host_len, board, board_len, item->title, item->title_len);
        return err;
 }
index 0da5624..a6f9223 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * test_bbsmenuparser.c
  *
- * Copyright (c) 2009-2012 project bchan
+ * Copyright (c) 2009-2015 project bchan
  *
  * This software is provided 'as-is', without any express or implied
  * warranty. In no event will the authors be held liable for any damages
@@ -395,6 +395,96 @@ LOCAL UNITTEST_RESULT test_bbsmnparser_15()
        return UNITTEST_RESULT_FAIL;
 }
 
+typedef struct {
+       UB *url;
+       W url_len;
+       UB *expected_host;
+       W expected_host_len;
+       UH expected_port;
+       UB *expected_board;
+       W expected_board_len;
+} test_bbsmnparser_item_url_t;
+
+LOCAL UNITTEST_RESULT test_bbsmnparser_item_url_common(test_bbsmnparser_item_url_t *data)
+{
+       bbsmncache_t *cache;
+       bbsmnparser_t *parser;
+       bbsmnparser_item_t *item;
+       UNITTEST_RESULT ret = UNITTEST_RESULT_PASS;
+       UB *host, *board;
+       W host_len, board_len;
+       UH port;
+
+       cache = bbsmncache_new();
+       if (cache == NULL) {
+               return UNITTEST_RESULT_FAIL;
+       }
+       parser = bbsmnparser_new(cache);
+       if (parser == NULL) {
+               bbsmncache_delete(cache);
+               return UNITTEST_RESULT_FAIL;
+       }
+
+       item = bbsmnparser_newboarditem(parser, NULL, 0, data->url, data->url_len);
+       if (item == NULL) {
+               ret = UNITTEST_RESULT_FAIL;
+       }
+       bbsmnparser_item_gethostboard(item, &host, &host_len, &port, &board, &board_len);
+       if (host_len != data->expected_host_len) {
+               printf("host length fail: expected = %d, result = %d\n", data->expected_host_len, host_len);
+               ret = UNITTEST_RESULT_FAIL;
+       } else if (strncmp(host, data->expected_host, host_len) != 0) {
+               printf("host fail\n");
+               ret = UNITTEST_RESULT_FAIL;
+       }
+       if (port != data->expected_port) {
+               printf("port fail: expected = %d, result = %d\n", data->expected_port, port);
+               ret = UNITTEST_RESULT_FAIL;
+       }
+       if (board_len != data->expected_board_len) {
+               printf("board length fail: expected = %d, result = %d\n", data->expected_board_len, board_len);
+               ret = UNITTEST_RESULT_FAIL;
+       } else if (strncmp(board, data->expected_board, board_len) != 0) {
+               printf("board fail\n");
+               ret = UNITTEST_RESULT_FAIL;
+       }
+
+       bbsmnparser_delete(parser);
+       bbsmncache_delete(cache);
+
+       return ret;
+}
+
+LOCAL UNITTEST_RESULT test_bbsmnparser_item_url_1(test_bbsmnparser_item_url_t *data)
+{
+       UB url[] = "http://aaa.bbb.ccc/test/";
+       W url_len = strlen(url);
+       UB host[] = "aaa.bbb.ccc";
+       W host_len = strlen(host);
+       UH port = 80;
+       UB board[] = "test";
+       W board_len = strlen(board);
+       test_bbsmnparser_item_url_t testdata = {
+               url, url_len, host, host_len, port, board, board_len
+       };
+       return test_bbsmnparser_item_url_common(&testdata);
+}
+
+LOCAL UNITTEST_RESULT test_bbsmnparser_item_url_2(test_bbsmnparser_item_url_t *data)
+{
+       UB url[] = "http://aaa.bbb.ccc:65000/test/";
+       W url_len = strlen(url);
+       UB host[] = "aaa.bbb.ccc";
+       W host_len = strlen(host);
+       UH port = 65000;
+       UB board[] = "test";
+       W board_len = strlen(board);
+       test_bbsmnparser_item_url_t testdata = {
+               url, url_len, host, host_len, port, board, board_len
+       };
+       return test_bbsmnparser_item_url_common(&testdata);
+}
+
 EXPORT VOID test_bbsmnparser_main(unittest_driver_t *driver)
 {
        UNITTEST_DRIVER_REGIST(driver, test_bbsmnparser_1);
@@ -412,4 +502,6 @@ EXPORT VOID test_bbsmnparser_main(unittest_driver_t *driver)
        UNITTEST_DRIVER_REGIST(driver, test_bbsmnparser_13);
        UNITTEST_DRIVER_REGIST(driver, test_bbsmnparser_14);
        UNITTEST_DRIVER_REGIST(driver, test_bbsmnparser_15);
+       UNITTEST_DRIVER_REGIST(driver, test_bbsmnparser_item_url_1);
+       UNITTEST_DRIVER_REGIST(driver, test_bbsmnparser_item_url_2);
 }