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

📄 textpro.c

📁 NASA 开发使用的一个专家系统
💻 C
📖 第 1 页 / 共 5 页
字号:
/******************************************************************************//*FUNCTION GET_ENTRIES :                                                      *//* Input : 1) name of file to be accessed for lookup of entry                 *//*         2) caller allocated buffer for main topic name                     *//*         3) name of the entry to be accessed in the file                    *//*         4) caller allocated buffer for a status code (see LOOKUP).         *//* Output : 1) returns a pointer into the stream of the lookup file which     *//*             indicates the starting position of the lookup information      *//*             (NULL if the topic was not found)                              *//* This function passes its input directly to LOOKUP.  See its description    *//* for further detail.                                                        *//*                                                                            *//******************************************************************************/static FILE *GetEntries(file,menu,name,code)  char *file, **menu, *name;  int *code;  {   FILE *fp;          /*Lookup file stream*/   long int offset;   /*Offset from beginning of file to beginning of topic*/   offset = LookupEntry(file,menu,name,code);   if (offset < 0)      return(NULL);   fp = fopen(file,OPEN_READ);   if (fp == NULL)     {      *code = NO_FILE;      return(NULL);     }   if (fseek(fp,offset,0) < 0)     {      fclose(fp);      *code = NO_FILE;      return(NULL);     }   return(fp);  }/******************************************************************************//*FUNCTION GET_CURR_MENU :                                                    *//* Input : 1) name of file to be accessed                                     *//*         2) caller allocated buffer for the current menu name               *//*         3) caller allocated buffer for status code : NO_FILE, NO_TOPIC, or *//*            NORMAL.                                                         *//* Output : 1) returns a pointer into the file stream indicating the beginning*//*             of the description of the current menu for the named file      *//*             (returns NULL if there is no current menu)                     *//******************************************************************************/static FILE *GetCurrentMenu(file,status)  char *file;  int *status;  {   struct lists *lptr;   /*Used in searching the file list*/   FILE *fp;             /*File stream*/   int l_flag;           /*Used in looping through the file list*/   /*=====================================*/   /*Find the named file in the file list */   /*=====================================*/   lptr = headings;   if (lptr != NULL)     if (strcmp(lptr->file,file) != 0)       l_flag = 1;     else       l_flag = 0;   else     l_flag = 0;   while (l_flag > 0)     {      lptr = lptr->next;      if (lptr != NULL)        if (strcmp(lptr->file,file) != 0)          l_flag = 1;        else          l_flag = 0;      else        l_flag = 0;     }   if (lptr == NULL)     {      *status = NO_FILE;      return(NULL);     }   /*============================================================*/   /*Position the pointer in the file stream to the current menu */   /*============================================================*/   if (lptr->curr_menu == NULL)     {      *status = NO_TOPIC;      return(NULL);     }   if ((fp = fopen(file,OPEN_READ)) == NULL)     {      *status = NO_FILE;      return(NULL);     }   if (fseek(fp,lptr->curr_menu->offset,0) < 0)     {      fclose(fp);      *status = NO_FILE;      return(NULL);     }   *status = NORMAL;   return(fp);  }/******************************************************************************//*FUNCTION GRAB_STRING :                                                      *//* Input : 1) file stream pointer                                             *//*         2) caller allocated buffer for storage of read string              *//*         3) size of caller's buffer                                         *//* Output : This function grabs a line of text from the currently opened      *//*          lookup file at the given file position in the stream.  If it      *//*          encounters EOF or the closing topic delimeter, it closes the file *//*          and returns NULL.  Otherwise, the return value is simply the      *//*          address of the caller's buffer.                                   *//*                                                                            *//* Notes : 1) This function expects a file pointer into a stream of a file    *//*            already opened!!                                                *//*         2) The caller must close the file himself if he wishes to          *//*            prematurely abort the complete reading of an entry.             *//******************************************************************************/static char *grab_string(fp,buf,bufsize)  FILE *fp;  char *buf;  int bufsize;  {   if (fgets(buf,bufsize,fp) == NULL)     {      fclose(fp);      return(NULL);     }   if ((buf[0] == LIT_DELIM) && (buf[1] == LIT_DELIM))     {      buf[0] = BLANK;      buf[1] = BLANK;     }   else if (findstr(buf,EDELIM) >= 0)     {      buf = NULL;      fclose(fp);     }   return(buf);  }/**************************************************************************//*FINDSTR FUNCTION :                                                      *//* Input : 1) string to be searched                                       *//*         2) string to be found                                          *//* Output : 1) returns index of string-1 where string-2 started, if found *//*          2) returns -1, if not found                                   *//**************************************************************************/static int findstr(s,t)  char *s, *t;  {   int i,j,k;   for (i = 0; s[i] != '\0'; i++)     {      for (j = i, k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++) ;      if ((t[k] == '\0') && (k != 0))        return(i);     }   return(-1);  }/**********************************************************************//*UPPER FUNCTION :                                                    *//* Input : 1) alphanumeric string                                     *//* Output : 1) all alphabetic characters of string are capitalized    *//**********************************************************************/static VOID upper(str)  char *str;  {   int index;   for (index = 0 ; str[index] != NULLCHAR; index++)     if (islower(str[index])) str[index] = (char) toupper(str[index]);  }/******************************************************************************//*FILE_NODE FUNCTION :                                                        *//* Input : 1) name of a file                                                  *//* Output : 1) returns address of an initalized NewFetchFile, if the file was    *//*             not already on the lookup table                                *//*          2) returns the null address, if the file was already present      *//******************************************************************************/static struct lists *NewFetchFile(file)  char *file;  {   struct lists *lptr = NULL, *lnode;   if (headings != NULL)     {      lptr = headings;      while (lptr->next != NULL)        {         if (strcmp(lptr->file,file) == 0)           return(NULL);         lptr = lptr->next;        }      if (strcmp(lptr->file,file) == 0)        return(NULL);     }   lnode = (struct lists *) gm2 ((int) sizeof(struct lists));   strcpy(lnode->file,file);   lnode->topics = NULL;   lnode->curr_menu = NULL;   lnode->next = NULL;   if (headings == NULL)     headings = lnode;   else     lptr->next = lnode;   return(lnode);  }/******************************************************************************//*ENTRIES_NODE FUNCTION :                                                     *//* Input : 1) file pointer                                                    *//*         2) file name                                                       *//*         3) input string from the file                                      *//*         4) buffer for error messages                                       *//*         5) size of the error message buffer                                *//*         6) line count in the file                                          *//* Output :                                                                   *//*This function scans the input string for the appropriate topic entry        *//*delimeter and, if it finds this to be correct, allocates a new entry node,  *//*and initializes it, and returns the address to the calling routine.  If an  *//*error is detected, the function writes an appropriate message to the        *//*caller's buffer, deallocates the node, deletes all previous nodes from the  *//*current file from the lookup table, closes the file, and returns the null   *//*address.                                                                    *//******************************************************************************/static struct entries *AllocateEntryNode(fp,file,str,errbuf,bufsize,line_ct)  FILE *fp;  char *str, *file, *errbuf;  int bufsize, line_ct;  {   struct entries *enode;   char bmarker[BDLEN+1],  /*Entry topic delimiting strings         */        t_code[2];         /*Type of entry flag : menu or info      */   /*================================================================*/   /*Allocate a new node and scan the delimeter string for tree info */   /*================================================================*/   enode = (struct entries *) gm2 ((int) sizeof(struct entries));   if (sscanf(str,BFORMAT,              &enode->level,t_code,bmarker,enode->name) != 4)     {      rm((VOID *) enode,(int) sizeof(struct entries));      fclose(fp);      TextLookupToss(file);      if (bufsize >= 60)        sprintf(errbuf,"Line %d : Invalid delimeter string.",line_ct);      return(NULL);     }   if (t_code[0] == 'M')     enode->type = MENU;   else if (t_code[0] == 'I')     enode->type = INFO;   else     {      rm((VOID *) enode,(int) sizeof(struct entries));      fclose(fp);      TextLookupToss(file);      if (bufsize >= 60)        sprintf(errbuf,"Line %d : Invalid entry type.",line_ct);      return(NULL);     }   if (strcmp(bmarker,BDELIM) != 0)     {      rm((VOID *) enode,(int) sizeof(struct entries));      fclose(fp);      TextLookupToss(file);      if (bufsize >= 60)        sprintf(errbuf,"Line %d : Invalid delimeter string.",line_ct);      return(NULL);     }   /*===============================================================*/   /* For systems which have record file systems (such as VMS),     */   /* the following statement is necessary to move the file pointer */   /* to the beginning of the next record.                          */   /*===============================================================*/   ungetc(getc(fp),fp);   enode->offset = ftell(fp);   enode->parent = NULL;   enode->child  = NULL;   enode->next = NULL;   upper(enode->name);   return(enode);  }/******************************************************************************//*FUNCTION ATTACH_LEAF :                                                      *//* Input : 1) address of current NewFetchFile                                    *//*         2) address of current topic entry-node                             *//*         3) file pointer                                                    *//*         4) name of file                                                    *//*         5) error message buffer                                            *//*         6) size of error message buffer                                    *//*         7) line count in the file                                          *//* Output :                                                                   *//*This function attaches the entry-node to its proper place in the tree of the*//*current file.  The function returns a boolean flag indicating the success   *//*(or lack thereof) of this connection.  In the case of an error, an error    *//*message is written to the caller's buffer, the file is closed, and the      *//*previous file entries are deleted from the lookup table.                    *//******************************************************************************/static int AttachLeaf(lnode,enode,fp,file,errbuf,bufsize,line_ct)  struct lists *lnode;  struct entries *enode;  FILE *fp;  char *file, *errbuf;  int bufsize, line_ct;  {   static struct entries *parent = NULL;                 /*Address of previous topic-entry loaded from the file. */                 /*Must be static to preserve value across function calls*/   int p_flag;   /*Used in searching the tree for a parent*/   /*====================*/   /*First topic for file*/   /*====================*/   if (lnode->topics == NULL)     lnode->topics = enode;

⌨️ 快捷键说明

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