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

📄 webdict2.cpp

📁 It provides a base of DICT web service ( similar to DICT rfc2229) and create relative applications (
💻 CPP
字号:
/* *  Copyright (C) 2002  Larry Cai -- <caiyu@yahoo.com> * *  This program is free software; you can redistribute it and/or modify *  it under the terms of the GNU 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 General Public License for more details. * *  You should have received a copy of the GNU General Public License *  along with this program; if not, write to the Free Software *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/#include "soapH.h" #include <regex.h>#include <stdio.h>#include <string.h>#include <sys/stat.h>#include <unistd.h>const int MAXDBS = 2;char dictfile[MAXDBS][100]={	"data/ecdict.txt",	"data/cedict.txt"};char dictname[MAXDBS][100]={"En-Ch","PinYin-En"};const char *dictsdesc[MAXDBS]={	"english to chinese with phonetic , 177848 words and phrases",	"pinyin to english from CEDICT project, 21490 words"};const char *dictlang[MAXDBS]={	"english->chinese",	"chinese->english"};					   				   int main(int argc,char *argv[]) { 	int m,s;/* master and slave sockets */	struct soap soap;	soap_init(&soap);//  if add follow line, java client will report SOAP version mismatch//	soap.enable_strict=1;		if(argc<2) /* serve as CGI application */		soap_serve(&soap);  // serve request	else	{		fprintf(stderr,"Binding on port %d\n",atoi(argv[1]));		m = soap_bind(&soap, NULL, atoi(argv[1]), 100);		if (m < 0)		{ 			soap_print_fault(&soap, stderr);	  		exit(-1);		}		fprintf(stderr, "Socket connection successful: master socket = %d\n", m);		for ( ; ; )		{ 			s = soap_accept(&soap);		  	fprintf(stderr, "Socket connection successful: slave socket = %d\n", s);			if (s < 0)			{ 				soap_print_fault(&soap, stderr);				exit(-1);			} 			soap_serve(&soap);			soap_end(&soap);		}	}	return 0;}int check_yindiao(char *word, int len){	int i, flag=0;	for(i = 0; i < len; i++) {		if(isdigit(word[i])) break;	}	if(i != len) flag = 1;	else flag = 0;	return(flag);}void erase_yindiao(char *str, int len){	int i, j;	char buffer[100];	j = 0;	for(i = 0; i < len; i++) {		if(!isdigit(str[i])) {			buffer[j] = str[i];			j++;		}	}	buffer[j] = '\0';	strcpy(str, buffer);}      void add_info(char *dest_str, char *str, int len){	char *ptr;	ptr = strstr(str, " ///");	*ptr = '\0';	//  strcat(dest_str, "\n");	strcat(dest_str, &ptr[5]);}int find_in_C_E_dic(FILE *fp, char *word, char *sword, char *match, char *line){	int i, found, cmp, sec_cmp, len;	char line1[65535], foundword[65535], *p1;	char firstword[5000], firstline[5000];	int  yindiao_flag = 0, single_flag = 0;	yindiao_flag = check_yindiao(word, strlen(word));	found = 0;	while(!feof(fp)) {		fgets(line1, 65535, fp);		strcpy(line, line1);		p1 = strstr(line1, " ///");		if(p1 == NULL) {			fprintf(stderr, "Dict format error\n");			continue;		}		*p1 = '\0';		strcpy(firstword, word);		p1 = strstr(firstword, " ");		if(p1 == NULL) 			single_flag = 1;		else  			*p1 = '\0';		if(yindiao_flag == 0) 			erase_yindiao(line1, strlen(line1));		strcpy(firstline, line1);		p1 = strstr(firstline, " ");		if(p1!=NULL) *p1 = '\0';		cmp = strcasecmp(firstword, firstline);		if(cmp) {			if(cmp > 0) continue;		  	if(cmp < 0) break;		}		else {		    sec_cmp = strcasecmp(word, line1);		  	if(!sec_cmp) {    			if(!found) {    		  		found = 1;    		  		strcpy(foundword, line);    			}    			else 					add_info(foundword, line, strlen(line));    			continue;		    }		}	}	if(found == 1) {		strcpy(line, foundword);	}	strcpy(line1, line);	p1 = strstr(line1, " ///");	*p1 = '\0';	strcpy(sword, line1);	strcpy(line, &p1[5]);	if(found == 1)		strcpy(match, "yes");	else 		strcpy(match, "no");	len = strlen(line);	for(i=0; i<len; i++) {		if ( (line[i] == '\\') && (line[i+1] == 'n') ) {	  		line[i] = ' ';	//      line[i+1] = '\n';	  		line[i+1] = ' ';		}	}	return SOAP_OK;}  void adjudgement_word(char *str, int len){	for(;;) {		if(!isalpha(str[len-1]) && !isdigit(str[len-1])) {			len--;			str[len] = '\0';		}		else 			break;	}}void record(int dictionary,char *word){	time_t now;    time(&now);    struct  tm      tnow;    char    date[32];    tnow = *localtime(&now);    strftime(date,32,"[%D:%T]",&tnow);			char log[1000]="";	char ch[3]="00";	strcpy(log,date);	if(!getenv("REMOTE_ADDR"))		return;	strcat(log,getenv("REMOTE_ADDR"));	strcat(log,":");	if(getenv("HTTP_USER_AGENT"))		strcat(log,getenv("HTTP_USER_AGENT"));	strcat(log,":");//	ch[0]= dictionary + '0';	sprintf(ch,"%02d",dictionary);	strcat(log,ch);	strcat(log,":");		// This may not good to record the private information ?	// so I simple encrypt the word ,	char lword[200]; 	int i;	int len=strlen(word);	if(len>100)		len=100;	for(i=0;i<len;i++)		lword[i]='z'-word[i]+'a';	lword[i]='\0';	strcat(log,lword); 	strcat(log,"\n");		FILE *fp;	fp=fopen("webdict.log","a+");	if(fp==NULL)		return;	fputs(log,fp); // Need lock in the future ?	fclose(fp);}int ns1__lookup(struct soap *soap,xsd__string db,xsd__string word,xsd__string commands,	xsd__string strategy,ns1__lookupResponse  &result){  FILE *fp;  char indexfile[150];  char ind[1000][3];  long pos[1000], pos1, pos2;  int i = 0, found, total, head, tail, middle;  int notfound, cmp, len;  char line[65535], line1[65535], foundword[65535], *p1,*p2, line2[200];  char match[5], sword[100];  char phonetic[100] = " ", lists[11][100];  char rlist[1000]="";  int Re_value = 0;    int dictionary;  dictionary=atoi(db);	soap_fault(soap); /* make sure we have a place to store the fault */	soap->fault->faultstring = (char*)soap_malloc(soap, 1024);		// printf("Hello %s\n",word);  record(dictionary,word);  for(i = 0; i < 11; i++) {     memset(lists[i], 0, sizeof(char)*100);     lists[i][0]='\0';  }	if (dictionary > MAXDBS || dictionary < 0) { 		strcpy(soap->fault->faultstring,"No search database doesn't exist ");		return SOAP_FAULT; 	}	strcpy(indexfile, dictfile[dictionary]);	strcat(indexfile, ".index");	fp = fopen(indexfile, "r");	if(fp == NULL){		strcpy(soap->fault->faultstring,"database index file  open error");		return SOAP_FAULT;	}  i = 0;  while(!feof(fp)) {    fgets(line, 256, fp);    p1 = strchr(line, '\t');    ind[i][0] = p1[1];    ind[i][1] = p1[2];    ind[i][2] = '\0';    pos[i] = atol(&p1[4]);    i++;  }  fclose(fp);  total = i - 1;  found = 0;  head = 0;  tail = total;  while (head <= tail) {    middle = (head + tail)/2;    if(ind[middle][0] > word[0]) tail = middle - 1;    else if (ind[middle][0] < word[0]) head = middle + 1;    else {      if(ind[middle][1] > word[1]) tail = middle - 1;      else if (ind[middle][1] < word[1]) head = middle + 1;      else {        found = 1;        break;      }    }  }    pos1 = pos[middle];  pos2 = pos[middle + 1];  line[0] = '\0';  adjudgement_word(word, strlen(word)); // erase the useless char at the end of word  fp = fopen(dictfile[dictionary], "r");  if (fp == NULL) {		strcpy(soap->fault->faultstring,"database file open error");		return SOAP_FAULT;  }    fseek(fp, pos1, SEEK_SET);  if(dictionary == MAXDBS-1) {    Re_value = find_in_C_E_dic(fp, word, sword, match, line);    fclose(fp);    result.match = match;    result.word = word;    result.phonetic = phonetic;    result.list = rlist;    result.definition = line;//    result.definition.__size = strlen(line);    return(Re_value);  }  found = 0;  notfound = 0;  i = 0;  while(!feof(fp)) {    fgets(line1, 65535, fp);    strcpy(line, line1);    p1 = strstr(line1, " ///");    if(p1 == NULL) {      fprintf(stderr, "Dict format Error\n");      continue;    }    *p1 = '\0';       if(found == 0) {      if(notfound && i < 11) {        strcpy(lists[i], line1);        i++;        continue;      }      else if(notfound && i >= 10) break;    }        cmp = strcasecmp(word, line1);    if(cmp) {      if(cmp < 0) {        if((!found) && (!notfound)) {          notfound = 1;          strcpy(lists[i], line1);          strcpy(foundword, line);          if (i > 5) {            int j = 0;            for(j=0; j<6; j++)              strcpy(lists[j], lists[j+i-5]);            i = j - 1;          }          i++;          continue;        }      }      if((found == 0) && (i >= 11)) i = 0;      strcpy(lists[i], line1);      if( (found == 1) && (i >= 10)) break;      i++;    }    else {      found = 1;      strcpy(foundword, line);      strcpy(lists[i],line1);      if(i > 5) {        int j;        for(j = 0; j < 6; j++)          strcpy(lists[j], lists[j+i-5]);        i = j - 1;      }      i++;      continue;    }  }  fclose(fp);//  fprintf(stderr, "%s\n", line);  if(found == 1 || notfound == 1) {    strcpy(line1, foundword);    strcpy(line, foundword);  }  p1 = strstr(line1, " ///");  *p1 = '\0';  strcpy(sword, line1);  strcpy(line, &p1[5]);  // Searching phonetic start// Format /// \...\ \\n */  strcpy(line1,line);  phonetic[0]=' ';  phonetic[1]='\0';  if(line1[0]=='\\') {  	p1 = strstr(line1,"\\ \\n");	if(p1!=NULL) {		p2=&line1[0];		if( (p1-p2) < 50 ) {			*p1='\0';			p2=p1+4;			strcpy(phonetic,&line1[1]);			strcpy(line,p2);		}//		else {//			phonetic[0]='A';//		}	}//	else {//			phonetic[0]='B';//	}// }else {//			phonetic[0]='C';  }  // Searching phonetic end.  if(found == 1)     strcpy(match, "yes");  else strcpy(match, "no");  len = strlen(line);  for(i = 0; i < len; i++) {    if ( (line[i] == '\\') && (line[i+1] == 'n') ) {      line[i] = ' ';      line[i+1] = '\n';    }  }//  fprintf(stderr, "%s", line);  result.match = match;  result.word = sword;  result.phonetic = phonetic;  result.list = rlist;  strcat(result.list,lists[0]);  for(i = 1; i < 11; i++) {  	strcat(result.list,"+");    	strcat(result.list,lists[i]);  }//  printf("Hello %s\n",line);  result.definition = line;//  result.definition.__size = len;  return (SOAP_OK);}int ns1__getdatabase(struct soap *soap,	xsd__string client,	xsd__string filter,	xsd__string dictversion,	getdatabaseResponse  &result){	int i,size;	char buffer[100];		strcpy(buffer,"DiCT 0.0.2");	result.dictversion = (char*)soap_malloc(soap,(strlen(buffer)+1));	strcpy(result.dictversion,buffer);		strcpy(buffer,"working!");	result.status = (char*)soap_malloc(soap,(strlen(buffer)+1));	strcpy(result.status,buffer);		size=MAXDBS;                        	result.dbs.__ptr = soap_new_ns1__db(soap,size);    result.dbs.__size = size;		for(i=0;i<MAXDBS;i++)	{		sprintf(buffer,"%d",i);		result.dbs.__ptr[i].number = (char*)soap_malloc(soap,(strlen(buffer)+1));		strcpy(result.dbs.__ptr[i].number,buffer);				strcpy(buffer,dictname[i]);		result.dbs.__ptr[i].name = (char*)soap_malloc(soap,(strlen(buffer)+1));		strcpy(result.dbs.__ptr[i].name,buffer);						strcpy(buffer,dictsdesc[i]);		result.dbs.__ptr[i].sdesc = (char*)soap_malloc(soap,(strlen(buffer)+1));		strcpy(result.dbs.__ptr[i].sdesc,buffer);				strcpy(buffer,dictlang[i]);		result.dbs.__ptr[i].language = (char*)soap_malloc(soap,(strlen(buffer)+1));		strcpy(result.dbs.__ptr[i].language,buffer);				strcpy(buffer,"similiar");		result.dbs.__ptr[i].strategy = (char*)soap_malloc(soap,(strlen(buffer)+1));		strcpy(result.dbs.__ptr[i].strategy,buffer);						strcpy(buffer,"NDICT");		result.dbs.__ptr[i].type = (char*)soap_malloc(soap,(strlen(buffer)+1));		strcpy(result.dbs.__ptr[i].type,buffer);								}	return SOAP_OK;	}int ns1__status(struct soap *soap,xsd__string query,struct ns1__statusResponse &result){	char buffer[1000];   	printf("hello larry\n");	sprintf(buffer,"your query %s is ok. server soap is powered by gSOAP.",query);	result.status = (char*)soap_malloc(soap,(strlen(buffer)+1));	strcpy(result.status,buffer);	return SOAP_OK;} // As always, a namespace mapping table is needed: struct Namespace namespaces[] = { // { "ns-pre x", "ns-name" }	{"SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/" },	{"SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/" }, 	{"xsi", "http://www.w3.org/1999/XMLSchema-instance" }, 	{"xsd", "http://www.w3.org/1999/XMLSchema"}, 	{"ns1", "urn:wdict" } , // bind "ns" namespace prefix	{ NULL, NULL }};	

⌨️ 快捷键说明

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