monotone

monotone Mtn Source Tree

Root/luaext_guess_binary.cc

1
2#include "lua.hh"
3
4#include <fstream>
5
6#include "file_io.hh"
7
8using std::ifstream;
9using std::ios_base;
10using std::string;
11
12LUAEXT(guess_binary_file_contents, )
13{
14 const char *path = luaL_checkstring(L, 1);
15
16 ifstream file(path, ios_base::binary);
17 if (!file)
18 {
19 lua_pushnil(L);
20 return 1;
21 }
22 const int bufsize = 8192;
23 char tmpbuf[bufsize];
24 string buf;
25 while (file.read(tmpbuf, sizeof tmpbuf))
26 {
27 I(file.gcount() <= static_cast<int>(sizeof tmpbuf));
28 buf.assign(tmpbuf, file.gcount());
29 if (guess_binary(buf))
30 {
31 lua_pushboolean(L, true);
32 return 1;
33 }
34 }
35 lua_pushboolean(L, false);
36 return 1;
37}
38
39
40// Local Variables:
41// mode: C++
42// fill-column: 76
43// c-file-style: "gnu"
44// indent-tabs-mode: nil
45// End:
46// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
47

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status