monotone

monotone Mtn Source Tree

Root/src/luaext_guess_binary.cc

1// Copyright (C) 2006 Timothy Brownawell <tbrownaw@gmail.com>
2//
3// This program is made available under the GNU GPL version 2.0 or
4// greater. See the accompanying file COPYING for details.
5//
6// This program is distributed WITHOUT ANY WARRANTY; without even the
7// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8// PURPOSE.
9
10#include "base.hh"
11#include "lua.hh"
12
13#include <fstream>
14
15#include "file_io.hh"
16
17using std::ifstream;
18using std::ios_base;
19using std::string;
20
21LUAEXT(guess_binary_file_contents, )
22{
23 const char *path = luaL_checkstring(LS, 1);
24
25 ifstream file(path, ios_base::binary);
26 if (!file)
27 {
28 lua_pushnil(LS);
29 return 1;
30 }
31 const int bufsize = 8192;
32 char tmpbuf[bufsize];
33 string buf;
34 while (file.read(tmpbuf, sizeof tmpbuf))
35 {
36 I(file.gcount() <= static_cast<int>(sizeof tmpbuf));
37 buf.assign(tmpbuf, file.gcount());
38 if (guess_binary(buf))
39 {
40 lua_pushboolean(LS, true);
41 return 1;
42 }
43 }
44 lua_pushboolean(LS, false);
45 return 1;
46}
47
48// Local Variables:
49// mode: C++
50// fill-column: 76
51// c-file-style: "gnu"
52// indent-tabs-mode: nil
53// End:
54// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:

Archive Download this file

Branches

Tags

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