|
ydict,自制的一个小工具,分享一下。
有 Bug 请报一下。
帮忙改进一下更好。
编程水平不高,调用了几个 Linux 的程序...
本来想用 Google 的字典,但不能用 wget 下载,所以用了 Yahoo 的...
ydict.c- /* ydict - Yahoo Dictionary
- * Use wget to get the web page from yahoo and display to terminal
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <pwd.h>
- #include <unistd.h>
- #define VERSION "0.02"
- char path[100]; /* the path to save .ydict.tmp */
- FILE *sfp; /* the file pointer of function - print_speech */
- void
- big52utf8 (void) /* change charset */
- {
- system ("iconv ~/.ydict-big5.tmp -f big5 -t gb2312 | iconv -f gb2312 -t utf8 -o ~/.ydict.tmp");
- }
- void
- print_speech (void) /* the function to print speech */
- {
- char ch;
- char print = 0;
- char *speech = "<div class=pcixin>"; /* keyword of speech */
- char *speech0 = speech; /* start point of speech */
- if (sfp == NULL) /* set the file open if not set */
- sfp = fopen (path, "r");
- while ((ch = getc (sfp)) != EOF)
- {
- if (print == 0) /* 0 = check only, not output */
- if (ch == *speech)
- {
- ++speech;
- if (*speech == '\0')
- {
- speech = speech0;
- print = 1;
- }
- }
- else
- speech = speech0;
- else /* print = 1, output something */
- if (ch != '<')
- printf ("%c", ch);
- else
- {
- printf ("\n");
- print = 0;
- return;
- }
- }
- fclose (sfp);
- }
- char
- print_explain (void) /* the function to print explain */
- {
- FILE *fp;
- char ch;
- char *explain = "<div class=pexplain>"; /* keyword of explain */
- char *explain0 = explain; /* start point of explain */
- char *nspeech = "<div class=pcixin>"; /* keyword of next speech */
- char *nspeech0 = nspeech; /* start point of next speech */
- char print = 0;
- char chkexplain = 0; /* 1 = check explain */
- char findit = 0; /* 0 = can not find the word */
- fp = fopen (path, "r");
- while ((ch = getc (fp)) != EOF) /* read the file */
- {
- if (print == 0) /* 0 = check, no output */
- if (ch == *nspeech)
- {
- ++ nspeech;
- if (*nspeech == '\0')
- {
- nspeech = nspeech0;
- print_speech(); /* call function to print speech */
- }
- else
- chkexplain = 1;
- }
- else
- {
- nspeech = nspeech0;
- chkexplain = 1;
- }
- else /*print = 1, output something*/
- if (ch != '<')
- printf ("%c", ch);
- else
- {
- printf ("\n");
- print = 0;
- findit = 1; /* 1 = found the word */
- }
- if (chkexplain == 1) /* 1 = print explain of the word */
- {
- chkexplain = 0;
- if (ch == *explain)
- {
- ++ explain;
- if (*explain == '\0')
- {
- explain = explain0;
- print = 1;
- }
- }
- else
- explain = explain0;
- }
- }
- fclose (fp);
- return findit;
- }
- void
- set_home (void)
- {
- uid_t uid;
- struct passwd *info;
- uid = getuid ();
- info = getpwuid (uid);
- strcpy (path, info->pw_dir);
- strcat (path, "/.ydict.tmp");
- }
- void
- remove_tmp (void)
- {
- system ("rm ~/.ydict.tmp ~/.ydict-big5.tmp");
- }
- void
- get (char *word)
- {
- char cmd[250];
- strcpy (cmd, "wget --post-data="s=");
- strcat (cmd, word);
- strcat (cmd,
- "" http://hk.dictionary.yahoo.com/search.html -4q -O ~/.ydict-big5.tmp");
- system (cmd);
- }
- void
- print_help (void)
- {
- printf("Usage: ydict [WORD]\n");
- printf("Example: ydict apple\n");
- }
- int
- main (int argc, char *argv[])
- {
- printf ("ydict %s\n", VERSION);
- if (argc < 2)
- {
- printf ("No input\n");
- print_help ();
- exit (EXIT_FAILURE);
- }
- if ((strcmp(argv[1], "--help")) == 0)
- {
- print_help ();
- exit (EXIT_FAILURE);
- }
- set_home ();
- printf ("Receiving information from network...\n\n");
- get (argv[1]);
- big52utf8 ();
- if (print_explain () == 0)
- printf ("Could not find the word.\n");
- remove_tmp ();
- }
复制代码
用gcc编绎一下 |
|