⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bookmarks.c

📁 著名的手机浏览器开源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
         if (bm_node->section == sec_node->section) {            g_string_sprintf(gstr, "s%d %s %s\n",                             bm_node->section, bm_node->url, bm_node->title);            fwrite(gstr->str, gstr->len, 1, BmTxt);         }      }   }   g_string_free(gstr, TRUE);   fclose(BmTxt);   /* keep track of the timestamp */   stat(BmFile, &BmStat);   BmFileTimeStamp = BmStat.st_mtime;   return 0;}/* -- Add bookmark --------------------------------------------------------- *//* * Add a new bookmark to DB :) */int Bmsrv_add_bm(int SockFD, char *url, char *title){   char *msg="Added bookmark!";   int section = 0;   //g_print("Adding:\n  %s\n  %s\n", title, url);   /* Add in memory */   Bms_add(section, url, title);   /* Write to file */   Bms_save();   if (Bmsrv_dpi_send_status_msg(SockFD, msg))      return 1;   return 0;}/* -- Modify --------------------------------------------------------------- *//* * Count how many sections and urls were marked in a request */static void Bmsrv_count_urls_and_sections(char *url, gint *n_sec, gint *n_url){   char *p, *q;   int i;   /* Check marked urls and sections */   *n_sec = *n_url = 0;   if ((p = strchr(url, '?'))) {      for (q = p; (q = strstr(q, "&url")); ++q) {         for (i = 0; isdigit(q[4+i]); ++i);         *n_url += (q[4+i] == '=') ? 1 : 0;      }      for (q = p; (q = strstr(q, "&s")); ++q) {         for (i = 0; isdigit(q[2+i]); ++i);         *n_sec += (q[2+i] == '=') ? 1 : 0;      }   }}/* * Send a dpi reload request * Return code: { 0:OK, 1:Abort, 2:Close } */static int Bmsrv_send_reload_request(int OutFD, char *url){   gint st = 0;   GString *gstr = g_string_new("");   g_string_sprintf(gstr, "<dpi cmd='reload_request' url='%s'>", url);   if (send_data_chunk(OutFD, gstr->str, gstr->len))      st = 1;   g_string_free(gstr, TRUE);   return st;}/* * Send the HTML for the modify page * Return code: { 0:OK, 1:Abort, 2:Close } */static int Bmsrv_send_modify_page(int OutFD){   static GString *gstr = NULL;   gchar *l_title;   GSList *list1, *list2;   BmSec *sec_node;   BmRec *bm_node;   //g_print("  sending page body...\n");   if (!gstr)      gstr = g_string_new("");   /* send modify page header */   if (send_data_string(OutFD, modifypage_header))      return 1;   /* write sections header */   if (send_data_string(OutFD, modifypage_sections_header))      return 1;   /* write sections */   for (list1 = B_secs; list1; list1 = list1->next) {      sec_node = list1->data;      g_string_sprintf(gstr, modifypage_sections_item,                       sec_node->section, sec_node->section, sec_node->title);      if (send_data_string(OutFD, gstr->str))         return 1;   }   /* write sections footer */   if (send_data_string(OutFD, modifypage_sections_footer))      return 1;   /* send page middle */   if (send_data_string(OutFD, modifypage_middle1))      return 1;   /* send bookmark cards */   for (list1 = B_secs; list1; list1 = list1->next) {      sec_node = list1->data;      /* send card header */      l_title = make_one_line_str(sec_node->title);      g_string_sprintf(gstr, modifypage_section_card_header,                       sec_node->section, l_title);      g_free(l_title);      if (send_data_string(OutFD, gstr->str))         return 1;      /* send section's bookmarks */      for (list2 = B_bms; list2; list2 = list2->next) {         bm_node = list2->data;         if (bm_node->section == sec_node->section) {            g_string_sprintf(gstr, modifypage_section_card_item,                             bm_node->key, bm_node->url, bm_node->title);            if (send_data_string(OutFD, gstr->str))               return 1;         }      }      /* send card footer */      if (send_data_string(OutFD, modifypage_section_card_footer))         return 1;   }   /* finish page */   if (send_data_string(OutFD, modifypage_footer))      return 1;   //g_print("  page body sent.\n");   return 2;}/* * Send the HTML for the modify page for "add section" * Return code: { 0:OK, 1:Abort, 2:Close } */static int Bmsrv_send_modify_page_add_section(int OutFD){   //g_print("  sending page body...\n");   /* send modify page2 */   if (send_data_string(OutFD, modifypage_add_section_page))      return 1;   //g_print("  page body sent.\n");   return 2;}/* * Send the HTML for the modify page for "add url" * Return code: { 0:OK, 1:Abort, 2:Close } */static int Bmsrv_send_modify_page_add_url(int OutFD){   if (send_data_string(OutFD, modifypage_add_url))      return 1;   return 2;}/* * Parse a modify urls request and either: *   - make a local copy of the url *     or *   - send the modify page for the marked urls and sections * Return code: { 0:OK, 1:Abort, 2:Close } */static int Bmsrv_send_modify_update(int OutFD, char *url){   static char *url1 = NULL;   static GString *gstr = NULL;   char *p, *q;   int i, key, n_sec, n_url;   BmRec *bm_node;   BmSec *sec_node;   /* bookmarks were loaded before */   if (!gstr)      gstr = g_string_new("");   if (OutFD == -1) {      /* just copy url */      g_free(url1);      url1 = g_strdup(url);      return 0;   }   /* send HTML here */   if (send_data_string(OutFD, modifypage_update_header))      return 1;   /* Count number of marked urls and sections */   Bmsrv_count_urls_and_sections(url1, &n_sec, &n_url);   if (n_sec) {      g_string_sprintf(gstr, modifypage_update_title, "Update&nbsp;sections:");      send_data_chunk(OutFD, gstr->str, gstr->len);      send_data_string(OutFD, modifypage_update_item_header);      /* send items here */      p = strchr(url1, '?');      for (q = p; (q = strstr(q, "&s")); ++q) {         for (i = 0; isdigit(q[2+i]); ++i);         if (q[2+i] == '=') {            key = strtol(q + 2, NULL, 10);            if ((sec_node = Bms_get_sec(key))) {               g_string_sprintf(gstr, modifypage_update_item2,                                sec_node->section, sec_node->title);               send_data_chunk(OutFD, gstr->str, gstr->len);            }         }      }      send_data_string(OutFD, modifypage_update_item_footer);   }   if (n_url) {      g_string_sprintf(gstr, modifypage_update_title, "Update&nbsp;titles:");      send_data_chunk(OutFD, gstr->str, gstr->len);      send_data_string(OutFD, modifypage_update_item_header);      /* send items here */      p = strchr(url1, '?');      for (q = p; (q = strstr(q, "&url")); ++q) {         for (i = 0; isdigit(q[4+i]); ++i);         if (q[4+i] == '=') {            key = strtol(q + 4, NULL, 10);            bm_node = Bms_get(key);            g_string_sprintf(gstr, modifypage_update_item,                             bm_node->key, bm_node->title, bm_node->url);            send_data_chunk(OutFD, gstr->str, gstr->len);         }      }      send_data_string(OutFD, modifypage_update_item_footer);   }   send_data_string(OutFD, modifypage_update_footer);   return 2;}/* * Make the modify-page and send it back * Return code: { 0:OK, 1:Abort, 2:Close } */static int Bmsrv_send_modify_answer(int SockFD, char *url){   GString *gstr = g_string_new("");   g_string_sprintf(gstr, "<dpi cmd='start_send_page' url='%s'>", url);   //g_print("  cmd_buf=%s\n", gstr->str);   /* Send dpi command */   if (send(SockFD, gstr->str, gstr->len, 0) == -1) {      g_string_free(gstr, TRUE);      perror("[send]");      return 1;   }   g_string_free(gstr, TRUE);   /* Send HTTP header */   //g_print("  sending Header...\n");   if (send(SockFD, Header, strlen(Header), 0) == -1) {      perror("[send]");      return 1;   }   if (MODIFY_PAGE_NUM == 2) {      MODIFY_PAGE_NUM = 1;      return Bmsrv_send_modify_page_add_section(SockFD);   } else if (MODIFY_PAGE_NUM == 3) {      MODIFY_PAGE_NUM = 1;      return Bmsrv_send_modify_update(SockFD, NULL);   } else if (MODIFY_PAGE_NUM == 4) {      MODIFY_PAGE_NUM = 1;      return Bmsrv_send_modify_page_add_url(SockFD);   } else {      return Bmsrv_send_modify_page(SockFD);   }}/* Operations *//* * Parse a delete bms request, delete them, and update bm file. * Return code: { 0:OK, 1:Abort } */static int Bmsrv_modify_delete(int OutFD, char *url){   gchar *p;   gint nb, ns, key;   /* bookmarks were loaded before */   /* Remove marked sections */   p = strchr(url, '?');   for (ns = 0; (p = strstr(p, "&s")); ++p) {      if (isdigit(p[2])) {         key = strtol(p + 2, NULL, 10);         Bms_sec_del(key);         ++ns;      }   }   /* Remove marked urls */   p = strchr(url, '?');   for (nb = 0; (p = strstr(p, "&url")); ++p) {      if (isdigit(p[4])) {         key = strtol(p + 4, NULL, 10);         Bms_del(key);         ++nb;      }   }/* -- This doesn't work because dillo erases the message upon the *    receipt of the first data stream. *   sprintf(msg, "Deleted %d bookmark%s!>", n, (n > 1) ? "s" : "");   if (Bmsrv_dpi_send_status_msg(OutFD, msg))      return 1;*/   /* Write new bookmarks file */   if (nb || ns)      Bms_save();   return 0;}/* * Parse a move urls request, move and update bm file. * Return code: { 0:OK, 1:Abort } */static int Bmsrv_modify_move(int OutFD, char *url){   char *p;   int n, section = 0, key;   /* bookmarks were loaded before */   /* get target section */   for (p = url; (p = strstr(p, "&s")); ++p) {      if (isdigit(p[2])) {         section = strtol(p + 2, NULL, 10);         break;      }   }   if (!p)      return 1;   /* move marked urls */   p = strchr(url, '?');   for (n = 0; (p = strstr(p, "&url")); ++p) {      if (isdigit(p[4])) {         key = strtol(p + 4, NULL, 10);         Bms_move(key, section);         ++n;      }   }   /* Write new bookmarks file */   if (n) {      Bms_save();   }   return 0;}/* * Parse a modify request: update urls and sections, then save. * Return code: { 0:OK, 1:Abort } */static int Bmsrv_modify_update(int OutFD, char *url){   char *p, *q, *title;   int i, key;   /* bookmarks were loaded before */   p = strchr(url, '?');   for (  ; (p = strstr(p, "s")); ++p) {      if (p[-1] == '&' || p[-1] == '?' ) {         for (i = 0; isdigit(p[1 + i]); ++i);         if (i && p[1 + i] == '=') {            /* we have a title/key to change */            key = strtol(p + 1, NULL, 10);            if ((q = strchr(p + 1, '&')))               title = g_strndup(p + 2 + i, q - (p + 2 + i));            else               title = g_strdup(p + 2 + i);            Unencode_str(title);            Bms_update_sec_title(key, title);            g_free(title);         }      }   }   p = strchr(url, '?');   for (  ; (p = strstr(p, "title")); ++p) {      if (p[-1] == '&' || p[-1] == '?' ) {         for (i = 0; isdigit(p[5 + i]); ++i);         if (i && p[5 + i] == '=') {            /* we have a title/key to change */            key = strtol(p + 5, NULL, 10);            if ((q = strchr(p + 5, '&')))               title = g_strndup(p + 6 + i, q - (p + 6 + i));            else               title = g_strdup(p + 6 + i);            Unencode_str(title);            Bms_update_title(key, title);            g_free(title);         }      }   }   /* Write new bookmarks file */   Bms_save();   return 0;}/* * Parse an "add section" request, and update the bm file. * Return code: { 0:OK, 1:Abort } */static int Bmsrv_modify_add_section(int OutFD, char *url){   char *p, *title = NULL;   /* bookmarks were loaded before */   /* get new section's title */   if ((p = strstr(url, "&title="))) {      title = g_strdup (p + 7);      if ((p = strchr(title, '&')))         *p = 0;      Unencode_str(title);   } else      return 1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -