📄 iomngr.c
字号:
/* INPUT MANAGER - READS INPUT FROM A INPUT OF TEXT FILES AND/OR BUFFERS - FUNCTIONS DESIGNED TO REPLACE fgets AND fscanf (ALTHOUGH CALLING SEQUENCE DIFFERS).27 Jan 1998 In ConcatenateGets() was ALLOCATE and FREE 'ing each call. Made TempStr static so only need to allocate when previous allocation was too small.21 May 1998 Add MACRO routines *//*************************************************************************Include Files*************************************************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdarg.h>#include "cdhouse.h"#include "strsub.h"#include "iomngr.h"#include "cdsubs.h"#include "parse.h"/*************************************************************************Defines*************************************************************************/#define BOOLEAN int#define TRUE 1#define FALSE 0#define MACRO_NONE -1#define MACRO_CHAR '$'#define MACRO_LPAREN '('#define MACRO_RPAREN ')'#define MACRO_ALLOC 100#define NSTR 256/*************************************************************************Macros*************************************************************************/#define IS_ALPHANUM(c) \ (((c)>='0' && (c)<='9') || \ ((c)>='a' && (c)<='z') || \ ((c)>='A' && (c)<='Z'))/*************************************************************************Module Variables*************************************************************************/static char **MacroName_m = NULL;static char **MacroValue_m = NULL;static int NumMacro_m = 0;static int NumMacroAlloc_m = 0;/*************************************************************************Local Function Prototypes*************************************************************************/static char *ConcatenateGets (char *, int, FILE *);static int GetMacroIndex(char *);static void ChangeMacro (int index, char *MacroValue);/*************************************************************************Exported subroutines*************************************************************************/BUFF *m_ini_buff (void) { BUFF *NewBuff = NULL; /* Allocate New Buffer */ ALLOCATE (NewBuff, BUFF, 1) /* Initialize Pointers to Nodes to NULL */ NewBuff->FirstNode = NULL; NewBuff->LastNode = NULL; NewBuff->CurrentNode = NULL; /* Return */ return NewBuff; }void m_rew_buff (BUFF *Buffer) { Buffer->CurrentNode = Buffer->FirstNode; }void m_add_buff (BUFF *Buffer, char *InputString) { StringNode_t *NewNode = NULL; char *NewString = NULL; /* Allocate New Node to hold string */ ALLOCATE (NewNode, StringNode_t, 1) ALLOCATE (NewString, char, strlen(InputString)+1) /* Copy newstring into allocated space */ strcpy (NewString, InputString); NewNode->String = NewString; /* If Buffer List is empty, start new list for node ... */ if (Buffer->LastNode==NULL) { Buffer->FirstNode = NewNode; Buffer->LastNode = NewNode; Buffer->CurrentNode = NewNode; } /* .. else add node to end of current list */ else { /* Set pointer to current last node in list to new node */ Buffer->LastNode->NextNode = NewNode; /* Set pointer to current last node in buffer to new node */ Buffer->LastNode = NewNode; } }/* Delete entire buffer */void m_del_buff (BUFF *Buffer) { StringNode_t *NextNode; StringNode_t *CurrentNode; CurrentNode = Buffer->FirstNode; while (CurrentNode != NULL) { NextNode = CurrentNode->NextNode; FREE (CurrentNode->String) FREE (CurrentNode); CurrentNode = NextNode; } }char *m_gets_buff (char *OutputString, int MaxStringLength, BUFF *Buffer) { /* If at end of node list return null */ if (Buffer->CurrentNode==NULL) return NULL; /* Copy current node string into OuputString */ strncpy (OutputString, Buffer->CurrentNode->String, MaxStringLength); /* Advance Current Node */ Buffer->CurrentNode = Buffer->CurrentNode->NextNode; /* Return pointer to output string */ return OutputString; }#ifdef __TURBOC__int m_scanf_buff (BUFF *Buffer, char *FormatString, ...) { int cnt; va_list argptr; /* CHECK FOR END OF BUFFER */ if (Buffer->CurrentNode == NULL) return 0; /* Var pointer stuff */ va_start (argptr, FormatString); cnt = vsscanf (Buffer->CurrentNode->String, FormatString, argptr); va_end (argptr); /* Advance current node to next in the list */ Buffer->CurrentNode = Buffer->CurrentNode->NextNode; return (cnt); }#endifint m_eof_buff (BUFF *Buffer) { return Buffer->CurrentNode==NULL; }/* Initialize INPUT list, just return NULL */LIST *m_ini_list (void) { return (LIST *) NULL; }/* Add Buffer or File to Front of Input List */void m_add_list(LIST **InputListPtr,void *FileOrBuffer,char *NewInputNodeType) { LIST *NewInputNode = NULL; BOOLEAN IsBuffer; /* Create new input node */ ALLOCATE (NewInputNode, LIST, 1) /* Determine if input node is buffer type */ IsBuffer = (NewInputNodeType[0] == 'b'); /* Pointer from new node to next node */ NewInputNode->NextFileOrBuffer = (void *) *InputListPtr; /* Indicate if new node is buffer type */ NewInputNode->IsBuffer = IsBuffer; /* Assign pointer to File or Buffer */ NewInputNode->FileOrBuffer = (void *) FileOrBuffer; /* Set new input list pointer to new node */ *InputListPtr = NewInputNode; }/* Delete first node from input list */void m_del_list (LIST **InputListPtr) { LIST *CurrentNode; LIST *NextNode; /* Obtain address to first node */ CurrentNode = *InputListPtr; /* Return if at end of list */ if (CurrentNode == NULL) return; /* Save following node */ NextNode = CurrentNode->NextFileOrBuffer; /* Free buffer at current list node */ if (CurrentNode->IsBuffer) { m_del_buff ( (BUFF *) CurrentNode->FileOrBuffer); } /* Free file at current list node */ else { fclose ( (FILE *) CurrentNode->FileOrBuffer); } /* Free current list node */ FREE (CurrentNode) /* Set List pointer to following node */ *InputListPtr = NextNode; }void m_rew_list (LIST *InputList) { if (InputList->IsBuffer) m_rew_buff ( (BUFF *) InputList->FileOrBuffer); else rewind ( (FILE *) InputList->FileOrBuffer); }/* Get next line from whatever source, replace macros */char *m_gets_list(char *OutputString,int MaxStringLength,LIST *InputList) { if (NULL==m_gets_list_f (OutputString, MaxStringLength, InputList)) return NULL; /* Process macros */ ReplaceMacro (OutputString, MaxStringLength); return OutputString; }/* Get next line from whatever source, don't replace macros */char *m_gets_list_f(char *OutputString,int MaxStringLength,LIST *InputList) { int StringLength; char *ReadStatus; /* If buffer then call buffer gets routine */ if (InputList->IsBuffer) return m_gets_buff ( OutputString, MaxStringLength, (BUFF *) InputList->FileOrBuffer ); /* Read from standard file */#if 0 ReadStatus = fgets (OutputString, MaxStringLength, (FILE *) InputList->FileOrBuffer);#endif ReadStatus = ConcatenateGets (OutputString, MaxStringLength, (FILE *) InputList->FileOrBuffer); /* Test for end of file */ if (ReadStatus == NULL) return NULL; /* Remove trailing \n */ /* Remove trailing \n (NOTE: Strictly speaking StringLength should always be greater than 0 because of presences of newline character, but we test it just in case) */ StringLength = strlen (OutputString); if (StringLength > 0 && OutputString[StringLength-1]=='\n') OutputString[StringLength-1] = '\0'; /* Return Pointer to Output string */ return OutputString; }#ifdef __TURBOC__int m_scanf_list (LIST *InputList, char *FormatString, ...) { char StringBuffer[1024]; int cnt; va_list argptr; /* GET INPUT LINE */ if (m_gets_list (StringBuffer, 1024, InputList) ) { va_start (argptr, FormatString); cnt = vsscanf (StringBuffer, FormatString, argptr); va_end (argptr); } else cnt = 0; return (cnt); }#endifint m_eof_list (LIST *InputList) { if (InputList->IsBuffer) return m_eof_buff ( (BUFF *) InputList->FileOrBuffer); else return feof ( (FILE *) InputList->FileOrBuffer); }/* Return number of files/buffers in LIST */int m_get_depth (LIST *CurNode) { int Depth = 0; while (CurNode!=NULL) { Depth++; CurNode = CurNode->NextFileOrBuffer; } return Depth; }/*************************************************************************Local Functions*************************************************************************//*Read string from input file, contatenating those that end with '\'Its ok if last line ends with '\', the trailing '\' will be ignoredTrailing \n is removed (unlike original fgets() )*/static char *ConcatenateGets (char *OutputStr, int MaxLength, FILE *InputFile) { char *Result; BOOLEAN NeedMore; BOOLEAN Empty; int RemainingLength;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -