📄 spelling.c
字号:
/* TradeClient <http://tradeclient.sourceforge.net> * $Id: spelling.c,v 1.4 2001/03/20 22:19:33 ttabner Exp $ * * Copyright (C) 1999-2000 Bynari Inc. * Copyright (C) 2001 Project TradeClient * * LGPL * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU Library General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library * General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */#include "puma.h"void spell_print_msw (Misspelled *msw) { int i; printf ("msw=%p\n", msw); if (msw) { printf ("msw->correct=%d\n", msw->correct); printf ("msw->origword=%p\n", msw->origword); printf ("msw->nunofsugg=%d\n", msw->numofsugg); printf ("msw->sugg=%p\n", msw->sugg); if (msw->sugg) { i=0; for (i=0; i<msw->numofsugg; i++) { printf ("msw->sugg[%d]=%p\n", i, msw->sugg[i]); } } }}void spell_print_cs (CheckSpelling *cs) { int i; printf ("cs=%p\n", cs); if (cs) { printf ("cs->numofmsw=%d\n", cs->numofmsw); printf ("cs->text=%p '%.50s\n", cs->text, cs->text); printf ("cs->ph1=%d\n", cs->ph1); printf ("cs->ph2=%d\n", cs->ph2); printf ("cs->curimsw=%d\n", cs->curimsw); printf ("cs->msws=%p\n", cs->msws); if (cs->msws) { i=0; for (i=0; i<cs->numofmsw; i++) { printf ("cs->msws[%d]=%p\n", i, cs->msws[i]); } } }}char *spell_fix_text (char *text) { int i; char *ret, *tmpr; if (!text) return NULL; ret=(char *)calloc ((strlen(text)+1)*2, sizeof(char)); tmpr=ret; for (i=0; i<strlen(text); i++) { if (i-1 < 0) { switch (ret[i]) { case '*': case '@': case '#': case '~': case '+': case '-': case '%': *tmpr='^'; tmpr++; break; default: break; } } else if (ret[i-1]=='\n') { switch (ret[i]) { case '*': case '@': case '#': case '~': case '+': case '-': case '%': *tmpr='^'; tmpr++; break; default: break; } } *tmpr=text[i]; tmpr++; } return ret;}Misspelled **spell_add_misspell (char *tline, Misspelled **msws, int *numemb) { Misspelled **tmpmsws; int i; char *tmp1, *tmp2, *tmp3; if (!tline) return NULL; if (!msws) { *numemb=1; tmpmsws=(Misspelled **)calloc (1, sizeof(Misspelled *)); tmpmsws[0]=(Misspelled *)calloc (1, sizeof(Misspelled)); *numemb=1; } else { tmpmsws=(Misspelled **)calloc (*numemb+1, sizeof(Misspelled *)); for (i=0; i<*numemb; i++) { tmpmsws[i]=msws[i]; } tmpmsws[*numemb]=(Misspelled *)calloc (1, sizeof(Misspelled)); *numemb+=1; } tmp1=tline+2; tmp2=strchr (tmp1, ' '); tmpmsws[*numemb-1]->origword=(char *)calloc (tmp2-tmp1+2, sizeof(char)); memmove (tmpmsws[*numemb-1]->origword, tmp1, tmp2-tmp1); tmp1=tmp2+1; tmp2=strchr (tmp1, ' '); tmp3=(char *)calloc (tmp2-tmp1+2, sizeof(char)); memmove (tmp3, tmp1, tmp2-tmp1); tmpmsws[*numemb-1]->numofsugg=atoi(tmp3); free (tmp3); tmp1=tmp2+1; tmp2=strchr (tmp1, ' '); tmpmsws[*numemb-1]->sugg=(char **)calloc (tmpmsws[*numemb-1]->numofsugg+2, sizeof(char *)); tmp2--; for (i=0; i<tmpmsws[*numemb-1]->numofsugg; i++) { tmp1=tmp2+2; tmp2=strpbrk (tmp1, ",\n"); if (!tmp2) tmp2=tline+strlen (tline); tmpmsws[*numemb-1]->sugg[i]=(char *)calloc (tmp2-tmp1+2, sizeof(char)); memmove (tmpmsws[*numemb-1]->sugg[i], tmp1, tmp2-tmp1); } /* printf ("Original word '%s'\n", tmpmsws[*numemb-1]->origword); printf ("Number of matches '%d'\n", tmpmsws[*numemb-1]->numofsugg); for (i=0; i<tmpmsws[*numemb-1]->numofsugg; i++) { printf (" match %.2d '%s'\n", i, tmpmsws[*numemb-1]->sugg[i]); } */ return tmpmsws;}Misspelled **spell_add_miss (char *tline, Misspelled **msws, int *numemb) { Misspelled **tmpmsws; int i; char *tmp1, *tmp2; if (!tline) return NULL; if (!msws) { *numemb=1; tmpmsws=(Misspelled **)calloc (1, sizeof(Misspelled *)); tmpmsws[0]=(Misspelled *)calloc (1, sizeof(Misspelled)); *numemb=1; } else { tmpmsws=(Misspelled **)calloc (*numemb+1, sizeof(Misspelled *)); for (i=0; i<*numemb; i++) { tmpmsws[i]=msws[i]; } tmpmsws[*numemb]=(Misspelled *)calloc (1, sizeof(Misspelled)); *numemb+=1; } tmp1=tline+2; tmp2=strchr (tmp1, ' '); tmpmsws[*numemb-1]->origword=(char *)calloc (tmp2-tmp1+2, sizeof(char)); memmove (tmpmsws[*numemb-1]->origword, tmp1, tmp2-tmp1); tmpmsws[*numemb-1]->numofsugg=0; return tmpmsws;}CheckSpelling *spell_check_spelling (char *text) { char *ecall, *iret, *tmps1, *tmps2; char *dir="/tmp/tradeclient", *file1, *file2; int fd, i; CheckSpelling *cs; if (!text) return NULL; cs=(CheckSpelling *)calloc (1, sizeof(CheckSpelling)); cs->text=spell_fix_text (text); /* Yes, yes, I know im not doing it very well. all I can say is pipes dont like me and named pipes didn't help. */ file1=(char *)calloc (strlen(dir)+52, sizeof(char)); file2=(char *)calloc (strlen(dir)+52, sizeof(char)); snprintf (file1, strlen(dir)+50, "%s/%s", dir, "ispin"); snprintf (file2, strlen(dir)+50, "%s/%s", dir, "ispout"); mkdir (dir, S_IREAD | S_IWRITE | S_IEXEC); fd=open (file2, O_CREAT | O_RDONLY, S_IRUSR | S_IWUSR); close (fd); fd=open (file1, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); write (fd, text, strlen (text)); close (fd); ecall=(char *)calloc (strlen (text)+102, sizeof (char)); snprintf (ecall, strlen (text)+100, "cat %s | ispell -a > %s", file1, file2); system (ecall); iret=(char *)calloc (strlen (text)*2+202, sizeof (char)); fd=open (file2, O_RDONLY); read (fd, iret, strlen (text)*2+200); close (fd); unlink (file1); unlink (file2); free (file1); free (file2); // printf ("iret=\n%s\n", iret); /* parse the responce from ispell */ for (i=0; i<strlen (iret); i++) { if ((iret[i]!='@') && (iret[i]!='*') && (iret[i]!='+') && (iret[i]!='-') && (iret[i]!='\n')) { switch (iret[i]) { case '&': tmps1=strchr((iret+i), '\n'); tmps2=(char *)calloc (tmps1-(iret+i)+2, sizeof(char)); memmove (tmps2, (iret+i), tmps1-(iret+i)); cs->msws=spell_add_misspell (tmps2, cs->msws, &cs->numofmsw); free (tmps2); break; case '#': tmps1=strchr((iret+i), '\n'); tmps2=(char *)calloc (tmps1-(iret+i)+2, sizeof(char)); memmove (tmps2, (iret+i), tmps1-(iret+i)); cs->msws=spell_add_miss (tmps2, cs->msws, &cs->numofmsw); free( tmps2 ) ; break; } } while (iret[i]!='\n') { i++; } } free( ecall ) ; free( iret ) ; return cs;}Misspelled *spell_check_word (char *word) { char *ecall, *iret, *tmps1, *tmps2, *tmps3, *tline; /* I know its not called tradeclient anymore, but theirs a few refrances to this dir allready. all need to point to /tmp/tradeclient */ char *dir="/tmp/tradeclient", *file1, *file2; char *tword; int fd, i; Misspelled *msw; if (!word) return NULL; tword=spell_fix_text (word); file1=(char *)calloc (strlen(dir)+52, sizeof(char)); file2=(char *)calloc (strlen(dir)+52, sizeof(char)); snprintf (file1, strlen(dir)+50, "%s/%s", dir, "ispin"); snprintf (file2, strlen(dir)+50, "%s/%s", dir, "ispout"); mkdir (dir, S_IREAD | S_IWRITE | S_IEXEC); fd=open (file2, O_CREAT | O_RDONLY, S_IRUSR | S_IWUSR); close (fd); fd=open (file1, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); write (fd, tword, strlen (tword)); close (fd); ecall=(char *)calloc (strlen (tword)+102, sizeof (char)); snprintf (ecall, strlen (tword)+100, "cat %s | ispell -a > %s", file1, file2); system (ecall); iret=(char *)calloc (strlen (tword)*2+202, sizeof (char)); fd=open (file2, O_RDONLY); read (fd, iret, strlen (tword)*2+200); close (fd); unlink (file1); unlink (file2); free (file1); free (file2); // printf ("iret=\n%s\n", iret); msw=(Misspelled *)calloc (1, sizeof(Misspelled)); msw->correct=FALSE; /* parse the responce from ispell */ i=0; if (iret[i]=='@') { tmps1=strchr (iret, '\n'); i=tmps1-iret+1; } if ((iret[i]!='@') && (iret[i]!='*') && (iret[i]!='+') && (iret[i]!='-')) { switch (iret[i]) { case '&': tmps1=strchr((iret+i), '\n'); tline=(char *)calloc (tmps1-(iret+i)+2, sizeof(char)); memmove (tline, (iret+i), tmps1-(iret+i)); tmps1=tline+2; tmps2=strchr (tmps1, ' '); msw->origword=(char *)calloc (tmps2-tmps1+2, sizeof(char)); memmove (msw->origword, tmps1, tmps2-tmps1); tmps1=tmps2+1; tmps2=strchr (tmps1, ' '); tmps3=(char *)calloc (tmps2-tmps1+2, sizeof(char)); memmove (tmps3, tmps1, tmps2-tmps1); msw->numofsugg=atoi(tmps3); free (tmps3); tmps1=tmps2+1; tmps2=strchr (tmps1, ' '); msw->sugg=(char **)calloc (msw->numofsugg+1, sizeof(char *)); for (i=0; i<msw->numofsugg; i++) { tmps1=tmps2+1; tmps2=strpbrk (tmps1, ",\n"); if (!tmps2) tmps2=tline+strlen (tline); msw->sugg[i]=(char *)calloc (tmps2-tmps1+2, sizeof(char)); memmove (msw->sugg[i], tmps1, tmps2-tmps1); } /* printf ("Original word '%s'\n", msw->origword); printf ("Number of matches '%d'\n", msw->numofsugg); for (i=0; i<msw->numofsugg; i++) { printf (" match '%s'\n", msw->sugg[i]); } */ free (tline); break; } } else { msw->correct=TRUE; } free( ecall ) ; free( iret ) ; return msw;}/* dont free */Misspelled *spell_getnext_misspelled (CheckSpelling *cs) { char *tmp1, *tmp2; if (!cs) return NULL; if (cs->curimsw>=cs->numofmsw) return NULL; tmp1=strstr (cs->text+cs->ph2, cs->msws[cs->curimsw]->origword); if (cs->text!=tmp1) { for (;isalpha(cs->text[tmp1-cs->text-1]) || isalpha(tmp1[strlen (cs->msws[cs->curimsw]->origword)]); tmp1=strstr (tmp1+1, cs->msws[cs->curimsw]->origword)) { } } else { for (;isalpha(tmp1[strlen (cs->msws[cs->curimsw]->origword)]); tmp1=strstr (tmp1+1, cs->msws[cs->curimsw]->origword)) { } } tmp2=tmp1 + strlen (cs->msws[cs->curimsw]->origword); if (!tmp2) tmp2=cs->text+strlen (cs->text); cs->ph1=tmp1-cs->text; cs->ph2=tmp2-cs->text; cs->curimsw++; return cs->msws[cs->curimsw-1];}void spell_insert_correction (CheckSpelling *cs, char *cword) { char *tmptext; if (!cs) return; tmptext=(char *)calloc (strlen(cs->text)+strlen(cword)+2, sizeof(char)); memmove (tmptext, cs->text, cs->ph1); memmove (tmptext+cs->ph1, cword, strlen (cword)); memmove (tmptext+cs->ph1+strlen (cword), cs->text+cs->ph2, strlen (cs->text+cs->ph2)); free (cs->text); cs->text=tmptext;}void spell_insert_todict_word (char *word) { char *ecall; if (!word) return; ecall=(char *)calloc (strlen (word)+52, sizeof (char)); snprintf (ecall, strlen (word)+50, "echo \"*%s\" | ispell -a", word); system (ecall);}void spell_destroy_msw( Misspelled *msw ) { int i; if( !msw ) return ; if( msw -> origword ) free( msw -> origword ) ; if( msw -> sugg ) { for( i=0 ; msw -> sugg[i] ; i++ ) free( msw->sugg[i] ) ; free( msw -> sugg ) ; } free( msw ) ; return ;}void spell_destroy_cs( CheckSpelling *cs ) { int i; if( !cs ) return ; if( cs -> text ) free( cs -> text ) ; if( cs -> msws ) { for( i=0; i < cs->numofmsw ; i++ ) spell_destroy_msw( cs -> msws[i] ) ; free( cs -> msws ) ; } free( cs ) ; return ;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -