/// Renames the file to the new name. // @string destination the file or directory path to move the file to. // @return true or nil. (error) // @return error message. // @function renameTo int FileUserdata::renameTo(lua_State* L) { int rv = 0; FileUserdata* fud = checkPrivateUserdata<FileUserdata>(L, 1); size_t pathLen = 0; const char* path = luaL_checklstring(L, 2, &pathLen); if (pathLen == 0) { lua_pushnil(L); lua_pushstring(L, "invalid path"); return 2; } try { fud->mFile.renameTo(path); lua_pushboolean(L, 1); rv = 1; } catch (const Poco::Exception& e) { rv = pushPocoException(L, e); } catch (...) { rv = pushUnknownException(L); } return rv; }
/// Makes the file writeable (if flag is true), or non-writeable (if flag is false). // @bool[opt] writable boolean indicating if the file should be marked writable. (default = true) // @return true or nil. (error) // @return error message. // @function setWritable int FileUserdata::setWritable(lua_State* L) { int rv = 0; int writable = 1; FileUserdata* fud = checkPrivateUserdata<FileUserdata>(L, 1); if (lua_gettop(L) > 1) writable = lua_toboolean(L, 2); try { fud->mFile.setWriteable(writable); lua_pushboolean(L, 1); rv = 1; } catch (const Poco::Exception& e) { rv = pushPocoException(L, e); } catch (...) { rv = pushUnknownException(L); } return rv; }
/// Get a table (array) of file userdata for each entry in the directory. // @return table or nil. (error) // @return error message. // @function listFiles int FileUserdata::listFiles(lua_State* L) { int rv = 0; FileUserdata* fud = checkPrivateUserdata<FileUserdata>(L, 1); try { std::vector<Poco::File> files; fud->mFile.list(files); int tableIndex = 1; lua_createtable(L, files.size(), 0); std::vector<Poco::File>::iterator i = files.begin(); while (i != files.end()) { FileUserdata* newFud = new(lua_newuserdata(L, sizeof *newFud)) FileUserdata(*i); setupPocoUserdata(L, newFud, POCO_FILE_METATABLE_NAME); lua_rawseti(L, -2, tableIndex); ++i; ++tableIndex; } rv = 1; } catch (const Poco::Exception& e) { rv = pushPocoException(L, e); } catch (...) { rv = pushUnknownException(L); } return rv; }
/// Delete's the file or directory. // @bool[opt] recursive delete all sub files and directories recursively. // @return true or nil. (error) // @return error message. // @function remove int FileUserdata::remove(lua_State* L) { int rv = 0; FileUserdata* fud = checkPrivateUserdata<FileUserdata>(L, 1); // default false as per defaul parameter to Poco::File::remove(). int recursive = 0; if (lua_gettop(L) > 1) recursive = lua_toboolean(L, 2); try { fud->mFile.remove(recursive); lua_pushboolean(L, 1); rv = 1; } catch (const Poco::Exception& e) { rv = pushPocoException(L, e); } catch (...) { rv = pushUnknownException(L); } return rv; }
/// Get a table (array) of strings for each entry in the directory. // @return table or nil. (error) // @return error message. // @function listNames int FileUserdata::listNames(lua_State* L) { int rv = 0; FileUserdata* fud = checkPrivateUserdata<FileUserdata>(L, 1); try { std::vector<std::string> fileNames; fud->mFile.list(fileNames); int tableIndex = 1; lua_createtable(L, fileNames.size(), 0); std::vector<std::string>::iterator i = fileNames.begin(); while (i != fileNames.end()) { lua_pushlstring(L, i->c_str(), i->size()); lua_rawseti(L, -2, tableIndex); ++i; ++tableIndex; } rv = 1; } catch (const Poco::Exception& e) { rv = pushPocoException(L, e); } catch (...) { rv = pushUnknownException(L); } return rv; }
/// Attempts to lock the mutex. // @int[opt] ms optional number of milliseconds to try to acquire the mutex. // @return boolean indicating if lock was acquired or timeout occured. // @function tryLock int MutexUserdata::tryLock(lua_State* L) { int rv = 0; MutexUserdata* mud = checkPrivateUserdata<MutexUserdata>(L, 1); int top = lua_gettop(L); long ms; if (top > 1) ms = luaL_checkinteger(L, 2); try { bool result = false; if (top > 1) result = mud->mMutex->tryLock(ms); else result = mud->mMutex->tryLock(); lua_pushboolean(L, result); rv = 1; } catch (const Poco::Exception& e) { rv = pushPocoException(L, e); } catch (...) { rv = pushUnknownException(L); } return rv; }
int FileUserdata::File(lua_State* L) { int rv = 0; size_t pathLen = 0; int firstArg = lua_istable(L, 1) ? 2 : 1; const char* path = luaL_checklstring(L, firstArg, &pathLen); if (pathLen == 0) { lua_pushnil(L); lua_pushstring(L, "invalid path"); return 2; } try { FileUserdata *fud = new(lua_newuserdata(L, sizeof *fud)) FileUserdata(path); setupPocoUserdata(L, fud, POCO_FILE_METATABLE_NAME); rv = 1; } catch (const Poco::Exception& e) { rv = pushPocoException(L, e); } catch (...) { rv = pushUnknownException(L); } return rv; }
/// constructs a new fastmutex userdata. // @return userdata or nil. (error) // @return error message // @function new int FastMutexUserdata::FastMutex(lua_State* L) { int rv = 0; try { FastMutexUserdata* fmud = new(lua_newuserdata(L, sizeof *fmud)) FastMutexUserdata(); setupPocoUserdata(L, fmud, POCO_FASTMUTEX_METATABLE_NAME); rv = 1; } catch (const Poco::Exception& e) { rv = pushPocoException(L, e); } catch (...) { rv = pushUnknownException(L); } return rv; }
// userdata methods int FastMutexUserdata::lock(lua_State* L) { int rv = 0; FastMutexUserdata* fmud = checkPrivateUserdata<FastMutexUserdata>(L, 1); try { fmud->mFastMutex->lock(); } catch (const Poco::Exception& e) { pushPocoException(L, e); lua_error(L); } catch (...) { pushUnknownException(L); lua_error(L); } return rv; }
/// Creates a new, empty file in an atomic operation. Returns true if the file has been created and false if the file already exists. // @return true or nil. (error) // @return error message. // @function createFile int FileUserdata::createFile(lua_State* L) { int rv = 0; FileUserdata* fud = checkPrivateUserdata<FileUserdata>(L, 1); try { int created = fud->mFile.createFile(); lua_pushboolean(L, created); rv = 1; } catch (const Poco::Exception& e) { rv = pushPocoException(L, e); } catch (...) { rv = pushUnknownException(L); } return rv; }
/// create a new mutex userdata. // @return userdata or nil. (error) // @return error message. // @function new int MutexUserdata::Mutex(lua_State* L) { int rv = 0; void* ud = lua_newuserdata(L, sizeof(MutexUserdata)); luaL_getmetatable(L, "Poco.Mutex.metatable"); lua_setmetatable(L, -2); try { rv = 1; MutexUserdata* mud = new(ud) MutexUserdata(); setPrivateUserdata(L, -1, mud); } catch (const Poco::Exception& e) { rv = pushPocoException(L, e); } catch (...) { rv = pushUnknownException(L); } return rv; }
/// Unlocks the mutex so that it can be acquired by other threads. // @function unlock int MutexUserdata::unlock(lua_State* L) { int rv = 0; MutexUserdata* mud = checkPrivateUserdata<MutexUserdata>(L, 1); try { mud->mMutex->unlock(); lua_pushboolean(L, 1); rv = 1; } catch (const Poco::Exception& e) { rv = pushPocoException(L, e); } catch (...) { rv = pushUnknownException(L); } return rv; }
/// Gets the last modified date of the file userdata as a timestamp userdata. // @see timestamp // @return timestamp userdata or nil. (error) // @return error message. // @function lastModified int FileUserdata::getLastModified(lua_State* L) { int rv = 0; FileUserdata* fud = checkPrivateUserdata<FileUserdata>(L, 1); try { Poco::Timestamp ts = fud->mFile.getLastModified(); TimestampUserdata* tsud = new(lua_newuserdata(L, sizeof *tsud)) TimestampUserdata(ts); setupPocoUserdata(L, tsud, POCO_TIMESTAMP_METATABLE_NAME); rv = 1; } catch (const Poco::Exception& e) { rv = pushPocoException(L, e); } catch (...) { rv = pushUnknownException(L); } return rv; }
/// Constructs a new fileostream userdata. // @string path to file. // @return userdata or nil. (error) // @return error message. // @function new // @see ostream int FileOStreamUserdata::FileOStream(lua_State* L) { int rv = 0; int firstArg = lua_istable(L, 1) ? 2 : 1; const char* path = luaL_checkstring(L, firstArg); try { FileOStreamUserdata* fosud = new(lua_newuserdata(L, sizeof *fosud)) FileOStreamUserdata(path); setupPocoUserdata(L, fosud, POCO_FILEOSTREAM_METATABLE_NAME); rv = 1; } catch (const Poco::Exception& e) { rv = pushPocoException(L, e); } catch (...) { rv = pushUnknownException(L); } return rv; }
/// sets the size of the file in bytes. Can be used to truncate a file. // @int size the new size of the file path. // @return true or nil. (error) // @return error message. // @function setSize int FileUserdata::setSize(lua_State* L) { int rv = 0; FileUserdata* fud = checkPrivateUserdata<FileUserdata>(L, 1); lua_Number size = luaL_checknumber(L, 2); try { fud->mFile.setSize(size); lua_pushboolean(L, 1); rv = 1; } catch (const Poco::Exception& e) { rv = pushPocoException(L, e); } catch (...) { rv = pushUnknownException(L); } return rv; }
/// Sets the modification date of the file path. // @see timestamp // @param timestamp userdata representing the last modified time. // @return true or nil. (error) // @return error message. // @function setLastModified int FileUserdata::setLastModified(lua_State* L) { int rv = 0; FileUserdata* fud = checkPrivateUserdata<FileUserdata>(L, 1); TimestampUserdata* tsud = checkPrivateUserdata<TimestampUserdata>(L, 2); try { fud->mFile.setLastModified(tsud->mTimestamp); lua_pushboolean(L, 1); rv = 1; } catch (const Poco::Exception& e) { rv = pushPocoException(L, e); } catch (...) { rv = pushUnknownException(L); } return rv; }
/// Checks if the file can be read. // @return boolean // @function canWrite int FileUserdata::canWrite(lua_State* L) { int rv = 0; FileUserdata* fud = checkPrivateUserdata<FileUserdata>(L, 1); try { int writable = fud->mFile.canWrite(); lua_pushboolean(L, writable); rv = 1; } catch (const Poco::Exception& e) { pushPocoException(L, e); lua_error(L); } catch (...) { pushUnknownException(L); lua_error(L); } return rv; }
/// Gets the size of the file path entry as a number. // @return number file size. // @function size int FileUserdata::getSize(lua_State* L) { int rv = 0; FileUserdata* fud = checkPrivateUserdata<FileUserdata>(L, 1); try { lua_Number num = fud->mFile.getSize(); lua_pushnumber(L, num); rv = 1; } catch (const Poco::Exception& e) { pushPocoException(L, e); lua_error(L); } catch (...) { pushUnknownException(L); lua_error(L); } return rv; }
/// Checks if the file path is a symbolic link. // @return boolean // @function isLink int FileUserdata::isLink(lua_State* L) { int rv = 0; FileUserdata* fud = checkPrivateUserdata<FileUserdata>(L, 1); try { int link = fud->mFile.isLink(); lua_pushboolean(L, link); rv = 1; } catch (const Poco::Exception& e) { pushPocoException(L, e); lua_error(L); } catch (...) { pushUnknownException(L); lua_error(L); } return rv; }