-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[preproc] asm files parseable from stdin, 2nd preproc pass
- Loading branch information
sbird
committed
Apr 1, 2024
1 parent
7a99226
commit d5acfb4
Showing
9 changed files
with
86 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,4 @@ class CFile | |
void RaiseWarning(const char* format, ...); | ||
}; | ||
|
||
#define CHUNK_SIZE 4096 | ||
|
||
#endif // C_FILE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "preproc.h" | ||
#include "io.h" | ||
#include <string> | ||
#include <cstring> | ||
|
||
char *ReadFileToBuffer(const char *filename, bool isStdin, long *size) | ||
{ | ||
FILE *fp; | ||
if (isStdin) | ||
fp = stdin; | ||
else | ||
fp = std::fopen(filename, "rb"); | ||
|
||
if (fp == NULL) | ||
FATAL_ERROR("Failed to open \"%s\" for reading.\n", filename); | ||
|
||
*size = 0; | ||
char *buffer = (char *)malloc(CHUNK_SIZE + 1); | ||
if (buffer == NULL) { | ||
FATAL_ERROR("Failed to allocate memory to process file \"%s\"!", filename); | ||
} | ||
|
||
std::size_t numAllocatedBytes = CHUNK_SIZE + 1; | ||
std::size_t bufferOffset = 0; | ||
std::size_t count; | ||
|
||
while ((count = std::fread(buffer + bufferOffset, 1, CHUNK_SIZE, fp)) != 0) { | ||
if (!std::ferror(fp)) { | ||
*size += count; | ||
|
||
if (std::feof(fp)) { | ||
break; | ||
} | ||
|
||
numAllocatedBytes += CHUNK_SIZE; | ||
bufferOffset += CHUNK_SIZE; | ||
buffer = (char *)realloc(buffer, numAllocatedBytes); | ||
if (buffer == NULL) { | ||
FATAL_ERROR("Failed to allocate memory to process file \"%s\"!", filename); | ||
} | ||
} else { | ||
FATAL_ERROR("Failed to read \"%s\". (error: %s)", filename, std::strerror(errno)); | ||
} | ||
} | ||
|
||
buffer[*size] = 0; | ||
|
||
std::fclose(fp); | ||
return buffer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef IO_H_ | ||
#define IO_H_ | ||
|
||
#define CHUNK_SIZE 4096 | ||
|
||
char *ReadFileToBuffer(const char *filename, bool isStdin, long *size); | ||
|
||
#endif // IO_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters