about summary refs log tree commit diff stats
path: root/src/bindings/termcap.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-01-15 21:00:42 +0100
committerbptato <nincsnevem662@gmail.com>2024-01-15 21:01:53 +0100
commitf637588d76627368bf7d82f6aa8f5596fe53bddf (patch)
tree58999b3d70cbdb038e17e06607b58d391787dca1 /src/bindings/termcap.nim
parent1915e6d25e45a135caaacec1bc9a603510adfad1 (diff)
downloadchawan-f637588d76627368bf7d82f6aa8f5596fe53bddf.tar.gz
quickjs: patch libunicode.h to use LRE_BOOL
This way it actually compiles :)

(QJS includes cutils.h too, so BOOL works there. We don't, so this is
the easiest fix.)
Diffstat (limited to 'src/bindings/termcap.nim')
0 files changed, 0 insertions, 0 deletions
n112' href='#n112'>112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
# Wrappers around file system primitives that take a 'resources' object and
# are thus easier to test.

container resources [
  lock:bool
  data:&:@:resource
]

container resource [
  name:text
  contents:text
]

def start-reading resources:&:resources, filename:text -> contents:&:source:char [
  local-scope
  load-ingredients
  {
    break-unless resources
    # fake file system
    contents <- start-reading-from-fake-resources resources, filename
    return
  }
  # real file system
  file:num <- $open-file-for-reading filename
  assert file, [file not found]
  contents:&:source:char, sink:&:sink:char <- new-channel 30
  start-running receive-from-file file, sink
]

def start-reading-from-fake-resources resources:&:resources, resource:text -> contents:&:source:char [
  local-scope
  load-ingredients
  i:num <- copy 0
  data:&:@:resource <- get *resources, data:offset
  len:num <- length *data
  {
    done?:bool <- greater-or-equal i, len
    break-if done?
    tmp:resource <- index *data, i
    i <- add i, 1
    curr-resource:text <- get tmp, name:offset
    found?:bool <- equal resource, curr-resource
    loop-unless found?
    contents:&:source:char, sink:&:sink:char <- new-channel 30
    curr-contents:text <- get tmp, contents:offset
    start-running receive-from-text curr-contents, sink
    return
  }
  return 0/not-found
]

def receive-from-file file:num, sink:&:sink:char -> sink:&:sink:char [
  local-scope
  load-ingredients
  {
    c:char, eof?:bool <- $read-from-file file
    break-if eof?
    sink <- write sink, c
    loop
  }
  sink <- close sink
  file <- $close-file file
]

def receive-from-text contents:text, sink:&:sink:char -> sink:&:sink:char [
  local-scope
  load-ingredients
  i:num <- copy 0
  len:num <- length *contents
  {
    done?:bool <- greater-or-equal i, len
    break-if done?
    c:char <- index *contents, i
    sink <- write sink, c
    i <- add i, 1
    loop
  }
  sink <- close sink
]

def start-writing resources:&:resources, filename:text -> sink:&:sink:char, routine-id:num [
  local-scope
  load-ingredients
  source:&:source:char, sink:&:sink:char <- new-channel 30
  {
    break-unless resources
    # fake file system
    routine-id <- start-running transmit-to-fake-file resources, filename, source
    reply
  }
  # real file system
  file:num <- $open-file-for-writing filename
  assert file, [no such file]
  routine-id <- start-running transmit-to-file file, source
]

def transmit-to-file file:num, source:&:source:char -> source:&:source:char [
  local-scope
  load-ingredients
  {
    c:char, done?:bool, source <- read source
    break-if done?
    $write-to-file file, c
    loop
  }
  file <- $close-file file
]

def transmit-to-fake-file resources:&:resources, filename:text, source:&:source:char -> resources:&:resources, source:&:source:char [
  local-scope
  load-ingredients
  lock:location <- get-location *resources, lock:offset
  wait-for-reset-then-set lock
  # compute new file contents
  buf:&:buffer <- new-buffer 30
  {
    c:char, done?:bool, source <- read source
    break-if done?
    buf <- append buf, c
    loop
  }
  contents:text <- buffer-to-array buf
  new-resource:resource <- merge filename, contents
  # write to resources
  curr-filename:text <- copy 0
  data:&:@:resource <- get *resources, data:offset
  # replace file contents if it already exists
  i:num <- copy 0
  len:num <- length *data
  {
    done?:bool <- greater-or-equal i, len
    break-if done?
    tmp:resource <- index *data, i
    curr-filename <- get tmp, name:offset
    found?:bool <- equal filename, curr-filename
    loop-unless found?
    put-index *data, i, new-resource
    reset lock
    reply
  }
  # if file didn't already exist, make room for it
  new-len:num <- add len, 1
  new-data:&:@:resource <- new resource:type, new-len
  put *resources, data:offset, new-data
  # copy over old files
  i:num <- copy 0
  {
    done?:bool <- greater-or-equal i, len
    break-if done?
    tmp:resource <- index *data, i
    put-index *new-data, i, tmp
  }
  # write new file
  put-index *new-data, len, new-resource
  reset lock
]