Finished Language Generation Tool
This commit is contained in:
@@ -126,6 +126,46 @@ bool_t File::readString(std::string *out) {
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t File::readAhead(char *buffer, size_t max, char needle) {
|
||||
assertNotNull(buffer);
|
||||
assertTrue(max > 0);
|
||||
|
||||
if(!this->isOpen()) {
|
||||
if(!this->open(FILE_MODE_READ)) return 0;
|
||||
}
|
||||
assertTrue(this->mode == FILE_MODE_READ);
|
||||
|
||||
// Buffer
|
||||
size_t pos = ftell(this->file);
|
||||
size_t amountLeftToRead = mathMin<size_t>(max, this->length - pos);
|
||||
char temporary[FILE_BUFFER_SIZE];
|
||||
size_t n = 0;
|
||||
|
||||
while(amountLeftToRead > 0) {
|
||||
size_t toRead = mathMin<size_t>(amountLeftToRead, FILE_BUFFER_SIZE);
|
||||
amountLeftToRead -= toRead;
|
||||
// Read bytes
|
||||
size_t read = fread(temporary, sizeof(char), toRead, this->file);
|
||||
|
||||
// Read error?
|
||||
if(toRead != read) return 0;
|
||||
|
||||
// Did we read the needle?
|
||||
size_t i = 0;
|
||||
while(i < read) {
|
||||
char c = temporary[i++];
|
||||
if(c == needle) {
|
||||
return n;
|
||||
} else {
|
||||
buffer[n++] = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Needle was not found.
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool_t File::writeString(std::string in) {
|
||||
if(!this->isOpen()) {
|
||||
if(!this->open(FILE_MODE_WRITE)) return false;
|
||||
@@ -143,6 +183,11 @@ bool_t File::writeRaw(char *data, size_t len) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void File::setPosition(size_t n) {
|
||||
fseek(this->file, 0, SEEK_SET);
|
||||
fseek(this->file, n, SEEK_CUR);
|
||||
}
|
||||
|
||||
File::~File() {
|
||||
if(this->file != nullptr) this->close();
|
||||
}
|
@@ -33,14 +33,14 @@ namespace Dawn {
|
||||
class File {
|
||||
private:
|
||||
enum FileMode mode;
|
||||
size_t length;
|
||||
|
||||
public:
|
||||
FILE *file = nullptr;
|
||||
static std::string normalizeSlashes(std::string str);
|
||||
static void mkdirp(std::string path);
|
||||
|
||||
std::string filename;
|
||||
size_t length;
|
||||
FILE *file = nullptr;
|
||||
|
||||
/**
|
||||
* Constructs a new File interface class.
|
||||
@@ -95,6 +95,20 @@ namespace Dawn {
|
||||
*/
|
||||
bool_t readString(std::string *out);
|
||||
|
||||
/**
|
||||
* Reads ahead from the current position to a specific needle (character).
|
||||
*
|
||||
* @param buffer Buffer to output read chars to.
|
||||
* @param max Max length of the buffer / amount of chars to read ahead.
|
||||
* @param needle The character (needle) to look for.
|
||||
* @return Amount of chars read, or <= 0 on error.
|
||||
*/
|
||||
size_t readAhead(
|
||||
char *buffer,
|
||||
size_t max,
|
||||
char needle
|
||||
);
|
||||
|
||||
/**
|
||||
* Writes the entire contents of a string to a file.
|
||||
*
|
||||
@@ -111,6 +125,8 @@ namespace Dawn {
|
||||
*/
|
||||
bool_t writeRaw(char *data, size_t );
|
||||
|
||||
void setPosition(size_t pos);
|
||||
|
||||
~File();
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user