示例#1
0
static const wchar_t *findfile (lua_State *L, const char *name,
                                              const char *pname) {
  const wchar_t *path;
  const wchar_t *wname;
  lua_pushstring(L, name);
  wname = utf8_to_utf16(L, -1, NULL);  /* `name' is encoded in UTF-8 */
  wname = LF_Gsub(L, wname, L".", L"\\");
  lua_getfield(L, LUA_ENVIRONINDEX, pname);
  if (lua_tostring(L, -1) == NULL)
    luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
  path = utf8_to_utf16(L, -1, NULL);
  if (path == NULL)
    luaL_error(L, LUA_QL("package.%s") " contains invalid UTF-8", pname);
  lua_pushliteral(L, "");  /* error accumulator */
  while ((path = pushnexttemplate(L, path)) != NULL) {
    const wchar_t *filename;
    filename = LF_Gsub(L, (const wchar_t*)lua_tostring(L, -1), LUA_PATH_MARK, wname);
    lua_remove(L, -2);  /* remove path template */
    if (readable(filename))  /* does file exist and is readable? */
      return filename;  /* return that file name */
    push_utf8_string(L, filename, -1);
    lua_pushfstring(L, "\n\tno file " LUA_QS, lua_tostring(L, -1));
    lua_remove(L, -2);  /* remove UTF-8 file name */
    lua_remove(L, -2);  /* remove UTF-16 file name */
    lua_concat(L, 2);  /* add entry to possible error message */
  }
  return NULL;  /* not found */
}
示例#2
0
static const char *searchpath (lua_State *L, const char *name,
			       const char *path, const char *sep,
			       const char *dirsep)
{
  luaL_Buffer msg;  /* to build error message */
  luaL_buffinit(L, &msg);
  if (*sep != '\0')  /* non-empty separator? */
    name = luaL_gsub(L, name, sep, dirsep);  /* replace it by 'dirsep' */
  while ((path = pushnexttemplate(L, path)) != NULL) {
    const char *filename = luaL_gsub(L, lua_tostring(L, -1),
				     LUA_PATH_MARK, name);
    lua_remove(L, -2);  /* remove path template */
    // @Voidious: Use lua_State->cwd in place of current dir.
    const char *absFilename = luaL_gsub(L, filename, "~", lua_getcwd(L));
    if (readable(absFilename)) { /* does file exist and is readable? */
      lua_pop(L, 1);
#if defined(_WIN32)
      const char *relativeFilename = luaL_gsub(L, filename, "~\\", "");
#else
      const char *relativeFilename = luaL_gsub(L, filename, "~/", "");
#endif
      lua_remove(L, -2);  /* remove file name */
      return relativeFilename;
    }
    lua_pop(L, 1);
    lua_pushfstring(L, "\n\tno file " LUA_QS, filename);
    lua_remove(L, -2);  /* remove file name */
    luaL_addvalue(&msg);  /* concatenate error msg. entry */
  }
  luaL_pushresult(&msg);  /* create error message */
  return NULL;  /* not found */
}
示例#3
0
static const wchar_t *searchpath (lua_State *L, const char *name,
                                                const wchar_t *path,
                                                const wchar_t *sep,
                                                const wchar_t *dirsep) {
  const wchar_t *wname;
  luaL_Buffer msg;  /* to build error message */
  luaL_buffinit(L, &msg);
  lua_pushstring(L, name);
  wname = utf8_to_utf16(L, -1, NULL);  /* `name' is encoded in UTF-8 */
  if (*sep != 0)  /* non-empty separator? */
    wname = LF_Gsub(L, wname, sep, dirsep);  /* replace it by 'dirsep' */
  while ((path = pushnexttemplate(L, path)) != NULL) {
    const wchar_t *filename = LF_Gsub(L, (const wchar_t*)lua_tostring(L, -1),
                                      LUA_PATH_MARK, wname);
    lua_remove(L, -2);  /* remove path template */
    if (readable(filename))  /* does file exist and is readable? */
      return filename;  /* return that file name */
    push_utf8_string(L, filename, -1);
    lua_pushfstring(L, "\n\tno file " LUA_QS, lua_tostring(L, -1));
    lua_remove(L, -2);  /* remove UTF-8 file name */
    lua_remove(L, -2);  /* remove UTF-16 file name */
    luaL_addvalue(&msg);  /* concatenate error msg. entry */
  }
  luaL_pushresult(&msg);  /* create error message */
  return NULL;  /* not found */
}
示例#4
0
文件: qtluautils.cpp 项目: elq/torch5
static const char *pushfilename (lua_State *L, const char *name) 
{
  const char *path;
  const char *filename;
  luaQ_getfield(L, LUA_GLOBALSINDEX, "package");
  luaQ_getfield(L, -1, "cpath");
  lua_remove(L, -2);
  if (! (path = lua_tostring(L, -1)))
    luaL_error(L, LUA_QL("package.cpath") " must be a string");
  lua_pushliteral(L, ""); 
  while ((path = pushnexttemplate(L, path))) {
    filename = luaL_gsub(L, lua_tostring(L, -1), "?", name);
    lua_remove(L, -2);
    if (readable(filename))
      { // stack:  cpath errmsg filename
        lua_remove(L, -3);
        lua_remove(L, -2);
        return lua_tostring(L, -1);
      }
    lua_pushfstring(L, "\n\tno file " LUA_QS, filename);
    lua_remove(L, -2); /* remove file name */
    lua_concat(L, 2);  /* add entry to possible error message */
  }
  lua_pushfstring(L, "module" LUA_QS "not found", name);
  lua_replace(L, -3);
  lua_concat(L, 2);
  lua_error(L);
  return 0;
}
示例#5
0
文件: xlua.cpp 项目: jaquadro/XLua
const char* x_findfile (lua_State* L, const char* name, const char* pname) {
	LPRDATA rdPtr = XLuaGlobal::Get().GetStateByState(L)->rdList.front()->rdPtr;

	const char* path;
	name = luaL_gsub(L, name, ".", LUA_DIRSEP);
	lua_getfield(L, LUA_ENVIRONINDEX, pname);
	path = lua_tostring(L, -1);
	if (path == NULL) {
		luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
	}
	lua_pushliteral(L, "");  /* error accumulator */
	while ((path = pushnexttemplate(L, path)) != NULL) {
		const char* filename;
		filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
		lua_remove(L, -2);  /* remove path template */

		DWORD dwsz;
		HANDLE hFile = rdPtr->rHo.hoAdRunHeader->rh4.rh4Mv->mvOpenHFile(filename, &dwsz, 0);
		if (hFile != INVALID_HANDLE_VALUE) {
			rdPtr->rHo.hoAdRunHeader->rh4.rh4Mv->mvCloseHFile(hFile);
			return filename;
		}
		rdPtr->rHo.hoAdRunHeader->rh4.rh4Mv->mvCloseHFile(hFile);

		//if (readable(filename)) { /* does file exist and is readable? */
		//	return filename;  /* return that file name */
		//}
		lua_pushfstring(L, "\n\tno file " LUA_QS, filename);
		lua_remove(L, -2);  /* remove file name */
		lua_concat(L, 2);  /* add entry to possible error message */
	}
	return NULL;  /* not found */
}
示例#6
0
文件: loadlib.c 项目: Rockbox/rockbox
static const char *findfile (lua_State *L, const char *name,
                                           const char *pname) {
  get_current_path(L, 2); /* ROCKLUA ADDED */
  const char *current_path = lua_tostring(L, -1);
  const char *path;

  name = luaL_gsub(L, name, ".", LUA_DIRSEP);
  lua_getfield(L, LUA_ENVIRONINDEX, pname);
  path = lua_tostring(L, -1);
  if (path == NULL)
    luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
  lua_pushliteral(L, "");  /* error accumulator */
  while ((path = pushnexttemplate(L, path)) != NULL) {
    const char *filename;
    filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
    if(current_path != NULL) filename = luaL_gsub(L, filename, "$", current_path);
    lua_remove(L, -2);  /* remove path template */
    if (readable(filename))  /* does file exist and is readable? */
      return filename;  /* return that file name */
    lua_pushfstring(L, "\n\tno file " LUA_QS, filename);
    lua_remove(L, -2);  /* remove file name */
    lua_concat(L, 2);  /* add entry to possible error message */
  }
  return NULL;  /* not found */
}
static const char *searchpath (lua_State *L, const char *name,
                                             const char *path) {
  name = luaL_gsub(L, name, ".", LUA_DIRSEP);
  lua_pushliteral(L, "");  /* error accumulator */
  while ((path = pushnexttemplate(L, path)) != NULL) {
    const char *filename = luaL_gsub(L, lua_tostring(L, -1),
                                     LUA_PATH_MARK, name);
    lua_remove(L, -2);  /* remove path template */
    if (readable(filename))  /* does file exist and is readable? */
      return filename;  /* return that file name */
    lua_pushfstring(L, "\n\tno file " LUA_QS, filename);
    lua_remove(L, -2);  /* remove file name */
    lua_concat(L, 2);  /* add entry to possible error message */
  }
  return NULL;  /* not found */
}
示例#8
0
static const char *findfile (lua_State *L, const char *name,
                                           const char *pname) {
  const char *path;
  name = luaL_gsub(L, name, ".", LUA_DIRSEP);
  lua_getfield(L, LUA_ENVIRONINDEX, pname);
  path = lua_tostring(L, -1);
  if (path == NULL)
    luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
  while ((path = pushnexttemplate(L, path)) != NULL) {
    const char *filename;
    filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
    if (readable(filename))  /* does file exist and is readable? */
      return filename;  /* return that file name */
    lua_pop(L, 2);  /* remove path template and file name */ 
  }
  return NULL;  /* not found */
}
示例#9
0
	static const char *searchpath(lua_State *L, const char *name, const char *path, const char *sep, const char *dirsep, bool is_local) {
		luaL_Buffer msg;  /* to build error message */
		luaL_buffinit(L, &msg);
		if (*sep != '\0')  /* non-empty separator? */
			name = luaL_gsub(L, name, sep, dirsep);  /* replace it by 'dirsep' */
		while ((path = pushnexttemplate(L, path)) != NULL) {
			const char *filename = luaL_gsub(L, lua_tostring(L, -1),
				LUA_PATH_MARK, name);
			lua_remove(L, -2);  /* remove path template */
			if (readable(filename, is_local))  /* does file exist and is readable? */
				return filename;  /* return that file name */
			lua_pushfstring(L, "\n\tno file " LUA_QS, filename);
			lua_remove(L, -2);  /* remove file name */
			luaL_addvalue(&msg);  /* concatenate error msg. entry */
		}
		luaL_pushresult(&msg);  /* create error message */
		return NULL;  /* not found */
	}
示例#10
0
文件: FileIO.c 项目: scriptum/Engine
/* based on code from lua.c of the Lua distribution */
static const char *findfile (lua_State *L, const char *name) {
	const char *path;
	name = luaL_gsub(L, name, ".", "/");
	lua_getglobal(L, "package");
	lua_getfield(L, -1, "scrupppath");
	path = lua_tostring(L, -1);
	if (path == NULL)
		luaL_error(L, LUA_QL("package.scrupppath") " must be a string");
	lua_pushstring(L, "");  /* error accumulator */
	while ((path = pushnexttemplate(L, path)) != NULL) {
		const char *filename;
		filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
		if (PHYSFS_exists(filename))  /* does file exist? */
			return filename;  /* return that file name */
		lua_pop(L, 2);  /* remove path template and file name */
		lua_pushfstring(L, "\n\tno file " LUA_QL("searchpath://%s") , filename);
		lua_concat(L, 2);
	}
	return NULL;  /* not found */
}
const char *AutotestingSystemLua::findfile (lua_State *L, const char *name, const char *pname) 
{
		const char *path;
		name = luaL_gsub(L, name, ".", LUA_DIRSEP);
		lua_getglobal(L, "package");
		lua_getfield(L, -1, pname);
		path = lua_tostring(L, -1);
		if (path == NULL)
			luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
		lua_pushliteral(L, "");  /* error accumulator */
		while ((path = pushnexttemplate(L, path)) != NULL) {
			const char *filename;
			filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
			lua_remove(L, -2);  /* remove path template */
			if (FileSystem::Instance()->IsFile(filename))  /* does file exist and is readable? */
				return filename;  /* return that file name */
			lua_pushfstring(L, "\n\tno file " LUA_QS, filename);
			lua_remove(L, -2);  /* remove file name */
			lua_concat(L, 2);  /* add entry to possible error message */
		}
		return NULL;  /* not found */
}
示例#12
0
文件: loadlib.c 项目: lvshaco/ejoy2d
static struct sp_entry *
searchpack(lua_State *L, struct sp_entryv *v,
        const char *name,
        const char *path,
        const char *sep,
        const char *dirsep) {
  int top = lua_gettop(L);
  if (*sep != '\0')  /* non-empty separator? */
    name = luaL_gsub(L, name, sep, dirsep);  /* replace it by 'dirsep' */
  while ((path = pushnexttemplate(L, path)) != NULL) {
    const char *filename = luaL_gsub(L, lua_tostring(L, -1),
                                     LUA_PATH_MARK, name);
    struct sp_entry *e = ispacked(v, filename);
    if (e)  {/* does file exist and is readable? */
      lua_settop(L, top);
      return e; /* return that sp_entry */
    }
    lua_pop(L,2);
  }
  lua_settop(L,top); 
  return NULL;  /* not found */
}
示例#13
0
文件: loadlib.c 项目: lvshaco/ejoy2d
static char *
loadpack(lua_State *L, 
    const char *ppath, 
    const char *lpath, 
    const char *name, 
    char **p, size_t *sz) {
  luaL_Buffer msg;  /* to build error message */
  luaL_buffinit(L, &msg);
  
  char *body = NULL;
  while ((ppath = pushnexttemplate(L, ppath)) != NULL) {
    const char *pack = lua_tostring(L,-1);
    FILE *fp = fopen(pack, "r");
    if (fp == NULL) {
      lua_pushfstring(L, "\n\tno pack " LUA_QS, pack);
      goto skip;
    }
    struct sp_entryv v;
    sp_entryv_init(&v);
    if (sp_lentryv(fp, &v)) {
      fclose(fp);
      lua_pushfstring(L, "\n\tillegal pack " LUA_QS, pack);
      goto skip;
    }
    struct sp_entry *e;
    e = searchpack(L, &v, name, lpath, ".", LUA_LSUBSEP);
    if (e == NULL) {
      sp_entryv_fini(&v);
      fclose(fp);
      lua_pushfstring(L, "\n\tno in pack " LUA_QS, pack);
      goto skip;
    }
    body = malloc(e->bodysz);
    fseek(fp, e->offset, SEEK_SET);
    fread(body, e->bodysz, 1, fp);
    size_t size;
    char *dec = sp_decrypt(body, e->bodysz, &size);
    if (dec == NULL) {
      free(body);
      sp_entryv_fini(&v);
      fclose(fp);
      lua_pushfstring(L, "\n\tdecrypt fail pack " LUA_QS, pack);
      luaL_addvalue(&msg);
      lua_pop(L,1);
      break;
    } 
    lua_pop(L,1);
    lua_pushstring(L, e->name);
    sp_entryv_fini(&v);
    fclose(fp);
    *p = dec;
    *sz = size;
    return body;
skip:
    luaL_addvalue(&msg);
    lua_pop(L,1);
    continue;
  }
  luaL_pushresult(&msg);
  return NULL;
}