summaryrefslogtreecommitdiff
path: root/src/source.cc
blob: 69f1941f60016630a8bb41a720160ebe6972d3f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef SOURCE_CC
#define SOURCE_CC

#include "array.cc"
#include "common.cc"
#include "memory.cc"

typedef u32 Source_Id;

struct Span {
  Source_Id id;
  usize start, end;

  Span(Source_Id id, usize start, usize end) : id(id), start(start), end(end) {}
};

struct Buffer {
  String file;
  String content;
  Link link;
  const Allocator* allocator;
};

bool buffer_init(Buffer* buffer, const Allocator* allocator,
                 const String* content_in, const String* file_in) {
  assert_neq(buffer, nullptr);
  assert_neq(allocator, nullptr);
  assert_neq(content_in, nullptr);
  assert_neq(file_in, nullptr);

  String content, file;
  bool ret = slice_copy(allocator, content_in, &content);
  if (unlikely(!ret)) return false;

  ret = slice_copy(allocator, file_in, &file);
  if (unlikely(!ret)) {
    slice_deallocate(allocator, &content);
    return false;
  }

  buffer->file = file;
  buffer->content = content;
  buffer->allocator = allocator;
  buffer->link = {};
  return true;
}

void buffer_deinit(Buffer* buffer) {
  assert_neq(buffer, nullptr);

  slice_deallocate(buffer->allocator, &buffer->file);
  slice_deallocate(buffer->allocator, &buffer->content);

  memset(buffer, 0, sizeof(*buffer));
}

struct Buffer_Manager {
  Link* stack;
  Array<Buffer> buffers;
};

#define buffer_manager_init(allocator) \
  Buffer_Manager { nullptr, array_init(Buffer, allocator) }

bool buffer_manager_push(Buffer_Manager* manager, Buffer* b) {
  assert_neq(manager, nullptr);
  assert_neq(b, nullptr);

  bool ret = array_push(&manager->buffers, b);
  if(!ret) return false;

  b = array_last(&manager->buffers);
  if(manager->stack == nullptr) {
    manager->stack = &b->link;
    return true;
  }

  link_after(manager->stack, &b->link);
  return true;
}

void buffer_manager_deinit(Buffer_Manager* manager) {
  assert_neq(manager, nullptr);
  array_deinit(&manager->buffers);
}

#endif