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

📄 genlib.c

📁 linux下的BBS
💻 C
📖 第 1 页 / 共 2 页
字号:
    if (read(fd, &mcHead, sizeof(mcHead)) != sizeof(mcHead)) corrupt();    if (strncmp(mcHead.magic, MCMagic, MCMagicLen) != 0) corrupt();    if (mcHead.majorVer != MCMajorVer) error(NULL, "unrecognized catalog version");    if ((mcHead.flags & MCGetByteOrder()) == 0) error(NULL, "wrong byte order");    if (lseek(fd, mcHead.firstSet, L_SET) == -1) corrupt();    while (True) {	if (read(fd, &mcSet, sizeof(mcSet)) != sizeof(mcSet)) corrupt();	if (mcSet.invalid) continue;	set = (setT *) malloc(sizeof(setT));	if (!set) nomem();	bzero(set, sizeof(*set));	if (cat->first) {	    cat->last->next = set;	    set->prev = cat->last;	    cat->last = set;	} else cat->first = cat->last = set;	set->setId = mcSet.setId;	/* Get the data */	if (mcSet.dataLen) {	    data = (char *) malloc(mcSet.dataLen);	    if (!data) nomem();	    if (lseek(fd, mcSet.data.off, L_SET) == -1) corrupt();	    if (read(fd, data, mcSet.dataLen) != mcSet.dataLen) corrupt();	    if (lseek(fd, mcSet.u.firstMsg, L_SET) == -1) corrupt();	    for (i = 0; i < mcSet.numMsgs; ++i) {		if (read(fd, &mcMsg, sizeof(mcMsg)) != sizeof(mcMsg)) corrupt();		if (mcMsg.invalid) {		    --i;		    continue;		}		msg = (msgT *) malloc(sizeof(msgT));		if (!msg) nomem();		bzero(msg, sizeof(*msg));		if (set->first) {		    set->last->next = msg;		    msg->prev = set->last;		    set->last = msg;		} else set->first = set->last = msg;		msg->msgId = mcMsg.msgId;		msg->str = dupstr((char *) (data + mcMsg.msg.off));	    }	    free(data);	}	if (!mcSet.nextSet) break;	if (lseek(fd, mcSet.nextSet, L_SET) == -1) corrupt();    }}static void printS(fd, str)int fd;char *str;{    write(fd, str, strlen(str));}static void printL(fd, l)int fd;long l;{    char	buf[32];    sprintf(buf, "%ld", l);    write(fd, buf, strlen(buf));}static void printLX(fd, l)int fd;long l;{    char	buf[32];    sprintf(buf, "%lx", l);    write(fd, buf, strlen(buf));}static void genconst(fd, type, setConst, msgConst, val)int fd;int type;char *setConst;char *msgConst;long val;{    switch (type) {      case MCLangC:	if (!msgConst) {	    printS(fd, "\n#define ");	    printS(fd, setConst);	    printS(fd, "Set");	} else {	    printS(fd, "#define ");	    printS(fd, setConst);	    printS(fd, msgConst);	}	printS(fd, "\t0x");	printLX(fd, val);	printS(fd, "\n");	break;      case MCLangCPlusPlus:      case MCLangANSIC:	if (!msgConst) {	    printS(fd, "\nconst long ");	    printS(fd, setConst);	    printS(fd, "Set");	} else {	    printS(fd, "const long ");	    printS(fd, setConst);	    printS(fd, msgConst);	}	printS(fd, "\t= ");	printL(fd, val);	printS(fd, ";\n");	break;      default:	error(NULL, "not a recognized (programming) language type");    }}void MCWriteConst(#if PROTO		int fd, int type, int orConsts)#else		fd, type, orConsts)int fd;int type;int orConsts;#endif{    msgT	*msg;    setT	*set;    long	id;    if (orConsts && (type == MCLangC || type == MCLangCPlusPlus || type == MCLangANSIC)) {	printS(fd, "/* Use these Macros to compose and decompose setId's and msgId's */\n");	printS(fd, "#ifndef MCMakeId\n");        printS(fd, "# define MCMakeId(s,m)\t(unsigned long)(((unsigned short)s<<(sizeof(short)*8))\\\n");        printS(fd, "\t\t\t\t\t|(unsigned short)m)\n");        printS(fd, "# define MCSetId(id)\t(unsigned int) (id >> (sizeof(short) * 8))\n");        printS(fd, "# define MCMsgId(id)\t(unsigned int) ((id << (sizeof(short) * 8))\\\n");        printS(fd, "\t\t\t\t\t>> (sizeof(short) * 8))\n");	printS(fd, "#endif\n");    }    for (set = cat->first; set; set = set->next) {	if (set->hconst) genconst(fd, type, set->hconst, NULL, set->setId);	for (msg = set->first; msg; msg = msg->next) {	    if (msg->hconst) {		if (orConsts) id = MCMakeId(set->setId, msg->msgId);		else id = msg->msgId;		genconst(fd, type, set->hconst, msg->hconst, id);		free(msg->hconst);		msg->hconst = NULL;	    }	}	if (set->hconst) {	    free(set->hconst);	    set->hconst = NULL;	}    }}void MCWriteCat(#if PROTO		int fd)#else		fd)int fd;#endif{    MCHeaderT	mcHead;    int		cnt;    setT	*set;    msgT	*msg;    MCSetT	mcSet;    MCMsgT	mcMsg;    off_t	pos;    bcopy(MCMagic, mcHead.magic, MCMagicLen);    mcHead.majorVer = MCMajorVer;    mcHead.minorVer = MCMinorVer;    mcHead.flags = MCGetByteOrder();    mcHead.firstSet = 0;	/* We'll be back to set this in a minute */    for (cnt = 0, set = cat->first; set; set = set->next) ++cnt;    mcHead.numSets = cnt;    lseek(fd, 0L, L_SET);    write(fd, &mcHead, sizeof(mcHead));    mcHead.firstSet = lseek(fd, 0, L_INCR);    lseek(fd, 0L, L_SET);    write(fd, &mcHead, sizeof(mcHead));    for (set = cat->first; set; set = set->next) {	bzero(&mcSet, sizeof(mcSet));	mcSet.setId = set->setId;	mcSet.invalid = False;	/* The rest we'll have to come back and change in a moment */	pos = lseek(fd, 0, L_INCR);	write(fd, &mcSet, sizeof(mcSet));	/* Now write all the string data */	mcSet.data.off = lseek(fd, 0, L_INCR);	cnt = 0;	for (msg = set->first; msg; msg = msg->next) {	    msg->offset = lseek(fd, 0, L_INCR) - mcSet.data.off;	    mcSet.dataLen += write(fd, msg->str, strlen(msg->str) + 1);	    ++cnt;	}	mcSet.u.firstMsg = lseek(fd, 0, L_INCR);	mcSet.numMsgs = cnt;	/* Now write the message headers */	for (msg = set->first; msg; msg = msg->next) {	    mcMsg.msgId = msg->msgId;	    mcMsg.msg.off = msg->offset;	    mcMsg.invalid = False;	    write(fd, &mcMsg, sizeof(mcMsg));	}	/* Go back and fix things up */	if (set == cat->last) {	    mcSet.nextSet = 0;	    lseek(fd, pos, L_SET);	    write(fd, &mcSet, sizeof(mcSet));	} else {	    mcSet.nextSet = lseek(fd, 0, L_INCR);	    lseek(fd, pos, L_SET);	    write(fd, &mcSet, sizeof(mcSet));	    lseek(fd, mcSet.nextSet, L_SET);	}    }}void MCAddSet(#if PROTO		int setId, char *hconst)#else		setId, hconst)int setId;char *hconst;#endif{    setT	*set;    if (setId <= 0) {	error(NULL, "setId's must be greater than zero");	return;    }    if (hconst && !*hconst) hconst = NULL;    for (set = cat->first; set; set = set->next) {	if (set->setId == setId) {	    if (set->hconst && hconst) free(set->hconst);	    set->hconst = NULL;	    break;	} else if (set->setId > setId) {	    setT	*newSet;	    newSet = (setT *) malloc(sizeof(setT));	    if (!newSet) nomem();	    bzero(newSet, sizeof(setT));	    newSet->prev = set->prev;	    newSet->next = set;	    if (set->prev) set->prev->next = newSet;	    else cat->first = newSet;	    set->prev = newSet;	    set = newSet;	    break;	}    }    if (!set) {	set = (setT *) malloc(sizeof(setT));	if (!set) nomem();	bzero(set, sizeof(setT));	if (cat->first) {	    set->prev = cat->last;	    set->next = NULL;	    cat->last->next = set;	    cat->last = set;	} else {	    set->prev = set->next = NULL;	    cat->first = cat->last = set;	}    }    set->setId = setId;    if (hconst) set->hconst = dupstr(hconst);    curSet = set;}void MCAddMsg(#if PROTO		int msgId, char *str, char *hconst)#else		msgId, str, hconst)int msgId;char *str;char *hconst;#endif{    msgT	*msg;    if (!curSet) error(NULL, "can't specify a message when no set exists");    if (msgId <= 0) {	error(NULL, "msgId's must be greater than zero");	return;    }    if (hconst && !*hconst) hconst = NULL;    for (msg = curSet->first; msg; msg = msg->next) {	if (msg->msgId == msgId) {	    if (msg->hconst && hconst) free(msg->hconst);	    if (msg->str) free(msg->str);	    msg->hconst = msg->str = NULL;	    break;	} else if (msg->msgId > msgId) {	    msgT	*newMsg;	    newMsg = (msgT *) malloc(sizeof(msgT));	    if (!newMsg) nomem();	    bzero(newMsg, sizeof(msgT));	    newMsg->prev = msg->prev;	    newMsg->next = msg;	    if (msg->prev) msg->prev->next = newMsg;	    else curSet->first = newMsg;	    msg->prev = newMsg;	    msg = newMsg;	    break;	}    }    if (!msg) {	msg = (msgT *) malloc(sizeof(msgT));	if (!msg) nomem();	bzero(msg, sizeof(msgT));	if (curSet->first) {	    msg->prev = curSet->last;	    msg->next = NULL;	    curSet->last->next = msg;	    curSet->last = msg;	} else {	    msg->prev = msg->next = NULL;	    curSet->first = curSet->last = msg;	}    }    msg->msgId = msgId;    if (hconst) msg->hconst = dupstr(hconst);    msg->str = dupstr(str);}void MCDelSet(#if PROTO		int setId)#else		setId)int setId;#endif{    setT	*set;    msgT	*msg;    for (set = cat->first; set; set = set->next) {	if (set->setId == setId) {	    for (msg = set->first; msg; msg = msg->next) {		if (msg->hconst) free(msg->hconst);		if (msg->str) free(msg->str);		free(msg);	    }	    if (set->hconst) free(set->hconst);	    if (set->prev) set->prev->next = set->next;	    else cat->first = set->next;	    if (set->next) set->next->prev = set->prev;	    else cat->last = set->prev;	    free(set);	    return;	} else if (set->setId > setId) break;    }    warning(NULL, "specified set doesn't exist");}void MCDelMsg(#if PROTO		int msgId)#else		msgId)int msgId;#endif{    msgT	*msg;    if (!curSet) error(NULL, "you can't delete a message before defining the set");    for (msg = curSet->first; msg; msg = msg->next) {	if (msg->msgId == msgId) {	    if (msg->hconst) free(msg->hconst);	    if (msg->str) free(msg->str);	    if (msg->prev) msg->prev->next = msg->next;	    else curSet->first = msg->next;	    if (msg->next) msg->next->prev = msg->prev;	    else curSet->last = msg->prev;	    free(msg);	    return;	} else if (msg->msgId > msgId) break;    }    warning(NULL, "specified msg doesn't exist");}void MCDumpcat(fp)FILE  *fp;{    msgT	*msg;    setT	*set;    if (!cat)		errx(1, "no catalog open");    for (set = cat->first; set; set = set->next) {	fprintf(fp, "$set %ld", set->setId);	if (set->hconst)	    fprintf(fp, " # %s", set->hconst);	fprintf(fp, "\n\n");	for (msg = set->first; msg; msg = msg->next) {	    if (msg->hconst)		fprintf(fp, "# %s\n", msg->hconst);	    fprintf(fp, "%ld\t%s\n", msg->msgId, msg->str);	}	fprintf(fp, "\n");    }}

⌨️ 快捷键说明

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