📄 textpro.c
字号:
/*******************************************************/ /* "C" Language Integrated Production System */ /* */ /* CLIPS Version 6.24 06/05/06 */ /* */ /* TEXT PROCESSING MODULE */ /*******************************************************//*************************************************************//* Purpose: *//* *//* Principal Programmer(s): *//* Brian L. Donnell *//* *//* Contributing Programmer(s): *//* Gary D. Riley *//* *//* Revision History: *//* 6.23: Modified error messages so that they were *//* directly printed rather than storing them in *//* a string buffer which might not be large *//* enough to contain the entire message. DR0855 *//* Correction for FalseSymbol/TrueSymbol. DR0859 *//* *//* 6.24: Added get-region function. *//* *//* Added environment parameter to GenClose. *//* Added environment parameter to GenOpen. *//* *//*************************************************************//**************************************************************************//**************************************************************************//* 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 _STDIO_INCLUDED_#include <ctype.h>#include <string.h>#include "argacces.h"#include "commline.h"#include "envrnmnt.h"#include "extnfunc.h"#include "memalloc.h"#include "router.h"#include "sysdep.h"#include "textpro.h"#if TEXTPRO_FUNCTIONS || HELP_FUNCTIONS#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 */ };/*==================================================*//*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#define TEXTPRO_DATA 8struct textProcessingData { struct lists *headings; struct entries *parent;#if HELP_FUNCTIONS int HELP_INIT; char *help_file;#endif };#define TextProcessingData(theEnv) ((struct textProcessingData *) GetEnvironmentData(theEnv,TEXTPRO_DATA))int TextLookupFetch(void *,char *);int TextLookupToss(void *,char *);static FILE *GetEntries(void *,char *,char **,char *,int *);static FILE *GetCurrentMenu(void *,char *,int *);static char *grab_string(void *,FILE *,char *,int);static int findstr(char *,char *);static void upper(char *);static struct lists *NewFetchFile(void *,char *);static struct entries *AllocateEntryNode(void *,FILE *,char *,char *,int);static int AttachLeaf(void *,struct lists *,struct entries *,FILE *,char *,int);static long LookupEntry(void *,char *,char **,char *,int *);static void TossFunction(void *,struct entries *);static void DeallocateTextProcessingData(void *);/******************************************************************************//*============================================================================*//* 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 -1. *//****************************************************************************/globle int TextLookupFetch( void *theEnv, char *file) { 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. */ fp = GenOpen(theEnv,file,OPEN_READ); if (fp == NULL) { PrintErrorID(theEnv,"TEXTPRO",1,FALSE); EnvPrintRouter(theEnv,WERROR,"Could not open file \""); EnvPrintRouter(theEnv,WERROR,file); EnvPrintRouter(theEnv,WERROR,"\".\n"); return(-1); } if ((lnode = NewFetchFile(theEnv,file)) == NULL) { GenClose(theEnv,fp); PrintErrorID(theEnv,"TEXTPRO",2,FALSE); EnvPrintRouter(theEnv,WERROR,"File \""); EnvPrintRouter(theEnv,WERROR,file); EnvPrintRouter(theEnv,WERROR,"\" already loaded.\n"); return(-1); } /*===========================*/ /*Store the file entry topics*/ /*===========================*/ line_ct = 0; entries_ct = 0; INFO_BEGIN = FALSE; INFO_END = 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 == TRUE) { INFO_BEGIN = FALSE; INFO_END = TRUE; entries_ct++; } else { GenClose(theEnv,fp); TextLookupToss(theEnv,file); PrintErrorID(theEnv,"TEXTPRO",8,FALSE); EnvPrintRouter(theEnv,WERROR,"Line "); PrintLongInteger(theEnv,WERROR,line_ct); EnvPrintRouter(theEnv,WERROR," : Unmatched end marker.\n"); return(-1); } } else if (findstr(str,BDELIM) >= 0) { if (INFO_END == TRUE) { INFO_END = FALSE; INFO_BEGIN = TRUE; } else { GenClose(theEnv,fp); TextLookupToss(theEnv,file); PrintErrorID(theEnv,"TEXTPRO",4,FALSE); EnvPrintRouter(theEnv,WERROR,"Line "); PrintLongInteger(theEnv,WERROR,line_ct); EnvPrintRouter(theEnv,WERROR," : Previous entry not closed.\n"); return(-1); } if ((enode=AllocateEntryNode(theEnv,fp,file,str,line_ct))==NULL) return(-1); /*=================================*/ /*Store new entry node in the tree */ /*=================================*/ if (AttachLeaf(theEnv,lnode,enode,fp,file,line_ct) == FALSE) return(-1); } } } GenClose(theEnv,fp); if (INFO_END == FALSE) { TextLookupToss(theEnv,file); PrintErrorID(theEnv,"TEXTPRO",4,FALSE); EnvPrintRouter(theEnv,WERROR,"Line "); PrintLongInteger(theEnv,WERROR,line_ct); EnvPrintRouter(theEnv,WERROR," : Previous entry not closed.\n"); return(-1); } if (entries_ct == 0) TextLookupToss(theEnv,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( void *theEnv, char *file) { struct lists *plptr, *clptr; int l_flag; clptr = TextProcessingData(theEnv)->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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -