boolean anyParent(struct dtdElement *elList, struct dtdElement *child)
/* Return TRUE if anybody in elList could be a parent to child. */
{
struct dtdElement *el;
for (el = elList; el != NULL; el = el->next)
    {
    if (childMatch(el->children, child))
        return TRUE;
    }
return FALSE;
}
Esempio n. 2
0
File: ac.c Progetto: diida/shenhe
/**
  * @param path 字典位置
  * @param py 是否要拼音 
  */
NODE *acInit(char *path,short py, short replace)
{
	int find ,i,c,addr;
	addr_t key;
	NODE *root,*p ,*queue_header,*queue_end,*q,*fq;
	root = getNode('\0',NULL);
	p = root;
 
	FILE* fp,*fp2;
	fp = fopen(path,"r");
	//fp2 = fopen("dictpy.txt","w");
	char str[WORD_MAX_LEN];
	char pinyinTmp[7];
	char pinyin[WORD_MAX_LEN * 6];
	char *x,*star="*";
	acPinyinInit();

	while (!feof(fp)) 
	{
		bzero(str,WORD_MAX_LEN);
		bzero(pinyin,WORD_MAX_LEN * 6);
		fgets(str,WORD_MAX_LEN,fp);  //读取一行
		i = 0;
		while(str[i] != '\n' && str[i] != '\0' && str[i] != '\r' && str[i] != '*') {
			bzero(pinyinTmp,7);
			
            if(str[i] & 0x80) {
                key = -str[i] + 128;
                key = ( key == 256 ? 128 : key);
			if(str[i] & 0x40) {
					x = &str[i];
					UTF8_TO_UNICODE(addr,x);
					strcpy(pinyinTmp,acPinyinList[addr]);
					
				}
            } else {
                key = (addr_t)str[i];
				pinyinTmp[0] = str[i];
            }
			strcat(pinyin,pinyinTmp);
			if(p->next[key] == 0) {
				p->next[key] = getNode(str[i],p);
			}
			p = p->next[key];
			i++;
		}
		if(i == 0) continue;
		//	printf("%s %s %d\n",path,str,i);
		p->is_word = 1;
		p->replace = replace;
		p = root;
		if(py == 0)continue;
		//将这一行转为拼音
		c = 0;
       // fputs(pinyin,fp2);
       // fputc('\n',fp2);
		while(pinyin[c] != '\n' && pinyin[c] != '\0' && pinyin[c] != '\r' && c < WORD_MAX_LEN * 6) {
            if(pinyin[c] & 0x80) {
                key = -pinyin[c] + 128;
                key = ( key == 256 ? 128 : key);
            } else {
			    key = (addr_t)pinyin[c];
            }

			if(p->next[key] == 0) {
				p->next[key] = getNode(pinyin[c],p);
			}
			p = p->next[key];
			c++;
		}
		
		p->is_word = 1;
		p->replace = replace;
		p = root;
	}
	
    fclose(fp);
    //fclose(fp2);
	
	p = queue_header = queue_end = root;
    
	while(p) {
        c = 0;
        for(i = 0 ;i < MAX_CHILD ;i++) {
            if(p->next[i] != 0) {
                c++;
                queue_end->back = p->next[i];
                queue_end = p->next[i];
            }
        }
        if(c == 0) {
            p->is_leaf = 1;
        }

		//弹出
		queue_header = queue_header->back;
		p = queue_header;

		if(p==0) {
			break;
		}
        
		find = 0;
		q = p->parent->fail;
		while(q != NULL) {
			fq = childMatch(q,p->v);
			if(fq) {
				find = 1;
				p->fail = fq;
				break;
			} else {
				if(q->parent == NULL) {
					break;
				}
				q = q->parent->fail;
			}
		}
        
		if(!find) {
			p->fail = root;
		}
	}
    //最后才设置
    root->fail = root;
	return root;
}
void makeStartHandler(struct dtdElement *elList, FILE *f)
/* Create function that gets called by expat at start of tag. */
{
struct dtdElement *el;
struct dtdAttribute *att;

startHandlerPrototype(f, "");
fprintf(f, "{\n");
fprintf(f, "struct xapStack *st = xp->stack+1;\n");
fprintf(f, "int depth = xp->stackDepth;\n");
fprintf(f, "int i;\n");
fprintf(f, "\n");
for (el = elList; el != NULL; el = el->next)
    {
    fprintf(f, "%sif (sameString(name, \"%s\"))\n", (el == elList ? "" : "else "), el->name);
    fprintf(f, "    {\n");
    fprintf(f, "    struct %s *obj;\n", el->mixedCaseName);
    fprintf(f, "    AllocVar(obj);\n");
    if (el->attributes != NULL)
        {
	boolean first = TRUE;
	for (att = el->attributes; att != NULL; att = att->next)
	    {
	    if (att->usual != NULL)
		{
		char *quote = "\"";
		if (sameString(att->type, "INT") || sameString(att->type, "FLOAT"))
		    quote = "";
	        fprintf(f, "    obj->%s = %s%s%s;\n", att->name, quote, att->usual, quote);
		}
	    }
	fprintf(f, "    for (i=0; atts[i] != NULL; i += 2)\n");
	fprintf(f, "        {\n");
	fprintf(f, "        char *name = atts[i], *val = atts[i+1];\n");
	for (att = el->attributes; att != NULL; att = att->next)
	    {
	    fprintf(f, "        %s (sameString(name, \"%s\"))\n",
		    (first ? "if " : "else if"), att->name);
	    if (sameWord(att->type, "INT"))
		fprintf(f, "            obj->%s = atoi(val);\n", att->name);
	    else if (sameWord(att->type, "FLOAT"))
		fprintf(f, "            obj->%s = atof(val);\n", att->name);
	    else
	        fprintf(f, "            obj->%s = cloneString(val);\n", att->name);
	    first = FALSE;
	    }
	if (picky)
	    {
	    fprintf(f, "        else\n");
	    fprintf(f, "            xapError(xp, \"Unknown attribute %%s\", name);\n");
	    }
	fprintf(f, "        }\n");
	for (att = el->attributes; att != NULL; att = att->next)
	    {
	    if (att->required)
	        {
		if (sameWord(att->type, "INT") || sameWord(att->type, "FLOAT"))
		    {
		    /* For the moment can't check these. */
		    }
		else
		    {
		    fprintf(f, "    if (obj->%s == NULL)\n", att->name);
		    fprintf(f, "        xapError(xp, \"missing %s\");\n", att->name);
		    }
		}
	    }
	}
    if (anyParent(elList, el))
        {
	struct dtdElement *parent;
	boolean first = TRUE;
	fprintf(f, "    if (depth > 1)\n");
	fprintf(f, "        {\n");
	for (parent = elList; parent != NULL; parent = parent->next)
	    {
	    if (childMatch(parent->children, el))
	        {
		fprintf(f, "        %s (sameString(st->elName, \"%s\"))\n", 
			(first ? "if " : "else if"), parent->name);
		fprintf(f, "            {\n");
		fprintf(f, "            struct %s *parent = st->object;\n", parent->mixedCaseName);
		fprintf(f, "            slAddHead(&parent->%s, obj);\n", el->mixedCaseName);
		fprintf(f, "            }\n");
		first = FALSE;
		}
	    }
	if (picky)
	    {
	    fprintf(f, "        else\n");
	    fprintf(f, "            xapError(xp, \"%%s misplaced\", name);\n");
	    }
	fprintf(f, "        }\n");
	}
    fprintf(f, "    return obj;\n");
    fprintf(f, "    }\n");
    }
fprintf(f, "else\n");
fprintf(f, "    {\n");
if (picky)
    fprintf(f, "    xapError(xp, \"Unknown element %%s\", name);\n");
else
    fprintf(f, "    xapSkip(xp);\n");
fprintf(f, "    return NULL;\n");
fprintf(f, "    }\n");
fprintf(f, "}\n");
fprintf(f, "\n");
}