summaryrefslogtreecommitdiff
path: root/src/source.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/source.cc')
-rw-r--r--src/source.cc34
1 files changed, 26 insertions, 8 deletions
diff --git a/src/source.cc b/src/source.cc
index a393213..bf00afc 100644
--- a/src/source.cc
+++ b/src/source.cc
@@ -1,19 +1,35 @@
#ifndef SOURCE_CC
#define SOURCE_CC
+#include <cstdio>
#include "common.cc"
#include "memory.cc"
-typedef u32 Source_Id;
-
struct Span {
String file;
usize start, end;
Span() : file(), start(0), end(0) {}
- Span(String file, usize start, usize end) : file(file), start(start), end(end) {}
+ Span(String file, usize start, usize end)
+ : file(file), start(start), end(end) {}
};
+bool span_write(const Span* span, FILE* stream) {
+ assert_neq(span, nullptr);
+ assert_neq(stream, nullptr);
+
+ i32 rc = fprintf(stream, "Span { file: ");
+ if(unlikely(rc < 0)) return false;
+
+ bool status = string_write(&span->file, stream);
+ if(unlikely(!status)) return false;
+
+ rc = fprintf(stream, ", start: %zu, end: %zu }", span->start, span->end);
+ if(unlikely(rc < 0)) return false;
+
+ return true;
+}
+
struct Buffer {
String file;
String content;
@@ -22,15 +38,17 @@ struct Buffer {
usize cursor;
};
-bool buffer_init(const Allocator* allocator,
- const String* content_in, const String* file_in, Buffer** out) {
+bool buffer_init(const Allocator* allocator, const String* content_in,
+ const String* file_in, Buffer** out) {
assert_neq(allocator, nullptr);
assert_neq(content_in, nullptr);
assert_neq(file_in, nullptr);
assert_neq(out, nullptr);
+ //TODO: use defer!!
+
Buffer* buffer = allocate<Buffer>(allocator, 1);
- if(buffer == nullptr) return false;
+ if (buffer == nullptr) return false;
memset(buffer, 0, sizeof(*buffer));
String content, file;
@@ -59,8 +77,8 @@ void buffer_deinit(Buffer* buffer) {
assert_neq(buffer, nullptr);
const Allocator* allocator = buffer->allocator;
- if(allocator == nullptr) return;
-
+ if (allocator == nullptr) return;
+
slice_deallocate(buffer->allocator, &buffer->file);
slice_deallocate(buffer->allocator, &buffer->content);