📄 textpro.c
字号:
/*******************************************************/ /* "C" Language Integrated Production System */ /* */ /* CLIPS Version 6.05 04/09/97 */ /* */ /* TEXT PROCESSING MODULE */ /*******************************************************//*************************************************************//* Purpose: *//* *//* Principal Programmer(s): *//* Brian L. Donnell *//* *//* Contributing Programmer(s): *//* *//* Revision History: *//* *//*************************************************************//**************************************************************************//**************************************************************************//* LOOKUP TABLE FUNCTIONS *//* *//* The functions contained in this file set up and access a hierarchical *//* lookup system for multiple files. *//* *//* For usage see external documentation. *//**************************************************************************//**************************************************************************/#define _TEXTPRO_SOURCE_#include "setup.h"#include <stdio.h>#define _CLIPS_STDIO_#include <ctype.h>#if ANSI_COMPILER#include <string.h>#endif#include "commline.h"#include "clipsmem.h"#include "argacces.h"#include "extnfunc.h"#include "router.h"#include "textpro.h"#if CLP_TEXTPRO || CLP_HELP#define NAMESIZE 80#define NULLCHAR '\0'#define BLANK (' ')#define TAB ('\t')#define LNFEED ('\n')/*=========================================================*//*Status returns for the file loading and lookup functions *//*=========================================================*/#define NORMAL 0 /*Entry information found in file */#define NO_FILE -10 /*File not found for reference */#define NEW_FILE -15 /*File loaded onto internal lookup table*/#define OLD_FILE -20 /*File was already on the lookup table */#define NO_TOPIC -25 /*No entry information was found in file*/#define EXIT -30 /*Branch-up from root; exit lookup table*/#define BRANCH_UP -35 /*Move up from subtopic entry to parent */#define BRANCH_DOWN -40 /*Move down from main topic to subtopic *//*=================*//*Entry data types *//*=================*/#define MENU -45 /*Entry has subtopics*/#define INFO -50 /*Entry is a leaf; contains only information*//*==========================================*//*Entry node type for internal lookup table *//*==========================================*/struct entries { int level; /*Level of entry node in the lookup tree */ int type; /*Entry node data type : menu or info */ char name[NAMESIZE]; /*Entry node name */ long int offset; /*Location of entry info in the file */ struct entries *child; /*Address of list of subtopic entries */ struct entries *parent; /*Address of parent topic entry */ struct entries *next; /*Address of next entry at the same level */ };/*=========================================*//*File node type for internal lookup table *//*=========================================*/struct lists { char file[NAMESIZE]; /*File name */ struct entries *topics; /*Address of list of entry topics for file */ struct entries *curr_menu; /*Address of current main topic in file */ struct lists *next; /*Address of next file in the table */ };/*==============================================*//*Address of the first file in the lookup table *//*==============================================*/static struct lists *headings = NULL;/*==================================================*//*Delimeter strings for marking entries in the file *//*==================================================*/#define BDELIM "BEGIN-ENTRY-"#define BDLEN 12#define EDELIM "END-ENTRY"#define EDLEN 9#define BFORMAT "%d%1s%12s%s" /*Format string for sscanf*/#define LIT_DELIM ('$')#if IBM_TBC#define OPEN_READ "rb"#else#define OPEN_READ "r"#endif#if ANSI_COMPILERint TextLookupFetch(char *,char *,int);int TextLookupToss(char *);static FILE *GetEntries(char *,char **,char *,int *);static FILE *GetCurrentMenu(char *,int *);static char *grab_string(FILE *,char *,int);static int findstr(char *,char *);static VOID upper(char *);static struct lists *NewFetchFile(char *);static struct entries *AllocateEntryNode(FILE *,char *,char *,char *,int,int);static int AttachLeaf(struct lists *,struct entries *,FILE *,char *,char *,int,int);static long LookupEntry(char *,char **,char *,int *);static VOID TossFunction(struct entries *);#elseint TextLookupFetch();int TextLookupToss();static FILE *GetEntries();static FILE *GetCurrentMenu();static char *grab_string();static int findstr();static VOID upper();static struct lists *NewFetchFile();static struct entries *AllocateEntryNode();static int AttachLeaf();static long LookupEntry();static VOID TossFunction();#endif/******************************************************************************//*============================================================================*//* INTERNAL ROUTINES *//*============================================================================*//******************************************************************************//****************************************************************************//*LOAD FUNCTION : *//* Input : 1) name of file to be loaded into the lookup table *//* 2) caller-allocated buffer to contain an error message (if any) *//* 3) size of error message buffer *//* Output : *//* This function attempts to load the file's topic information into the *//* lookup table according to the format below : *//* *//* <level-num><entry-type-code>BEGIN-ENTRY-<topic-name> *//* . *//* . *//* Entry information in the form in which *//* it is to be displayed when referenced. *//* . *//* . *//* END-ENTRY *//* *//* The function returns the number of entries loaded if the entire file was *//* was correctly formatted, else it returns 0. An error message is stored *//* in the caller's buffer. *//****************************************************************************/globle int TextLookupFetch(file,errbuf,bufsize) char *file, *errbuf; int bufsize; { FILE *fp; /*Pointer into stream of input file */ char str[256]; /*Buffer for storing input file lines */ int INFO_BEGIN, INFO_END; /*Flags used to check proper syntax */ struct lists *lnode; /*Used to store file node in list */ struct entries *enode; /*Used to store entry node in topic list */ int line_ct; /*Line count - used for error messages */ int entries_ct; /*Number of entries successfully loaded. */ if (bufsize > 0) errbuf[0] = NULLCHAR; fp = fopen(file,OPEN_READ); if (fp == NULL) { if (bufsize >= 60) sprintf(errbuf,"Could not open file \"%s\".",file); return(0); } if ((lnode = NewFetchFile(file)) == NULL) { fclose(fp); if (bufsize >= 60) sprintf(errbuf,"File \"%s\" already loaded.",file); return(0); } /*===========================*/ /*Store the file entry topics*/ /*===========================*/ line_ct = 0; entries_ct = 0; INFO_BEGIN = CLIPS_FALSE; INFO_END = CLIPS_TRUE; while (fgets(str,256,fp) != NULL) { line_ct++; /*=============================================================*/ /*Forces the load function to ignore lines beginning with `$$' */ /*=============================================================*/ if ((str[0] != LIT_DELIM) || (str[1] != LIT_DELIM)) if (findstr(str,EDELIM) >= 0) { if (INFO_BEGIN == CLIPS_TRUE) { INFO_BEGIN = CLIPS_FALSE; INFO_END = CLIPS_TRUE; entries_ct++; } else { fclose(fp); TextLookupToss(file); if (bufsize >= 60) sprintf(errbuf,"Line %d : Unmatched end marker.",line_ct); return(0); } } else if (findstr(str,BDELIM) >= 0) { if (INFO_END == CLIPS_TRUE) { INFO_END = CLIPS_FALSE; INFO_BEGIN = CLIPS_TRUE; } else { fclose(fp); TextLookupToss(file); if (bufsize >= 60) sprintf(errbuf,"Line %d : Previous entry not closed.",line_ct); return(0); } if ((enode=AllocateEntryNode(fp,file,str,errbuf,bufsize,line_ct))==NULL) return(0); /*=================================*/ /*Store new entry node in the tree */ /*=================================*/ if (AttachLeaf(lnode,enode,fp,file,errbuf,bufsize,line_ct) == CLIPS_FALSE) return(0); } } fclose(fp); if (INFO_END == CLIPS_FALSE) { TextLookupToss(file); if (bufsize >= 60) sprintf(errbuf,"Line %d : Previous entry not closed.",line_ct); return(0); } if (entries_ct == 0) TextLookupToss(file); return(entries_ct); }/******************************************************************************//*FUNCTION UNLOAD : *//* Input : 1) name of file to be taken off the lookup table *//* Output : This functions deletes a file and all entry-topics associated with*//* it from the lookup table and returns a boolean flag indicating *//* failure or success. *//******************************************************************************/globle int TextLookupToss(file) char *file; { struct lists *plptr, *clptr; int l_flag; clptr = headings; plptr = clptr; if (clptr != NULL) if (strcmp(clptr->file,file) != 0) l_flag = 1; else l_flag = 0; else l_flag = 0; while (l_flag > 0) { plptr = clptr; clptr = clptr->next; if (clptr != NULL) if (strcmp(clptr->file,file) != 0) l_flag = 1; else l_flag = 0; else l_flag = 0; } if (clptr == NULL) return(CLIPS_FALSE); TossFunction(clptr->topics); if (plptr == clptr) headings = clptr->next; else plptr->next = clptr->next; rm((VOID *) clptr,(int) sizeof(struct lists)); return(CLIPS_TRUE); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -