monotone

monotone Mtn Source Tree

Root/luaext_guess_binary.cc

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

Archive Download this file

Branches

Tags

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