aboutsummaryrefslogtreecommitdiff
path: root/src/lua/src/linit.c
blob: c1f90dfab7156ce1e120d95b79e356395a8c113b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
** $Id: linit.c,v 1.14.1.1 2007/12/27 13:02:25 roberto Exp $
** Initialization of libraries for lua.c
** See Copyright Notice in lua.h
*/


#define linit_c
#define LUA_LIB

#include "lua.h"

#include "lualib.h"
#include "lauxlib.h"


static const luaL_Reg lualibs[] = {
  {"", luaopen_base},
  {LUA_LOADLIBNAME, luaopen_package},
  {LUA_TABLIBNAME, luaopen_table},
  {LUA_IOLIBNAME, luaopen_io},
  {LUA_OSLIBNAME, luaopen_os},
  {LUA_STRLIBNAME, luaopen_string},
  {LUA_MATHLIBNAME, luaopen_math},
  {LUA_DBLIBNAME, luaopen_debug},
  {NULL, NULL}
};


LUALIB_API void luaL_openlibs (lua_State *L) {
  const luaL_Reg *lib = lualibs;
  for (; lib->func; lib++) {
    lua_pushcfunction(L, lib->func);
    lua_pushstring(L, lib->name);
    lua_call(L, 1, 0);
  }
}

ublic License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "filecache.h" #include "clientserver.h" #include "log.h" #include "filesys.h" #include "hex.h" #include "sha1.h" #include <string> #include <iostream> #include <fstream> #include <sstream> #include <stdlib.h> bool FileCache::loadByPath(const std::string &path, std::ostream &os) { std::ifstream fis(path.c_str(), std::ios_base::binary); if(!fis.good()){ verbosestream<<"FileCache: File not found in cache: " <<path<<std::endl; return false; } bool bad = false; for(;;){ char buf[1024]; fis.read(buf, 1024); std::streamsize len = fis.gcount(); os.write(buf, len); if(fis.eof()) break; if(!fis.good()){ bad = true; break; } } if(bad){ errorstream<<"FileCache: Failed to read file from cache: \"" <<path<<"\""<<std::endl; } return !bad; } bool FileCache::updateByPath(const std::string &path, const std::string &data) { std::ofstream file(path.c_str(), std::ios_base::binary | std::ios_base::trunc); if(!file.good()) { errorstream<<"FileCache: Can't write to file at " <<path<<std::endl; return false; } file.write(data.c_str(), data.length()); file.close(); return !file.fail(); } bool FileCache::update(const std::string &name, const std::string &data) { std::string path = m_dir + DIR_DELIM + name; return updateByPath(path, data); } bool FileCache::update_sha1(const std::string &data) { SHA1 sha1; sha1.addBytes(data.c_str(), data.size()); unsigned char *digest = sha1.getDigest(); std::string sha1_raw((char*)digest, 20); free(digest); std::string sha1_hex = hex_encode(sha1_raw); return update(sha1_hex, data); } bool FileCache::load(const std::string &name, std::ostream &os) { std::string path = m_dir + DIR_DELIM + name; return loadByPath(path, os); } bool FileCache::load_sha1(const std::string &sha1_raw, std::ostream &os) { std::ostringstream tmp_os(std::ios_base::binary); if(!load(hex_encode(sha1_raw), tmp_os)) return false; SHA1 sha1; sha1.addBytes(tmp_os.str().c_str(), tmp_os.str().length()); unsigned char *digest = sha1.getDigest(); std::string sha1_real_raw((char*)digest, 20); free(digest); if(sha1_real_raw != sha1_raw){ verbosestream<<"FileCache["<<m_dir<<"]: filename "<<sha1_real_raw <<" mismatches actual checksum"<<std::endl; return false; } os<<tmp_os.str(); return true; }