{"action":"opened","issue":{"url":"https://api.github.com/repos/Corsaair/redtamarin/issues/11","labels_url":"https://api.github.com/repos/Corsaair/redtamarin/issues/11/labels{/name}","comments_url":"https://api.github.com/repos/Corsaair/redtamarin/issues/11/comments","events_url":"https://api.github.com/repos/Corsaair/redtamarin/issues/11/events","html_url":"https://github.com/Corsaair/redtamarin/issues/11","id":104656579,"number":11,"title":"add function setbuf, setvbuf to C.stdio","user":{"login":"zwetan","id":236154,"avatar_url":"https://avatars.githubusercontent.com/u/236154?v=3","gravatar_id":"","url":"https://api.github.com/users/zwetan","html_url":"https://github.com/zwetan","followers_url":"https://api.github.com/users/zwetan/followers","following_url":"https://api.github.com/users/zwetan/following{/other_user}","gists_url":"https://api.github.com/users/zwetan/gists{/gist_id}","starred_url":"https://api.github.com/users/zwetan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zwetan/subscriptions","organizations_url":"https://api.github.com/users/zwetan/orgs","repos_url":"https://api.github.com/users/zwetan/repos","events_url":"https://api.github.com/users/zwetan/events{/privacy}","received_events_url":"https://api.github.com/users/zwetan/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-09-03T09:38:54Z","updated_at":"2015-09-03T09:38:54Z","closed_at":null,"body":"see: http://pubs.opengroup.org/onlinepubs/9699919799/functions/setvbuf.html\r\n\r\nC signature:\r\nsetvbuf - assign buffering to a stream\r\n`int setvbuf(FILE *restrict stream, char *restrict buf, int type, size_t size);`\r\n\r\nsetbuf - assign buffering to a stream\r\n`void setbuf(FILE *restrict stream, char *restrict buf);`\r\n\r\nAS3 signature:\r\n`function setvbuf( stream:FILE, type:int, size:uint = 0 ):int;`\r\n`function setbuf( stream:FILE, type:int = -1 ):void;`\r\n\r\n> If buf is not a null pointer, the array it points to may be used instead of\r\n> a buffer allocated by setvbuf() and the argument size specifies the size of the array;\r\n\r\nwe don't want to give access to a buffer `char *restrict buf`\r\nas we prefer to let `setvbuf()` allocate its own buffer.\r\n\r\nWe still want to be able to change the size of the buffer,\r\n\r\nif `size` is not defined, we will use `BUFSIZ ` by default.\r\nFor ex under Mac OS X `BUFSIZ = 1024`\r\nand under Linux Ubuntu `BUFSIZ = 8192`\r\n\r\nIf we know in advance well need to write more than `BUFSIZ `,\r\nthen we can use a bigger buffer eg. `setvbuf( fp, _IOLBF, 4096 )`.\r\n\r\n`setbuf` will be implemented in AS3\r\n> the function call: `setbuf(stream, buf)`\r\n> shall be equivalent to: `setvbuf(stream, buf, _IOFBF, BUFSIZ)`\r\n> if buf is not a null pointer,\r\n> or to: `setvbuf(stream, buf, _IONBF, BUFSIZ)`\r\n> if buf is a null pointer.\r\n\r\n(by default a stream is non-buffered)\r\n\r\neg.\r\n```as3\r\npublic function setbuf( stream:FILE, type:int = -1 ):void\r\n{\r\n    if( type < 0 )\r\n    {\r\n        type = _IONBF; //this is by default\r\n    }\r\n    setvbuf( stream, type, BUFSIZ );\r\n}\r\n```\r\n\r\n\r\n\r\n\r\nreason:\r\nwe need to be able to change the buffering behaviour\r\n\r\nuse case:\r\nconcurrency when writing logs\r\n```as3\r\nvar bytes:ByteArray = new ByteArray();\r\n    bytes.writeUTFBytes( \"2015-09-02 some log line\\n\" );\r\n    bytes.position = 0;\r\n\r\n// we append to the file, eg. write at the end\r\nvar fp:FILE = fopen( \"somefile\", \"a\" );\r\n\r\n/* Here we force line buffering with a buffer of 4KB (4096 bytes)\r\n   it will \"reserve\" 4KB of space to write to the file.\r\n\r\n   Because fwrite() lock the file during a write,\r\n   because appending to the file stream (O_APPEND) move the offset to the end of the file,\r\n   and because we use a buffer (for each lines)\r\n   we can achieve concurrency.\r\n  \r\n   so even if another process write at the same\r\n   time to the log file the 2x concurrent writes will note mangle with each other\r\n\r\n   with buffering:\r\n   1: \"Here's process #1\"\r\n   2: \"Here's process #2\"\r\n\r\n   without buffering:\r\n   0: \"Hehere's process #2re's process #1\"\r\n*/\r\nvar buffered:int = setvbuf( fp, _IOLBF, 4096 );\r\n\r\nvar wrote:int = fwrite( bytes, bytes.length, fp );\r\n```\r\n"}}