📄 iomngr.c
字号:
int CurrentLength; int NumLinesRead; static char *TempStr = NULL; static int TempStrLength = 0; /* Allocate space for temporary string */ if (MaxLength>TempStrLength) { FREE (TempStr) ALLOCATE (TempStr, char, MaxLength) TempStrLength = MaxLength; } /* Initialize flags */ NeedMore = TRUE; Empty = FALSE; NumLinesRead = 0; RemainingLength = MaxLength; CurrentLength = 0; /* Read input lines until last one has not trailing '\' */ while (NeedMore && !Empty && RemainingLength>0) { /* Read string from input file */ Result = fgets (&(TempStr[CurrentLength]), RemainingLength, InputFile); /* Determine if string is empty */ Empty = (Result==NULL); /* Quit loop if string is empty */ if (Empty) break; /* Increment number of lines read */ NumLinesRead++; /* Get current length without trailing \n */ CurrentLength = strlen(TempStr); if (TempStr[CurrentLength-1]=='\n') { CurrentLength--; TempStr[CurrentLength] = '\0'; } /* If last character is '\', need another string to concatenate */ NeedMore = (CurrentLength>0 && TempStr[CurrentLength-1]=='\\'); /* If last character is '\', omit it */ if (NeedMore) { CurrentLength--; TempStr[CurrentLength] = '\0'; } /* Calculate remaining length available */ RemainingLength -= CurrentLength; } /* Test for end of file */ if (NumLinesRead==0) { return NULL; } /* Copy working string into output string */ strcpy (OutputStr, TempStr); /* Return NULL if string not read correctly */ if (Empty && NeedMore) return NULL; /* return pointer to output string */ else return OutputStr; }/*************************************************************************Macro Functions*************************************************************************/void read_macro(char *InputStr) { int imacro; char *MacroName = NULL; char *MacroValue= NULL; MacroName = strhed (&InputStr); MacroValue = InputStr; /* Does Macro Exist */ imacro = GetMacroIndex(MacroName); if (imacro==MACRO_NONE) { AddMacro (MacroName, MacroValue); } else { ChangeMacro (imacro, MacroValue); } printf ("*** Macro value <%s>\n", MacroValue); }void read_macrof(char *InputStr) { int imacro; char *MacroName = NULL; char *FormatPtr = NULL; char MacroValue[NSTR]; char *ptr; double dvalue; int ivalue; char *svalue; char cvalue; MacroName = strhed (&InputStr); FormatPtr = GetQuotedString (&InputStr, '"'); ReplaceSpecialChar (FormatPtr); /* Find variable type from format */ ptr = strchr (FormatPtr, '%'); if (ptr!=NULL) { ptr++; ptr = strpbrk (ptr, "cdefisuxCDEFISUX %"); } if (ptr==NULL || *ptr=='\0' || *ptr==' ' || *ptr=='%') { ERROR_PREFIX printf ("Cannot find valid format string.\n"); printf ("Must be of format similar to %%12.4e or %%5i\n"); printf ("where format letter can be cdefisux.\n"); CleanAfterError(); } /* Use correct number reading function */ switch (tolower(*ptr)) { case 'e': case 'f': sprintf (MacroValue, FormatPtr, dblstrf (&InputStr) ); break; case 'd': case 'i': case 'u': case 'x': case 'p': sprintf (MacroValue, FormatPtr, intstrf (&InputStr) ); break; case 's': sprintf (MacroValue, FormatPtr, strhed (&InputStr) ); break; case 'c': sprintf (MacroValue, FormatPtr, InputStr[0]); break; default: ERROR_PREFIX printf ("Invalid macro format entered. Program stoppings\n"); CleanAfterError(); break; } /* Does Macro Exist */ imacro = GetMacroIndex(MacroName); if (imacro==MACRO_NONE) { AddMacro (MacroName, MacroValue); } else { ChangeMacro (imacro, MacroValue); } }/*Replace macro with its value _in place_ within string*/void ReplaceMacro (char *InputStr, int MaxLen) { BOOLEAN IsMacroEnd; BOOLEAN IsStringEnd; BOOLEAN IsMatchParen; char IntegerStr[NSTR]; char Char; int ichar; int ochar; int mchar; int imacro; char *MacroName = NULL; char *OutputStr = NULL; ichar = 0; ochar = 0; ALLOCATE (MacroName, char, MaxLen) ALLOCATE (OutputStr, char, MaxLen) /* Copy input to output while not a Macro */ do { /* Start of macro */ if (InputStr[ichar]==MACRO_CHAR) { /* Move to next character */ ichar++; /* Replace double MACRO_CHAR with single MACRO_CHAR */ if (InputStr[ichar]==MACRO_CHAR) { OutputStr[ochar] = MACRO_CHAR; ichar++; ochar++; } else { /* Is next character Parenthesis - find matching */ IsMatchParen = (InputStr[ichar]==MACRO_LPAREN); if (IsMatchParen) ichar++; /* Search until end of macro string */ mchar = 0; IsMacroEnd = FALSE; IsStringEnd = FALSE; while (!IsMacroEnd && !IsStringEnd) { /* Detect end of macro name */ if (IsMatchParen) { IsMacroEnd = (InputStr[ichar]==MACRO_RPAREN); } else { IsMacroEnd = !IS_ALPHANUM(InputStr[ichar]); } /* Detect end of string */ IsStringEnd = (InputStr[ichar]=='\0'); /* Add character to Macro Name */ if (!IsMacroEnd && !IsStringEnd) { MacroName[mchar] = InputStr[ichar]; mchar++; ichar++; } } /* Add trailing NULL to MacroName[] */ MacroName[mchar] = '\0'; /* Insure at matching parenthesis if in that mode */ ASSERT(!IsMatchParen || InputStr[ichar]==MACRO_RPAREN) /* Consume trailing parenthesis */ if (IsMatchParen) { ichar++; } /* Find and substitute macro value */ imacro = GetMacroIndex(MacroName); /* Name is not a Macro try Name as variable, using integer representation */ if (imacro==MACRO_NONE) { if (DoesVariableExist(MacroName)) { sprintf (IntegerStr, "%.0f", GetVariableValue(MacroName)); mchar = 0; while (IntegerStr[mchar]!='\0') { if (ochar<MaxLen) OutputStr[ochar] = IntegerStr[mchar]; ochar++; mchar++; } } } /* Write out macro value */ else { mchar = 0; while (MacroValue_m[imacro][mchar]!='\0') { if (ochar<MaxLen) OutputStr[ochar] = MacroValue_m[imacro][mchar]; ochar++; mchar++; } } } } /* start of macro */ /* Character not start of macro */ else { if (ochar<MaxLen && InputStr[ichar]!='\0') OutputStr[ochar] = InputStr[ichar]; ichar++; ochar++; } } while (InputStr[ichar]!='\0'); /* Add trailing NULL */ if (ochar<MaxLen) { OutputStr[ochar] = '\0'; } /* Copy OutputStr to InputStr */ strncpy (InputStr, OutputStr, strlen(OutputStr)+1); FREE(MacroName) FREE(OutputStr) }/* Simple linear search through array of names */static int GetMacroIndex (char *MacroName) { BOOLEAN IsFound = FALSE; int Index; int imacro; LOOP (imacro, NumMacro_m) { if (!strcmpi(MacroName, MacroName_m[imacro])) { IsFound = TRUE; Index = imacro; break; } } if (IsFound) return Index; else return MACRO_NONE; }/* Add macro to list */void AddMacro (char *MacroName, char *MacroValue) { /* Reallocate string pointer array storage */ if (NumMacro_m>=NumMacroAlloc_m) { REALLOC (MacroName_m, char *, NumMacroAlloc_m, NumMacroAlloc_m+MACRO_ALLOC) REALLOC (MacroValue_m, char *, NumMacroAlloc_m, NumMacroAlloc_m+MACRO_ALLOC) NumMacroAlloc_m += MACRO_ALLOC; } /* Allocate string storage */ if (MacroName==NULL) { MacroName_m[NumMacro_m] = NULL; } else { ALLOCATE (MacroName_m[NumMacro_m], char, strlen(MacroName )+1) strcpy (MacroName_m[NumMacro_m], MacroName); } if (MacroValue==NULL) { MacroValue_m[NumMacro_m] = NULL; } else { ALLOCATE (MacroValue_m[NumMacro_m], char, strlen(MacroValue)+1) strcpy (MacroValue_m[NumMacro_m], MacroValue); } /* Increment number of macros */ NumMacro_m++; }/* Change value of existing macro */static void ChangeMacro (int index, char *MacroValue) { if (index>=NumMacro_m) { printf ("INTERNAL ERROR: ChangeMacro() called with invalid index.\n"); CleanAfterError(); } FREE (MacroValue_m[index]) ALLOCATE (MacroValue_m[index], char, strlen(MacroValue)+1) strcpy (MacroValue_m[index], MacroValue); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -