| <!DOCTYPE html><html><head><meta charset="utf-8" /><meta content="IE=edge" http-equiv="X-UA-Compatible" /><meta content="width=device-width, initial-scale=1, maximum-scale=2.0, user-scalable=yes, minimal-ui=yes" name="viewport" /><title>facil.io - The FIOBJ Data Stream Type</title><meta content="facil.io - The FIOBJ Data Stream Type" name="description" /><link href="https://fonts.googleapis.com/css?family=Montserrat|Quicksand|Karla" rel="stylesheet" type="text/css" /><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script><link href="/assets/styles/main.css" rel="stylesheet" type="text/css" /><script type="application/ld+json">{"@context":"http://schema.org","@type":"WebSite","url":"http://facil.io","name":"facil.io","description":"facil.io - a light web application framework in C, with support for HTTP, WebSockets and Pub/Sub out of the box.","keywords":"C, web, framework, websockets, websocket, realtime, real-time, easy","image":"http://facil.io/website/logo/facil-io.svg","author":[{"@type":"Person","name":"Bo (Myst)","url":"http://stackoverflow.com/users/4025095/myst","email":"bo(at)facil.io"}],"sourceOrganization":{"@context":"http://schema.org","@type":"Organization","name":"Plezi","url":"http://facil.io","description":"facil.io - a light web application framework in C, with support for HTTP, WebSockets and Pub/Sub out of the box.","logo":"http://facil.io/website/logo/facil-io.svg","image":"http://facil.io/website/logo/facil-io.svg","email":"bo(at)facil.io","member":[{"@type":"Person","name":"Bo (Myst)","url":"http://stackoverflow.com/users/4025095/myst","email":"bo(at)facil.io"}]}}</script><meta content="facil.io" name="apple-mobile-web-app-title" /><meta content="facil.io - the C Web Application Framework" name="application-name" /><meta content="#b91d47" name="msapplication-TileColor" /><meta content="/mstile-144x144.png" name="msapplication-TileImage" /><meta content="#ffffff" name="theme-color" /></head><body><a href="/" id="logo"></a><input id="show_nav" type="checkbox" /><nav id="top_nav"><ul><li><a href="/0.7.x/index">Latest Docs</a></li><li><a href="https://github.com/boazsegev/facil.io" target="_blank">Source Code</a></li><li><a href="javascript: change_themes();" id="theme">Night Theme</a></li></ul></nav><input id="show_sidebar" type="checkbox" /><nav id="side_bar"><h2 id="version-0-7-x"><a href="/0.7.x/index">Version 0.7.x</a></h2> |
| |
| <h3 id="core-library"><a href="/0.7.x/fio">Core Library</a></h3> |
| |
| <ul> |
| <li><a href="/0.7.x/fio#connection-protocol-management">Protocol Management</a></li> |
| <li><a href="/0.7.x/fio#running-facil-io">Running the IO reactor</a></li> |
| <li><a href="/0.7.x/fio#socket-connection-functions">Connection Functions</a></li> |
| <li><a href="/0.7.x/fio#event-task-scheduling">Event / Task Scheduling</a></li> |
| <li><a href="/0.7.x/fio#pub-sub-services">Pub/Sub Services</a></li> |
| <li><a href="/0.7.x/fio#the-custom-memory-allocator">Memory Allocation</a></li> |
| <li><a href="/0.7.x/fio#general-helpers">General Helpers</a></li> |
| <li><a href="/0.7.x/fio#linked-lists">Linked Lists</a></li> |
| <li><a href="/0.7.x/fio#string-helpers">String Helpers</a></li> |
| <li><a href="/0.7.x/fio#hash-maps-sets">Hash Maps / Sets</a></li> |
| <li><a href="/0.7.x/fio#version-and-compilation-related-macros">Compilation Macros</a></li> |
| <li><a href="/0.7.x/fio#weak-functions">Weak Functions</a></li> |
| </ul> |
| |
| <h3 id="extensions"><a href="/0.7.x/extensions">Extensions</a></h3> |
| |
| <ul> |
| <li><a href="/0.7.x/http">HTTP / WebSockets</a></li> |
| <li><a href="/0.7.x/redis">Redis (client)</a></li> |
| <li><a href="/0.7.x/fio_cli">CLI (command line)</a></li> |
| </ul> |
| |
| <h3 id="the-fiobj-types"><a href="/0.7.x/fiobj">The FIOBJ types</a></h3> |
| |
| <ul> |
| <li><a href="/0.7.x/fiobj_core">Core Functions</a></li> |
| <li><a href="/0.7.x/fiobj_primitives">Primitives</a></li> |
| <li><a href="/0.7.x/fiobj_numbers">Numbers</a></li> |
| <li><a href="/0.7.x/fiobj_str">Strings</a></li> |
| <li><a href="/0.7.x/fiobj_ary">Array</a></li> |
| <li><a href="/0.7.x/fiobj_hash">HashMap</a></li> |
| <li><a href="/0.7.x/fiobj_data">Data Streams</a></li> |
| <li><a href="/0.7.x/fiobj_json">JSON</a></li> |
| </ul> |
| </nav><div id="md_container"><div class='toc'><ul> |
| <li> |
| <a href="#facil-io-the-fiobj-data-stream-type">facil.io - The FIOBJ Data Stream Type</a> |
| <ul> |
| <li> |
| <ul> |
| <li> |
| <a href="#creating-the-data-stream-object">Creating the Data Stream object</a> |
| <ul> |
| <li> |
| <a href="#fiobj_data_newstr"><code>fiobj_data_newstr</code></a> |
| </li> |
| <li> |
| <a href="#fiobj_data_newstr2"><code>fiobj_data_newstr2</code></a> |
| </li> |
| <li> |
| <a href="#fiobj_data_newtmpfile"><code>fiobj_data_newtmpfile</code></a> |
| </li> |
| <li> |
| <a href="#fiobj_data_newfd"><code>fiobj_data_newfd</code></a> |
| </li> |
| <li> |
| <a href="#fiobj_data_slice"><code>fiobj_data_slice</code></a> |
| </li> |
| </ul> |
| </li> |
| <li> |
| <a href="#saving-the-data-stream-object">Saving the Data Stream object</a> |
| <ul> |
| <li> |
| <a href="#fiobj_data_save"><code>fiobj_data_save</code></a> |
| </li> |
| </ul> |
| </li> |
| <li> |
| <a href="#reading-data-from-the-stream">Reading Data From The Stream</a> |
| <ul> |
| <li> |
| <a href="#fiobj_data_read"><code>fiobj_data_read</code></a> |
| </li> |
| <li> |
| <a href="#fiobj_data_read2ch"><code>fiobj_data_read2ch</code></a> |
| </li> |
| <li> |
| <a href="#part-597705">``</a> |
| </li> |
| <li> |
| <a href="#fiobj_data_pos"><code>fiobj_data_pos</code></a> |
| </li> |
| <li> |
| <a href="#fiobj_data_len"><code>fiobj_data_len</code></a> |
| </li> |
| <li> |
| <a href="#fiobj_data_seek"><code>fiobj_data_seek</code></a> |
| </li> |
| <li> |
| <a href="#fiobj_data_pread"><code>fiobj_data_pread</code></a> |
| </li> |
| </ul> |
| </li> |
| <li> |
| <a href="#writing-to-the-data-stream">Writing To The Data Stream</a> |
| <ul> |
| <li> |
| <a href="#fiobj_data_write"><code>fiobj_data_write</code></a> |
| </li> |
| <li> |
| <a href="#fiobj_data_puts"><code>fiobj_data_puts</code></a> |
| </li> |
| <li> |
| <a href="#fiobj_data_assert_dynamic"><code>fiobj_data_assert_dynamic</code></a> |
| </li> |
| </ul> |
| </li> |
| </ul> |
| </li> |
| </ul> |
| </li> |
| </ul> |
| </div><h1 id="facil-io-the-fiobj-data-stream-type">facil.io - The FIOBJ Data Stream Type</h1> |
| |
| <h3 id="creating-the-data-stream-object">Creating the Data Stream object</h3> |
| |
| <h4 id="fiobj_data_newstr"><code>fiobj_data_newstr</code></h4> |
| <pre><code class='highlight'><span class="n">FIOBJ</span> <span class="n">fiobj_data_newstr</span><span class="p">(</span><span class="kt">void</span><span class="p">);</span> |
| </code></pre> |
| <p>Creates a new local in-memory Data Stream object.</p> |
| |
| <h4 id="fiobj_data_newstr2"><code>fiobj_data_newstr2</code></h4> |
| <pre><code class='highlight'><span class="n">FIOBJ</span> <span class="n">fiobj_data_newstr2</span><span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">buffer</span><span class="p">,</span> <span class="kt">uintptr_t</span> <span class="n">length</span><span class="p">,</span> |
| <span class="kt">void</span> <span class="p">(</span><span class="o">*</span><span class="n">dealloc</span><span class="p">)(</span><span class="kt">void</span> <span class="o">*</span><span class="p">));</span> |
| </code></pre> |
| <p>Creates a Data object from an existing buffer.</p> |
| |
| <p>The buffer will be deallocated using the provided <code>dealloc</code> function pointer.</p> |
| |
| <p>Use a NULL <code>dealloc</code> function pointer if the buffer is static and shouldn't be freed.</p> |
| |
| <h4 id="fiobj_data_newtmpfile"><code>fiobj_data_newtmpfile</code></h4> |
| <pre><code class='highlight'><span class="n">FIOBJ</span> <span class="n">fiobj_data_newtmpfile</span><span class="p">(</span><span class="kt">void</span><span class="p">);</span> |
| </code></pre> |
| <p>Creates a new local tempfile Data Stream object.</p> |
| |
| <h4 id="fiobj_data_newfd"><code>fiobj_data_newfd</code></h4> |
| <pre><code class='highlight'><span class="n">FIOBJ</span> <span class="n">fiobj_data_newfd</span><span class="p">(</span><span class="kt">int</span> <span class="n">fd</span><span class="p">);</span> |
| </code></pre> |
| <p>Creates a new local file Data Stream object.</p> |
| |
| <h4 id="fiobj_data_slice"><code>fiobj_data_slice</code></h4> |
| <pre><code class='highlight'><span class="n">FIOBJ</span> <span class="n">fiobj_data_slice</span><span class="p">(</span><span class="n">FIOBJ</span> <span class="n">parent</span><span class="p">,</span> <span class="kt">intptr_t</span> <span class="n">offset</span><span class="p">,</span> <span class="kt">uintptr_t</span> <span class="n">length</span><span class="p">);</span> |
| </code></pre> |
| <p>Creates a slice from an existing Data Stream object.</p> |
| |
| <h3 id="saving-the-data-stream-object">Saving the Data Stream object</h3> |
| |
| <h4 id="fiobj_data_save"><code>fiobj_data_save</code></h4> |
| <pre><code class='highlight'><span class="kt">int</span> <span class="n">fiobj_data_save</span><span class="p">(</span><span class="n">FIOBJ</span> <span class="n">io</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">filename</span><span class="p">);</span> |
| </code></pre> |
| <p>Creates a new local file Data Stream object.</p> |
| |
| <h3 id="reading-data-from-the-stream">Reading Data From The Stream</h3> |
| |
| <h4 id="fiobj_data_read"><code>fiobj_data_read</code></h4> |
| <pre><code class='highlight'><span class="n">fio_str_info_s</span> <span class="n">fiobj_data_read</span><span class="p">(</span><span class="n">FIOBJ</span> <span class="n">io</span><span class="p">,</span> <span class="kt">intptr_t</span> <span class="n">length</span><span class="p">);</span> |
| </code></pre> |
| <p>Reads up to <code>length</code> bytes and returns a temporary(!) buffer object (not NUL terminated).</p> |
| |
| <p>If <code>length</code> is zero or negative, it will be computed from the end of the input backwards (0 == EOF).</p> |
| |
| <p>The C string object will be invalidate the next time a function call to the Data Stream object is made.</p> |
| |
| <h4 id="fiobj_data_read2ch"><code>fiobj_data_read2ch</code></h4> |
| <pre><code class='highlight'><span class="n">fio_str_info_s</span> <span class="n">fiobj_data_read2ch</span><span class="p">(</span><span class="n">FIOBJ</span> <span class="n">io</span><span class="p">,</span> <span class="kt">uint8_t</span> <span class="n">token</span><span class="p">);</span> |
| </code></pre> |
| <p>Reads until the <code>token</code> byte is encountered or until the end of the stream.</p> |
| |
| <p>Returns a temporary(!) C string including the end of line marker.</p> |
| |
| <p>Careful when using this call on large file streams, as the whole file stream might be loaded into the memory.</p> |
| |
| <p>The C string object will be invalidate the next time a function call to the Data Stream object is made.</p> |
| |
| <h4 id="part-597705">``</h4> |
| <pre><code class='highlight'><span class="cp">#define fiobj_data_gets(io) fiobj_data_read2ch((io), '\n'); |
| </span></code></pre> |
| <p>Reads a line (until the '\n' byte is encountered) or until the end of the available data.</p> |
| |
| <p>Returns a temporary(!) buffer object (not NUL terminated) including the end of line marker.</p> |
| |
| <p>Careful when using this call on large file streams, as the whole file stream might be loaded into the memory.</p> |
| |
| <p>The C string object will be invalidate the next time a function call to the Data Stream object is made.</p> |
| |
| <h4 id="fiobj_data_pos"><code>fiobj_data_pos</code></h4> |
| <pre><code class='highlight'><span class="kt">intptr_t</span> <span class="n">fiobj_data_pos</span><span class="p">(</span><span class="n">FIOBJ</span> <span class="n">io</span><span class="p">);</span> |
| </code></pre> |
| <p>Returns the current reading position. Returns -1 on error.</p> |
| |
| <h4 id="fiobj_data_len"><code>fiobj_data_len</code></h4> |
| <pre><code class='highlight'><span class="kt">intptr_t</span> <span class="n">fiobj_data_len</span><span class="p">(</span><span class="n">FIOBJ</span> <span class="n">io</span><span class="p">);</span> |
| </code></pre> |
| <p>Returns the length of the stream.</p> |
| |
| <h4 id="fiobj_data_seek"><code>fiobj_data_seek</code></h4> |
| <pre><code class='highlight'><span class="kt">void</span> <span class="n">fiobj_data_seek</span><span class="p">(</span><span class="n">FIOBJ</span> <span class="n">io</span><span class="p">,</span> <span class="kt">intptr_t</span> <span class="n">position</span><span class="p">);</span> |
| </code></pre> |
| <p>Moves the reading position to the requested position.</p> |
| |
| <h4 id="fiobj_data_pread"><code>fiobj_data_pread</code></h4> |
| <pre><code class='highlight'><span class="n">fio_str_info_s</span> <span class="n">fiobj_data_pread</span><span class="p">(</span><span class="n">FIOBJ</span> <span class="n">io</span><span class="p">,</span> <span class="kt">intptr_t</span> <span class="n">start_at</span><span class="p">,</span> <span class="kt">uintptr_t</span> <span class="n">length</span><span class="p">);</span> |
| </code></pre> |
| <p>Reads up to <code>length</code> bytes starting at <code>start_at</code> position and returns a temporary(!) buffer object (not NUL terminated) string object. The reading position is ignored and unchanged.</p> |
| |
| <p>The C string object will be invalidate the next time a function call to the Data Stream object is made.</p> |
| |
| <h3 id="writing-to-the-data-stream">Writing To The Data Stream</h3> |
| |
| <h4 id="fiobj_data_write"><code>fiobj_data_write</code></h4> |
| <pre><code class='highlight'><span class="kt">intptr_t</span> <span class="n">fiobj_data_write</span><span class="p">(</span><span class="n">FIOBJ</span> <span class="n">io</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">buffer</span><span class="p">,</span> <span class="kt">uintptr_t</span> <span class="n">length</span><span class="p">);</span> |
| </code></pre> |
| <p>Writes <code>length</code> bytes at the end of the Data Stream stream, ignoring the reading position.</p> |
| |
| <p>Behaves and returns the same value as the system call <code>write</code>.</p> |
| |
| <h4 id="fiobj_data_puts"><code>fiobj_data_puts</code></h4> |
| <pre><code class='highlight'><span class="kt">intptr_t</span> <span class="n">fiobj_data_puts</span><span class="p">(</span><span class="n">FIOBJ</span> <span class="n">io</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">buffer</span><span class="p">,</span> <span class="kt">uintptr_t</span> <span class="n">length</span><span class="p">);</span> |
| </code></pre> |
| <p>Writes <code>length</code> bytes at the end of the Data Stream stream, ignoring the reading position, adding an EOL marker ("\r\n") to the end of the stream.</p> |
| |
| <p>Behaves and returns the same value as the system call <code>write</code>.</p> |
| |
| <h4 id="fiobj_data_assert_dynamic"><code>fiobj_data_assert_dynamic</code></h4> |
| <pre><code class='highlight'><span class="kt">void</span> <span class="n">fiobj_data_assert_dynamic</span><span class="p">(</span><span class="n">FIOBJ</span> <span class="n">io</span><span class="p">);</span> |
| </code></pre> |
| <p>Makes sure the Data Stream object isn't attached to a static or external string.</p> |
| |
| <p>If the Data Stream object is attached to a static or external string, the data will be copied to a new memory block.</p> |
| |
| <p>If the Data Stream object is a slice from another Data Stream object, the data will be copied and the type of Data Stream object (memory vs. tmpfile) will be inherited.</p> |
| </div><a href="/" id="sign"></a><div class="hidden" id="notice"><a class="notice_close" onclick="hide_notice()">X</a><div id="notice_text"></div></div><script>function change_themes() { |
| if(localStorage.getItem("theme") == 'dark') { |
| localStorage.setItem("theme", "light"); |
| } else { |
| localStorage.setItem("theme", "dark"); |
| } |
| $('body')[0].className = localStorage.getItem("theme"); |
| set_theme_link(); |
| $('#show_nav').attr('checked', false); |
| return false; |
| }; |
| function sidebar_name() { return window.location.pathname.slice(0, window.location.pathname.lastIndexOf("/")); } |
| function on_sidebar_link(e) { |
| sessionStorage.setItem("sidebar.expect", sidebar_name()); |
| sessionStorage.setItem("sidebar.pos", document.getElementById("side_bar").scrollTop); |
| } |
| function load_sidebar_pos() { |
| var e = document.getElementById("side_bar"); |
| if(!e) { |
| console.warn("No sidebar detected"); |
| return; |
| } |
| var expect = sidebar_name(); |
| if(sessionStorage.getItem("sidebar.expect") == expect) { |
| e.scrollTo(0, parseInt(sessionStorage.getItem("sidebar.pos"))); |
| } else { |
| sessionStorage.setItem("sidebar.expect", false); |
| sessionStorage.setItem("sidebar.pos", 0); |
| } |
| if(e) { |
| // add link callbacks |
| var links = document.links; |
| var count = links.length; |
| for (var i = 0; i < count; i++) { |
| var href = links[i].href; |
| if(href.startsWith(document.location.origin)) { |
| href = href.slice(document.location.origin.length); |
| } |
| if(href.startsWith(expect)) { |
| /* add link event */ |
| links[i].addEventListener("click", on_sidebar_link); |
| } |
| } |
| } |
| |
| }; |
| load_sidebar_pos(); |
| function set_theme_link() { |
| $("#theme").html( ( (localStorage.getItem("theme") == 'dark') ? "Day" : "Night") + " Theme" ); |
| } |
| $('body')[0].className = (localStorage.getItem("theme") == 'dark') ? "dark" : "light"; |
| set_theme_link(); |
| function show_notice() { document.getElementById("notice").classList.remove('hidden'); }; |
| function hide_notice() { document.getElementById("notice").classList.add('hidden'); }; |
| $('#toc').on("touchstart", function (e) { return true; } ); |
| $('#toc').on("hover", function (e) { return true; } ); |
| // hljs.initHighlighting(); |
| // Google Analytics |
| // if(location.hostname != "localhost") { |
| // (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ |
| // (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), |
| // m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) |
| // })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); |
| |
| // ga('create', 'UA-69931401-1', 'auto'); |
| // ga('send', 'pageview'); |
| // }</script></body></html> |