📄 asfile.c
字号:
for (i = 0; MacroTable[i].Identifier; i++) { AsRemoveMacro (FileBuffer, MacroTable[i].Identifier); } } if (StructTable) { for (i = 0; StructTable[i].Identifier; i++) { AsInsertPrefix (FileBuffer, StructTable[i].Identifier, StructTable[i].Type); } } /* Process the function table */ for (i = 0; i < 32; i++) { /* Decode the function bitmap */ switch ((1 << i) & Functions) { case 0: /* This function not configured */ break; case CVT_COUNT_TABS: AsCountTabs (FileBuffer, Filename); break; case CVT_COUNT_NON_ANSI_COMMENTS: AsCountNonAnsiComments (FileBuffer, Filename); break; case CVT_CHECK_BRACES: AsCheckForBraces (FileBuffer, Filename); break; case CVT_TRIM_LINES: AsTrimLines (FileBuffer, Filename); break; case CVT_COUNT_LINES: AsCountSourceLines (FileBuffer, Filename); break; case CVT_BRACES_ON_SAME_LINE: AsBracesOnSameLine (FileBuffer); break; case CVT_MIXED_CASE_TO_UNDERSCORES: AsMixedCaseToUnderscores (FileBuffer); break; case CVT_LOWER_CASE_IDENTIFIERS: AsLowerCaseIdentifiers (FileBuffer); break; case CVT_REMOVE_DEBUG_MACROS: AsRemoveDebugMacros (FileBuffer); break; case CVT_TRIM_WHITESPACE: AsTrimWhitespace (FileBuffer); break; case CVT_REMOVE_EMPTY_BLOCKS: AsRemoveEmptyBlocks (FileBuffer, Filename); break; case CVT_REDUCE_TYPEDEFS: AsReduceTypedefs (FileBuffer, "typedef union"); AsReduceTypedefs (FileBuffer, "typedef struct"); break; case CVT_SPACES_TO_TABS4: AsTabify4 (FileBuffer); break; case CVT_SPACES_TO_TABS8: AsTabify8 (FileBuffer); break; case CVT_COUNT_SHORTMULTILINE_COMMENTS:#ifdef ACPI_FUTURE_IMPLEMENTATION AsTrimComments (FileBuffer, Filename);#endif break; default: printf ("Unknown conversion subfunction opcode\n"); break; } } if (ConversionTable->NewHeader) { AsReplaceHeader (FileBuffer, ConversionTable->NewHeader); }}/****************************************************************************** * * FUNCTION: AsProcessOneFile * * DESCRIPTION: Process one source file. The file is opened, read entirely * into a buffer, converted, then written to a new file. * ******************************************************************************/ACPI_NATIVE_INTAsProcessOneFile ( ACPI_CONVERSION_TABLE *ConversionTable, char *SourcePath, char *TargetPath, int MaxPathLength, char *Filename, ACPI_NATIVE_INT FileType){ char *Pathname; char *OutPathname = NULL; Gbl_HeaderSize = LINES_IN_LEGAL_HEADER; /* Normal C file and H header */ if (strstr (Filename, ".asl")) { Gbl_HeaderSize = 29; /* Lines in default ASL header */ } /* Allocate a file pathname buffer for both source and target */ Pathname = calloc (MaxPathLength + strlen (Filename) + 2, 1); if (!Pathname) { printf ("Could not allocate buffer for file pathnames\n"); return -1; } Gbl_FileType = FileType; /* Generate the source pathname and read the file */ if (SourcePath) { strcpy (Pathname, SourcePath); strcat (Pathname, "/"); } strcat (Pathname, Filename); if (AsGetFile (Pathname, &Gbl_FileBuffer, &Gbl_FileSize)) { return -1; } /* Process the file in the buffer */ Gbl_MadeChanges = FALSE; if (!Gbl_IgnoreLoneLineFeeds && Gbl_HasLoneLineFeeds) { /* * All lone LFs will be converted to CR/LF * (when file is written, Windows version only) */ printf ("Converting lone linefeeds\n"); Gbl_MadeChanges = TRUE; } AsConvertFile (ConversionTable, Gbl_FileBuffer, Pathname, FileType); if (!(ConversionTable->Flags & FLG_NO_FILE_OUTPUT)) { if (!(Gbl_Overwrite && !Gbl_MadeChanges)) { /* Generate the target pathname and write the file */ OutPathname = calloc (MaxPathLength + strlen (Filename) + 2 + strlen (TargetPath), 1); if (!OutPathname) { printf ("Could not allocate buffer for file pathnames\n"); return -1; } strcpy (OutPathname, TargetPath); if (SourcePath) { strcat (OutPathname, "/"); strcat (OutPathname, Filename); } AsPutFile (OutPathname, Gbl_FileBuffer, ConversionTable->Flags); } } free (Gbl_FileBuffer); free (Pathname); if (OutPathname) { free (OutPathname); } return 0;}/****************************************************************************** * * FUNCTION: AsCheckForDirectory * * DESCRIPTION: Check if the current file is a valid directory. If not, * construct the full pathname for the source and target paths. * Checks for the dot and dot-dot files (they are ignored) * ******************************************************************************/ACPI_NATIVE_INTAsCheckForDirectory ( char *SourceDirPath, char *TargetDirPath, char *Filename, char **SourcePath, char **TargetPath){ char *SrcPath; char *TgtPath; if (!(strcmp (Filename, ".")) || !(strcmp (Filename, ".."))) { return -1; } SrcPath = calloc (strlen (SourceDirPath) + strlen (Filename) + 2, 1); if (!SrcPath) { printf ("Could not allocate buffer for directory source pathname\n"); return -1; } TgtPath = calloc (strlen (TargetDirPath) + strlen (Filename) + 2, 1); if (!TgtPath) { printf ("Could not allocate buffer for directory target pathname\n"); free (SrcPath); return -1; } strcpy (SrcPath, SourceDirPath); strcat (SrcPath, "/"); strcat (SrcPath, Filename); strcpy (TgtPath, TargetDirPath); strcat (TgtPath, "/"); strcat (TgtPath, Filename); *SourcePath = SrcPath; *TargetPath = TgtPath; return 0;}/****************************************************************************** * * FUNCTION: AsGetFile * * DESCRIPTION: Open a file and read it entirely into a an allocated buffer * ******************************************************************************/intAsGetFile ( char *Filename, char **FileBuffer, UINT32 *FileSize){ int FileHandle; UINT32 Size; char *Buffer; /* Binary mode leaves CR/LF pairs */ FileHandle = open (Filename, O_BINARY | O_RDONLY); if (!FileHandle) { printf ("Could not open %s\n", Filename); return -1; } if (fstat (FileHandle, &Gbl_StatBuf)) { printf ("Could not get file status for %s\n", Filename); goto ErrorExit; } /* * Create a buffer for the entire file * Add plenty extra buffer to accomodate string replacements */ Size = Gbl_StatBuf.st_size; Buffer = calloc (Size * 2, 1); if (!Buffer) { printf ("Could not allocate buffer of size %d\n", Size * 2); goto ErrorExit; } /* Read the entire file */ Size = read (FileHandle, Buffer, Size); if (Size == -1) { printf ("Could not read the input file %s\n", Filename); goto ErrorExit; } Buffer [Size] = 0; /* Null terminate the buffer */ close (FileHandle); /* Check for unix contamination */ Gbl_HasLoneLineFeeds = AsDetectLoneLineFeeds (Filename, Buffer); /* * Convert all CR/LF pairs to LF only. We do this locally so that * this code is portable across operating systems. */ AsConvertToLineFeeds (Buffer); *FileBuffer = Buffer; *FileSize = Size; return 0;ErrorExit: close (FileHandle); return -1;}/****************************************************************************** * * FUNCTION: AsPutFile * * DESCRIPTION: Create a new output file and write the entire contents of the * buffer to the new file. Buffer must be a zero terminated string * ******************************************************************************/intAsPutFile ( char *Pathname, char *FileBuffer, UINT32 SystemFlags){ UINT32 FileSize; int DestHandle; int OpenFlags; /* Create the target file */ OpenFlags = O_TRUNC | O_CREAT | O_WRONLY | O_BINARY; if (!(SystemFlags & FLG_NO_CARRIAGE_RETURNS)) { /* Put back the CR before each LF */ AsInsertCarriageReturns (FileBuffer); } DestHandle = open (Pathname, OpenFlags, S_IREAD | S_IWRITE); if (DestHandle == -1) { perror ("Could not create destination file"); printf ("Could not create destination file \"%s\"\n", Pathname); return -1; } /* Write the buffer to the file */ FileSize = strlen (FileBuffer); write (DestHandle, FileBuffer, FileSize); close (DestHandle); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -