LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 993|回复: 2

ydict - 自制的一个小工具

[复制链接]
发表于 2007-7-18 20:00:42 | 显示全部楼层 |阅读模式
ydict,自制的一个小工具,分享一下。
有 Bug 请报一下。
帮忙改进一下更好。

编程水平不高,调用了几个 Linux 的程序...
本来想用 Google 的字典,但不能用 wget 下载,所以用了 Yahoo 的...

ydict.c
  1. /* ydict - Yahoo Dictionary
  2. * Use wget to get the web page from yahoo and display to terminal
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <pwd.h>
  8. #include <unistd.h>
  9. #define VERSION "0.02"
  10. char path[100];  /* the path to save .ydict.tmp */
  11. FILE *sfp;  /* the file pointer of function - print_speech */
  12. void
  13. big52utf8 (void)  /* change charset */
  14. {
  15.   system ("iconv ~/.ydict-big5.tmp -f big5 -t gb2312 | iconv -f gb2312 -t utf8 -o ~/.ydict.tmp");
  16. }
  17. void
  18. print_speech (void)  /* the function to print speech */
  19. {
  20.   char ch;
  21.   char print = 0;
  22.   char *speech = "<div class=pcixin>";  /* keyword of speech */
  23.   char *speech0 = speech;  /* start point of speech */
  24.   if (sfp == NULL)  /* set the file open if not set */
  25.     sfp = fopen (path, "r");
  26.   while ((ch = getc (sfp)) != EOF)
  27.     {
  28.       if (print == 0)  /* 0 = check only, not output  */
  29.         if (ch == *speech)
  30.           {
  31.             ++speech;
  32.             if (*speech == '\0')
  33.               {
  34.                 speech = speech0;
  35.                 print = 1;
  36.               }
  37.           }
  38.         else
  39.           speech = speech0;
  40.       else  /* print = 1, output something  */
  41.         if (ch != '<')
  42.           printf ("%c", ch);
  43.         else
  44.           {
  45.             printf ("\n");
  46.             print = 0;
  47.             return;
  48.           }
  49.     }
  50.   fclose (sfp);
  51. }
  52. char
  53. print_explain (void)  /* the function to print explain */
  54. {
  55.   FILE *fp;
  56.   char ch;
  57.   char *explain = "<div class=pexplain>";  /* keyword of explain */
  58.   char *explain0 = explain;  /* start point of explain */
  59.   char *nspeech = "<div class=pcixin>";  /* keyword of next speech */
  60.   char *nspeech0 = nspeech;  /* start point of next speech */
  61.   char print = 0;
  62.   char chkexplain = 0;  /* 1 = check explain */
  63.   char findit = 0;  /* 0 = can not find the word */
  64.   fp = fopen (path, "r");
  65.   while ((ch = getc (fp)) != EOF)        /* read the file */
  66.     {
  67.       if (print == 0)  /* 0 = check, no output  */
  68.         if (ch == *nspeech)
  69.           {
  70.             ++ nspeech;
  71.             if (*nspeech == '\0')
  72.               {
  73.                 nspeech = nspeech0;
  74.                 print_speech();  /* call function to print speech */
  75.               }
  76.             else
  77.               chkexplain = 1;
  78.           }
  79.         else
  80.           {
  81.             nspeech = nspeech0;
  82.             chkexplain = 1;
  83.           }
  84.       else  /*print = 1, output something*/
  85.         if (ch != '<')
  86.           printf ("%c", ch);
  87.         else
  88.           {
  89.             printf ("\n");
  90.             print = 0;
  91.             findit = 1;  /* 1 = found the word */
  92.           }
  93.       if (chkexplain == 1)  /* 1 = print explain of the word */
  94.         {
  95.           chkexplain = 0;
  96.           if (ch == *explain)
  97.             {
  98.               ++ explain;
  99.               if (*explain == '\0')
  100.                 {
  101.                   explain = explain0;
  102.                   print = 1;
  103.                 }
  104.             }
  105.           else
  106.             explain = explain0;
  107.         }
  108.     }
  109.   fclose (fp);
  110.   return findit;
  111. }
  112. void
  113. set_home (void)
  114. {
  115.   uid_t uid;
  116.   struct passwd *info;
  117.   uid = getuid ();
  118.   info = getpwuid (uid);
  119.   strcpy (path, info->pw_dir);
  120.   strcat (path, "/.ydict.tmp");
  121. }
  122. void
  123. remove_tmp (void)
  124. {
  125.   system ("rm ~/.ydict.tmp ~/.ydict-big5.tmp");
  126. }
  127. void
  128. get (char *word)
  129. {
  130.   char cmd[250];
  131.   strcpy (cmd, "wget --post-data="s=");
  132.   strcat (cmd, word);
  133.   strcat (cmd,
  134.           "" http://hk.dictionary.yahoo.com/search.html -4q -O ~/.ydict-big5.tmp");
  135.   system (cmd);
  136. }
  137. void
  138. print_help (void)
  139. {
  140.   printf("Usage: ydict [WORD]\n");
  141.   printf("Example: ydict apple\n");
  142. }
  143. int
  144. main (int argc, char *argv[])
  145. {
  146.   printf ("ydict %s\n", VERSION);
  147.   if (argc < 2)
  148.     {
  149.       printf ("No input\n");
  150.       print_help ();
  151.       exit (EXIT_FAILURE);
  152.     }
  153.   if ((strcmp(argv[1], "--help")) == 0)
  154.     {
  155.       print_help ();
  156.       exit (EXIT_FAILURE);
  157.     }
  158.   set_home ();
  159.   printf ("Receiving information from network...\n\n");
  160.   get (argv[1]);
  161.   big52utf8 ();
  162.   if (print_explain () == 0)
  163.     printf ("Could not find the word.\n");
  164.   remove_tmp ();
  165. }
复制代码

用gcc编绎一下
  1. gcc ydict.c
  2. ./a.out
复制代码
发表于 2007-7-18 20:42:26 | 显示全部楼层
如果需要那么多 system() 的话,不如看看 shell script
#/bin/sh
wget -q -O- --post-data="s=$*" http://hk.dictionary.yahoo.com/search.html   |  iconv  -f big5 -t gb2312 | iconv -f gb2312 -t utf8  |  grep  'div class=pexplain' | sed -e :a -e 's/<[^>]*>//g;/</N;//ba ' -e 's/&nbsp\;//'


你的 C 每一次 system() 都是用外部 shell 执行的。

或者,把那些外部程序用 getopt、 iconv、 http 库替换
回复 支持 反对

使用道具 举报

发表于 2007-7-19 09:20:46 | 显示全部楼层
楼主的努力的态度让我非常欣赏.
只是从方法来讲,还是用bash的好。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表