{"id":229,"date":"2024-10-22T06:34:00","date_gmt":"2024-10-22T13:34:00","guid":{"rendered":"https:\/\/flenniken.net\/blog\/?p=229"},"modified":"2024-10-23T07:30:38","modified_gmt":"2024-10-23T14:30:38","slug":"line-reader","status":"publish","type":"post","link":"https:\/\/flenniken.net\/blog\/line-reader\/","title":{"rendered":"Line Reader"},"content":{"rendered":"\n<p>For statictea I wrote a module to read lines from a file because nim\u2019s high level functions do not support statictea\u2019s requirements.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Nim\u2019s builtin line reader (lines) does not return line endings and it doesn\u2019t care about line lengths. <\/p>\n\n\n\n<p>Statictea requires that:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>it preserves template line endings<\/li>\n\n\n\n<li>it uses a small fixed amount of memory<\/li>\n\n\n\n<li>its commands have a line limit<\/li>\n\n\n\n<li>it reports the template name and current line number for warnings<\/li>\n\n\n\n<li>it processes a template sequentially in one pass.<\/li>\n<\/ul>\n\n\n\n<p>The reader is implemented in the linebuffer module. It consists of the LineBuffer object with the readline method. <\/p>\n\n\n\n<p>The LineBuffer object holds the following information. <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>the stream<\/li>\n\n\n\n<li>the current position in the stream<\/li>\n\n\n\n<li>a fixed size buffer for lines<\/li>\n\n\n\n<li>the current line number<\/li>\n\n\n\n<li>the name of the stream<\/li>\n<\/ul>\n\n\n\n<p>The readline method returns the next line from the stream. A line is returned when the line ending is found (lf or crlf), when the stream runs out of bytes or when the maximum line length is reached. When no more data exists in the stream, an empty string is returned.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>You can see the source here:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/flenniken\/statictea\/blob\/master\/src\/linebuffer.nim\">linebuffer.nim<\/a><\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>To read lines you create a line buffer then you call its readline method, see the example below.  There might not be enough memory for the buffer, so you need to check that it got created. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>let lbO = newLineBuffer(templateStream,<\/code>  filename=templateFilename)\nif not <span style=\"font-family: inherit;\">lbO<\/span>.isSome():\n  # Not enough memory for the line buffer.\n  return\nvar lb = <span style=\"font-family: inherit;\">lbO<\/span>.get()\nwhile line = lb.readLine():\n  processLine(line)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Testing <\/h2>\n\n\n\n<p>To make testing easier the unit tests lower the line length and buffer length when it creates a LineBuffer.  <\/p>\n\n\n\n<p>You can see the tests here:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/flenniken\/statictea\/blob\/master\/tests\/test_linebuffer.nim\">test_linebuffer.nim<\/a><\/li>\n<\/ul>\n\n\n\n<p>The line buffer module doesn\u2019t have any dependencies so you could copy it into your nim project  if you have similar requirements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For statictea I wrote a module to read lines from a file because nim\u2019s high level functions do not support statictea\u2019s requirements. Nim\u2019s builtin line reader (lines) does not return line endings and it doesn\u2019t care about line lengths. Statictea &hellip; <a href=\"https:\/\/flenniken.net\/blog\/line-reader\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[27,28],"class_list":["post-229","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-nim","tag-stat"],"_links":{"self":[{"href":"https:\/\/flenniken.net\/blog\/wp-json\/wp\/v2\/posts\/229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/flenniken.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/flenniken.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/flenniken.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/flenniken.net\/blog\/wp-json\/wp\/v2\/comments?post=229"}],"version-history":[{"count":12,"href":"https:\/\/flenniken.net\/blog\/wp-json\/wp\/v2\/posts\/229\/revisions"}],"predecessor-version":[{"id":259,"href":"https:\/\/flenniken.net\/blog\/wp-json\/wp\/v2\/posts\/229\/revisions\/259"}],"wp:attachment":[{"href":"https:\/\/flenniken.net\/blog\/wp-json\/wp\/v2\/media?parent=229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/flenniken.net\/blog\/wp-json\/wp\/v2\/categories?post=229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/flenniken.net\/blog\/wp-json\/wp\/v2\/tags?post=229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}