Applied the new style guidelines.

Well, sort of. Lot's more to be done, but this is a start.
This commit is contained in:
rude
2012-06-28 23:52:33 +02:00
parent bc4e59ba24
commit 81c38e22d0
302 changed files with 42648 additions and 42081 deletions
+243 -240
View File
@@ -1,240 +1,243 @@
/**
* Copyright (c) 2006-2012 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "File.h"
// STD
#include <cstring>
// LOVE
#include "Filesystem.h"
#include <filesystem/FileData.h>
namespace love
{
namespace filesystem
{
namespace physfs
{
extern bool hack_setupWriteDirectory();
File::File(std::string filename)
: filename(filename), file(0), mode(filesystem::File::CLOSED)
{
}
File::~File()
{
if (mode != CLOSED)
close();
}
bool File::open(Mode mode)
{
if (mode == CLOSED)
return true;
// File must exist if read mode.
if ((mode == READ) && !PHYSFS_exists(filename.c_str()))
throw love::Exception("Could not open file %s. Does not exist.", filename.c_str());
// Check whether the write directory is set.
if ((mode == APPEND || mode == WRITE) && (PHYSFS_getWriteDir() == 0) && !hack_setupWriteDirectory())
throw love::Exception("Could not set write directory.");
// File already open?
if (file != 0)
return false;
this->mode = mode;
switch(mode)
{
case READ:
file = PHYSFS_openRead(filename.c_str());
break;
case APPEND:
file = PHYSFS_openAppend(filename.c_str());
break;
case WRITE:
file = PHYSFS_openWrite(filename.c_str());
break;
default:
break;
}
return (file != 0);
}
bool File::close()
{
if (!PHYSFS_close(file))
return false;
mode = CLOSED;
file = 0;
return true;
}
int64 File::getSize()
{
// If the file is closed, open it to
// check the size.
if (file == 0)
{
open(READ);
int64 size = (int64)PHYSFS_fileLength(file);
close();
return size;
}
return (int64)PHYSFS_fileLength(file);
}
Data * File::read(int64 size)
{
bool isOpen = (file != 0);
if (!isOpen && !open(READ))
throw love::Exception("Could not read file %s.", filename.c_str());
int64 max = (int64)PHYSFS_fileLength(file);
size = (size == ALL) ? max : size;
size = (size > max) ? max : size;
FileData * fileData = new FileData(size, getFilename());
read(fileData->getData(), size);
if (!isOpen)
close();
return fileData;
}
int64 File::read(void * dst, int64 size)
{
bool isOpen = (file != 0);
if (!isOpen)
open(READ);
int64 max = (int64)PHYSFS_fileLength(file);
size = (size == ALL) ? max : size;
size = (size > max) ? max : size;
// Sadly, we'll have to clamp to 32 bits here
size = (size > LOVE_UINT32_MAX) ? LOVE_UINT32_MAX : size;
int64 read = (int64)PHYSFS_read(file, dst, 1, (int) size);
if (!isOpen)
close();
return read;
}
bool File::write(const void * data, int64 size)
{
if (file == 0)
throw love::Exception("Could not write to file. File not open.");
// Another clamp, for the time being.
size = (size > LOVE_UINT32_MAX) ? LOVE_UINT32_MAX : size;
// Try to write.
int64 written = static_cast<int64>(PHYSFS_write(file, data, 1, (int) size));
// Check that correct amount of data was written.
if (written != size)
return false;
return true;
}
bool File::write(const Data * data, int64 size)
{
return write(data->getData(), (size == ALL) ? data->getSize() : size);
}
#ifdef LOVE_WINDOWS
// MSVC doesn't like the 'this' keyword
// well, we'll use 'that'.
// It zigs, we zag.
inline bool test_eof(File * that, PHYSFS_File *)
{
int64 pos = that->tell();
int64 size = that->getSize();
return pos == -1 || size == -1 || pos >= size;
}
#else
inline bool test_eof(File *, PHYSFS_File * file)
{
return PHYSFS_eof(file);
}
#endif
bool File::eof()
{
if (file == 0 || test_eof(this, file))
return true;
return false;
}
int64 File::tell()
{
if (file == 0)
return -1;
return (int64)PHYSFS_tell(file);
}
bool File::seek(uint64 pos)
{
if (file == 0)
return false;
if (!PHYSFS_seek(file, (PHYSFS_uint64)pos))
return false;
return true;
}
std::string File::getFilename() const
{
return filename;
}
std::string File::getExtension() const
{
std::string::size_type idx = filename.rfind('.');
if (idx != std::string::npos)
return filename.substr(idx+1);
else
return std::string();
}
filesystem::File::Mode File::getMode()
{
return mode;
}
} // physfs
} // filesystem
} // love
/**
* Copyright (c) 2006-2012 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "File.h"
// STD
#include <cstring>
// LOVE
#include "Filesystem.h"
#include "filesystem/FileData.h"
namespace love
{
namespace filesystem
{
namespace physfs
{
extern bool hack_setupWriteDirectory();
File::File(std::string filename)
: filename(filename)
, file(0)
, mode(filesystem::File::CLOSED)
{
}
File::~File()
{
if (mode != CLOSED)
close();
}
bool File::open(Mode mode)
{
if (mode == CLOSED)
return true;
// File must exist if read mode.
if ((mode == READ) && !PHYSFS_exists(filename.c_str()))
throw love::Exception("Could not open file %s. Does not exist.", filename.c_str());
// Check whether the write directory is set.
if ((mode == APPEND || mode == WRITE) && (PHYSFS_getWriteDir() == 0) && !hack_setupWriteDirectory())
throw love::Exception("Could not set write directory.");
// File already open?
if (file != 0)
return false;
this->mode = mode;
switch (mode)
{
case READ:
file = PHYSFS_openRead(filename.c_str());
break;
case APPEND:
file = PHYSFS_openAppend(filename.c_str());
break;
case WRITE:
file = PHYSFS_openWrite(filename.c_str());
break;
default:
break;
}
return (file != 0);
}
bool File::close()
{
if (!PHYSFS_close(file))
return false;
mode = CLOSED;
file = 0;
return true;
}
int64 File::getSize()
{
// If the file is closed, open it to
// check the size.
if (file == 0)
{
open(READ);
int64 size = (int64)PHYSFS_fileLength(file);
close();
return size;
}
return (int64)PHYSFS_fileLength(file);
}
Data *File::read(int64 size)
{
bool isOpen = (file != 0);
if (!isOpen && !open(READ))
throw love::Exception("Could not read file %s.", filename.c_str());
int64 max = (int64)PHYSFS_fileLength(file);
size = (size == ALL) ? max : size;
size = (size > max) ? max : size;
FileData *fileData = new FileData(size, getFilename());
read(fileData->getData(), size);
if (!isOpen)
close();
return fileData;
}
int64 File::read(void *dst, int64 size)
{
bool isOpen = (file != 0);
if (!isOpen)
open(READ);
int64 max = (int64)PHYSFS_fileLength(file);
size = (size == ALL) ? max : size;
size = (size > max) ? max : size;
// Sadly, we'll have to clamp to 32 bits here
size = (size > LOVE_UINT32_MAX) ? LOVE_UINT32_MAX : size;
int64 read = (int64)PHYSFS_read(file, dst, 1, (int) size);
if (!isOpen)
close();
return read;
}
bool File::write(const void *data, int64 size)
{
if (file == 0)
throw love::Exception("Could not write to file. File not open.");
// Another clamp, for the time being.
size = (size > LOVE_UINT32_MAX) ? LOVE_UINT32_MAX : size;
// Try to write.
int64 written = static_cast<int64>(PHYSFS_write(file, data, 1, (int) size));
// Check that correct amount of data was written.
if (written != size)
return false;
return true;
}
bool File::write(const Data *data, int64 size)
{
return write(data->getData(), (size == ALL) ? data->getSize() : size);
}
#ifdef LOVE_WINDOWS
// MSVC doesn't like the 'this' keyword
// well, we'll use 'that'.
// It zigs, we zag.
inline bool test_eof(File *that, PHYSFS_File *)
{
int64 pos = that->tell();
int64 size = that->getSize();
return pos == -1 || size == -1 || pos >= size;
}
#else
inline bool test_eof(File *, PHYSFS_File *file)
{
return PHYSFS_eof(file);
}
#endif
bool File::eof()
{
if (file == 0 || test_eof(this, file))
return true;
return false;
}
int64 File::tell()
{
if (file == 0)
return -1;
return (int64)PHYSFS_tell(file);
}
bool File::seek(uint64 pos)
{
if (file == 0)
return false;
if (!PHYSFS_seek(file, (PHYSFS_uint64)pos))
return false;
return true;
}
std::string File::getFilename() const
{
return filename;
}
std::string File::getExtension() const
{
std::string::size_type idx = filename.rfind('.');
if (idx != std::string::npos)
return filename.substr(idx+1);
else
return std::string();
}
filesystem::File::Mode File::getMode()
{
return mode;
}
} // physfs
} // filesystem
} // love
+89 -88
View File
@@ -1,88 +1,89 @@
/**
* Copyright (c) 2006-2012 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_FILESYSTEM_PHYSFS_FILE_H
#define LOVE_FILESYSTEM_PHYSFS_FILE_H
// LOVE
#include <filesystem/File.h>
// PhysFS
#ifdef LOVE_MACOSX // wacky Mac behavior means different #include syntax!
#include <physfs/physfs.h>
#else
#include <physfs.h>
#endif
// STD
#include <string>
namespace love
{
namespace filesystem
{
namespace physfs
{
class File : public love::filesystem::File
{
private:
// filename
std::string filename;
// PHYSFS File handle.
PHYSFS_file * file;
// The current mode of the file.
Mode mode;
public:
/**
* Constructs an File with the given source and filename.
* @param source The source from which to load the file. (Archive or directory)
* @param filename The relative filepath of the file to load from the source.
**/
File(std::string filename);
virtual ~File();
// Implements love::filesystem::File.
bool open(Mode mode);
bool close();
int64 getSize();
Data * read(int64 size = ALL);
int64 read(void * dst, int64 size);
bool write(const void * data, int64 size);
bool write(const Data * data, int64 size = ALL);
bool eof();
int64 tell();
bool seek(uint64 pos);
Mode getMode();
std::string getFilename() const;
std::string getExtension() const;
}; // File
} // physfs
} // filesystem
} // love
#endif // LOVE_FILESYSTEM_PHYSFS_FILE_H
/**
* Copyright (c) 2006-2012 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_FILESYSTEM_PHYSFS_FILE_H
#define LOVE_FILESYSTEM_PHYSFS_FILE_H
// LOVE
#include "filesystem/File.h"
// PhysFS
#ifdef LOVE_MACOSX // wacky Mac behavior means different #include syntax!
#include <physfs/physfs.h>
#else
#include <physfs.h>
#endif
// STD
#include <string>
namespace love
{
namespace filesystem
{
namespace physfs
{
class File : public love::filesystem::File
{
private:
// filename
std::string filename;
// PHYSFS File handle.
PHYSFS_file *file;
// The current mode of the file.
Mode mode;
public:
/**
* Constructs an File with the given source and filename.
* @param source The source from which to load the file. (Archive or directory)
* @param filename The relative filepath of the file to load from the source.
**/
File(std::string filename);
virtual ~File();
// Implements love::filesystem::File.
bool open(Mode mode);
bool close();
int64 getSize();
Data *read(int64 size = ALL);
int64 read(void *dst, int64 size);
bool write(const void *data, int64 size);
bool write(const Data *data, int64 size = ALL);
bool eof();
int64 tell();
bool seek(uint64 pos);
Mode getMode();
std::string getFilename() const;
std::string getExtension() const;
}; // File
} // physfs
} // filesystem
} // love
#endif // LOVE_FILESYSTEM_PHYSFS_FILE_H
File diff suppressed because it is too large Load Diff
+297 -297
View File
@@ -1,297 +1,297 @@
/**
* Copyright (c) 2006-2012 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_FILESYSTEM_PHYSFS_FILESYSTEM_H
#define LOVE_FILESYSTEM_PHYSFS_FILESYSTEM_H
// STD
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
// LOVE
#include <common/Module.h>
#include <common/config.h>
#include <common/int.h>
#include <filesystem/FileData.h>
#include "File.h"
// For great CWD. (Current Working Directory)
// Using this instead of boost::filesystem which totally
// cramped our style.
#ifdef LOVE_WINDOWS
# include <windows.h>
# include <direct.h>
#else
# include <sys/param.h>
# include <unistd.h>
#endif
// In Windows, we would like to use "LOVE" as the
// application folder, but in Linux, we like .love.
#define LOVE_APPDATA_PREFIX ""
#ifdef LOVE_WINDOWS
# define LOVE_APPDATA_FOLDER "LOVE"
# define LOVE_PATH_SEPARATOR "/"
# define LOVE_MAX_PATH _MAX_PATH
#else
# ifdef LOVE_MACOSX
# define LOVE_APPDATA_FOLDER "LOVE"
# elif defined(LOVE_LINUX)
# define LOVE_APPDATA_FOLDER "love"
# else
# define LOVE_APPDATA_PREFIX "."
# define LOVE_APPDATA_FOLDER "love"
# endif
# define LOVE_PATH_SEPARATOR "/"
# define LOVE_MAX_PATH MAXPATHLEN
#endif
namespace love
{
namespace filesystem
{
namespace physfs
{
class Filesystem : public Module
{
private:
// Counts open files.
int open_count;
// Pointer used for file reads.
char * buffer;
// Contains the current working directory (UTF8).
std::string cwd;
// %APPDATA% on Windows.
std::string appdata;
// This name will be used to create the folder
// in the appdata/userdata folder.
std::string save_identity;
// Full and relative paths of the game save folder.
// (Relative to the %APPDATA% folder, meaning that the
// relative string will look something like: ./LOVE/game)
std::string save_path_relative, save_path_full;
// The full path to the source of the game.
std::string game_source;
// Workaround for machines without PhysFS 2.0
bool isInited;
// Allow saving outside of the LOVE_APPDATA_FOLDER
// for release 'builds'
bool release;
bool releaseSet;
protected:
public:
Filesystem();
~Filesystem();
const char * getName() const;
void init(const char * arg0);
void setRelease(bool release);
bool isRelease() const;
/**
* This sets up the save directory. If the
* it is already set up, nothing happens.
* @return True on success, false otherwise.
**/
bool setupWriteDirectory();
/**
* Sets the name of the save folder.
* @param ident The name of the game. Will be used to
* to create the folder in the LOVE data folder.
**/
bool setIdentity(const char * ident);
const char * getIdentity() const;
/**
* Sets the path to the game source.
* This can only be set once.
* @param source Path to a directory or a .love-file.
**/
bool setSource(const char * source);
/**
* Creates a new file.
**/
File * newFile(const char* filename);
/**
* Creates a new FileData object. Data will be copied.
* @param data Pointer to the data.
* @param size The size of the data.
* @param filename The full filename used to file type identification.
**/
FileData * newFileData(void * data, unsigned int size, const char * filename);
/**
* Creates a new FileData object from base64 data.
* @param b64 The base64 data.
**/
FileData * newFileData(const char * b64, const char * filename);
/**
* Gets the current working directory.
**/
const char * getWorkingDirectory();
/**
* Gets the user home directory.
**/
const char * getUserDirectory();
/**
* Gets the APPDATA directory. On Windows, this is the folder
* in the %APPDATA% enviroment variable. On Linux, this is the
* user home folder.
**/
const char * getAppdataDirectory();
/**
* Gets the full path of the save folder.
**/
const char * getSaveDirectory();
/**
* Checks whether a file exists in the current search path
* or not.
* @param file The filename to check.
**/
bool exists(const char * file);
/**
* Checks if an existing file really is a directory.
* @param file The filename to check.
**/
bool isDirectory(const char * file);
/**
* Checks if an existing file really is a file,
* and not a directory.
* @param file The filename to check.
**/
bool isFile(const char * file);
/**
* Creates a directory. Write dir must be set.
* @param file The directory to create.
**/
bool mkdir(const char * file);
/**
* Removes a file (or directory).
* @param file The file or directory to remove.
**/
bool remove(const char * file);
/**
* Opens a file for reading or writing. (Depends
* on the mode chosen at the time of creation).
* @param file The file to open.
* @param mode The mode to open the file in.
**/
bool open(File * file, File::Mode mode);
/**
* Closes a file.
* @param file The file to close.
**/
bool close(File * file);
/**
* Reads count bytes from an open file.
* The first parameter is either a File or
* a string. An optional second parameter specified the
* max number of bytes to read.
**/
int read(lua_State * L);
/**
* Write the bytes in data to the file. File
* must be opened for write.
* The first parameter is either a File or
* a string.
**/
int write(lua_State * L);
/**
* Check if end-of-file is reached.
* @return True if EOF, false otherwise.
**/
bool eof(File * file);
/**
* Gets the current position in a file.
* @param file An open File.
**/
int tell(File * file);
/**
* Seek to a position within a file.
* @param pos The position to seek to.
**/
bool seek(File * file, uint64 pos);
/**
* This "native" method returns a table of all
* files in a given directory.
**/
int enumerate(lua_State * L);
/**
* Loads a file without running it. The loaded
* chunk is returned as a function.
* @param filename The filename of the file to load.
* @return A function.
**/
int load(lua_State * L);
int getLastModified(lua_State * L);
/**
* Text file line-reading iterator function used and
* pushed on the Lua stack by love.filesystem.lines
* and File:lines.
**/
static int lines_i(lua_State * L);
}; // Filesystem
} // physfs
} // filesystem
} // love
#endif // LOVE_FILESYSTEM_PHYSFS_FILESYSTEM_H
/**
* Copyright (c) 2006-2012 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_FILESYSTEM_PHYSFS_FILESYSTEM_H
#define LOVE_FILESYSTEM_PHYSFS_FILESYSTEM_H
// STD
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
// LOVE
#include "common/Module.h"
#include "common/config.h"
#include "common/int.h"
#include "filesystem/FileData.h"
#include "File.h"
// For great CWD. (Current Working Directory)
// Using this instead of boost::filesystem which totally
// cramped our style.
#ifdef LOVE_WINDOWS
# include <windows.h>
# include <direct.h>
#else
# include <sys/param.h>
# include <unistd.h>
#endif
// In Windows, we would like to use "LOVE" as the
// application folder, but in Linux, we like .love.
#define LOVE_APPDATA_PREFIX ""
#ifdef LOVE_WINDOWS
# define LOVE_APPDATA_FOLDER "LOVE"
# define LOVE_PATH_SEPARATOR "/"
# define LOVE_MAX_PATH _MAX_PATH
#else
# ifdef LOVE_MACOSX
# define LOVE_APPDATA_FOLDER "LOVE"
# elif defined(LOVE_LINUX)
# define LOVE_APPDATA_FOLDER "love"
# else
# define LOVE_APPDATA_PREFIX "."
# define LOVE_APPDATA_FOLDER "love"
# endif
# define LOVE_PATH_SEPARATOR "/"
# define LOVE_MAX_PATH MAXPATHLEN
#endif
namespace love
{
namespace filesystem
{
namespace physfs
{
class Filesystem : public Module
{
private:
// Counts open files.
int open_count;
// Pointer used for file reads.
char *buffer;
// Contains the current working directory (UTF8).
std::string cwd;
// %APPDATA% on Windows.
std::string appdata;
// This name will be used to create the folder
// in the appdata/userdata folder.
std::string save_identity;
// Full and relative paths of the game save folder.
// (Relative to the %APPDATA% folder, meaning that the
// relative string will look something like: ./LOVE/game)
std::string save_path_relative, save_path_full;
// The full path to the source of the game.
std::string game_source;
// Workaround for machines without PhysFS 2.0
bool isInited;
// Allow saving outside of the LOVE_APPDATA_FOLDER
// for release 'builds'
bool release;
bool releaseSet;
protected:
public:
Filesystem();
~Filesystem();
const char *getName() const;
void init(const char *arg0);
void setRelease(bool release);
bool isRelease() const;
/**
* This sets up the save directory. If the
* it is already set up, nothing happens.
* @return True on success, false otherwise.
**/
bool setupWriteDirectory();
/**
* Sets the name of the save folder.
* @param ident The name of the game. Will be used to
* to create the folder in the LOVE data folder.
**/
bool setIdentity(const char *ident);
const char *getIdentity() const;
/**
* Sets the path to the game source.
* This can only be set once.
* @param source Path to a directory or a .love-file.
**/
bool setSource(const char *source);
/**
* Creates a new file.
**/
File *newFile(const char *filename);
/**
* Creates a new FileData object. Data will be copied.
* @param data Pointer to the data.
* @param size The size of the data.
* @param filename The full filename used to file type identification.
**/
FileData *newFileData(void *data, unsigned int size, const char *filename);
/**
* Creates a new FileData object from base64 data.
* @param b64 The base64 data.
**/
FileData *newFileData(const char *b64, const char *filename);
/**
* Gets the current working directory.
**/
const char *getWorkingDirectory();
/**
* Gets the user home directory.
**/
const char *getUserDirectory();
/**
* Gets the APPDATA directory. On Windows, this is the folder
* in the %APPDATA% enviroment variable. On Linux, this is the
* user home folder.
**/
const char *getAppdataDirectory();
/**
* Gets the full path of the save folder.
**/
const char *getSaveDirectory();
/**
* Checks whether a file exists in the current search path
* or not.
* @param file The filename to check.
**/
bool exists(const char *file);
/**
* Checks if an existing file really is a directory.
* @param file The filename to check.
**/
bool isDirectory(const char *file);
/**
* Checks if an existing file really is a file,
* and not a directory.
* @param file The filename to check.
**/
bool isFile(const char *file);
/**
* Creates a directory. Write dir must be set.
* @param file The directory to create.
**/
bool mkdir(const char *file);
/**
* Removes a file (or directory).
* @param file The file or directory to remove.
**/
bool remove(const char *file);
/**
* Opens a file for reading or writing. (Depends
* on the mode chosen at the time of creation).
* @param file The file to open.
* @param mode The mode to open the file in.
**/
bool open(File *file, File::Mode mode);
/**
* Closes a file.
* @param file The file to close.
**/
bool close(File *file);
/**
* Reads count bytes from an open file.
* The first parameter is either a File or
* a string. An optional second parameter specified the
* max number of bytes to read.
**/
int read(lua_State *L);
/**
* Write the bytes in data to the file. File
* must be opened for write.
* The first parameter is either a File or
* a string.
**/
int write(lua_State *L);
/**
* Check if end-of-file is reached.
* @return True if EOF, false otherwise.
**/
bool eof(File *file);
/**
* Gets the current position in a file.
* @param file An open File.
**/
int tell(File *file);
/**
* Seek to a position within a file.
* @param pos The position to seek to.
**/
bool seek(File *file, uint64 pos);
/**
* This "native" method returns a table of all
* files in a given directory.
**/
int enumerate(lua_State *L);
/**
* Loads a file without running it. The loaded
* chunk is returned as a function.
* @param filename The filename of the file to load.
* @return A function.
**/
int load(lua_State *L);
int getLastModified(lua_State *L);
/**
* Text file line-reading iterator function used and
* pushed on the Lua stack by love.filesystem.lines
* and File:lines.
**/
static int lines_i(lua_State *L);
}; // Filesystem
} // physfs
} // filesystem
} // love
#endif // LOVE_FILESYSTEM_PHYSFS_FILESYSTEM_H
+227 -225
View File
@@ -1,225 +1,227 @@
/**
* Copyright (c) 2006-2012 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "wrap_File.h"
#include <common/Data.h>
#include <common/Exception.h>
#include <common/int.h>
namespace love
{
namespace filesystem
{
namespace physfs
{
File * luax_checkfile(lua_State * L, int idx)
{
return luax_checktype<File>(L, idx, "File", FILESYSTEM_FILE_T);
}
int w_File_getSize(lua_State * L)
{
File * t = luax_checkfile(L, 1);
int64 size = t->getSize();
// Push nil on failure or if size does not fit into a double precision floating-point number.
if (size == -1 || size >= 0x20000000000000LL)
lua_pushnil(L);
else
lua_pushnumber(L, (lua_Number)size);
return 1;
}
int w_File_open(lua_State * L)
{
File * file = luax_checkfile(L, 1);
File::Mode mode;
if (!File::getConstant(luaL_checkstring(L, 2), mode))
return luaL_error(L, "Incorrect file open mode: %s", luaL_checkstring(L, 2));
try
{
lua_pushboolean(L, file->open(mode) ? 1 : 0);
}
catch (Exception e)
{
return luaL_error(L, e.what());
}
return 1;
}
int w_File_close(lua_State * L)
{
File * file = luax_checkfile(L, 1);
lua_pushboolean(L, file->close() ? 1 : 0);
return 1;
}
int w_File_read(lua_State * L)
{
File * file = luax_checkfile(L, 1);
Data * d = 0;
int64 size = (int64)luaL_optnumber(L, 2, (lua_Number) file->getSize());
try
{
d = file->read(size);
}
catch (Exception e)
{
return luaL_error(L, e.what());
}
lua_pushlstring(L, (const char*) d->getData(), d->getSize());
lua_pushnumber(L, d->getSize());
d->release();
return 2;
}
int w_File_write(lua_State * L)
{
File * file = luax_checkfile(L, 1);
bool result;
if ( file->getMode() == File::CLOSED )
return luaL_error(L, "File is not open.");
if ( lua_isstring(L, 2) )
{
try
{
result = file->write(lua_tostring(L, 2), luaL_optint(L, 3, lua_objlen(L, 2)));
}
catch (Exception e)
{
return luaL_error(L, e.what());
}
}
else if ( luax_istype(L, 2, DATA_T))
{
try
{
love::Data * data = luax_totype<love::Data>(L, 2, "Data", DATA_T);
result = file->write(data, luaL_optint(L, 3, data->getSize()));
}
catch (Exception e)
{
return luaL_error(L, e.what());
}
}
else
{
return luaL_error(L, "String or data expected.");
}
lua_pushboolean(L, result);
return 1;
}
int w_File_eof(lua_State * L)
{
File * file = luax_checkfile(L, 1);
luax_pushboolean(L, file->eof());
return 1;
}
int w_File_tell(lua_State * L)
{
File * file = luax_checkfile(L, 1);
int64 pos = file->tell();
// Push nil on failure or if pos does not fit into a double precision floating-point number.
if (pos == -1 || pos >= 0x20000000000000LL)
lua_pushnil(L);
else
lua_pushnumber(L, (lua_Number)pos);
return 1;
}
int w_File_seek(lua_State * L)
{
File * file = luax_checkfile(L, 1);
lua_Number pos = luaL_checknumber(L, 2);
// Push false on negative and precision-problematic numbers.
// Better fail than seek to an unknown position.
if (pos < 0.0 || pos >= 9007199254740992.0)
luax_pushboolean(L, false);
else
luax_pushboolean(L, file->seek((uint64)pos));
return 1;
}
int w_File_lines(lua_State * L)
{
File * file;
if (luax_istype(L, 1, FILESYSTEM_FILE_T))
{
file = luax_checktype<File>(L, 1, "File", FILESYSTEM_FILE_T);
lua_pushnumber(L, 0); // File position.
luax_pushboolean(L, file->getMode() != File::CLOSED); // Save current file mode.
}
else
return luaL_error(L, "Expected File.");
if (file->getMode() != File::READ)
{
if (file->getMode() != File::CLOSED)
file->close();
try
{
if (!file->open(File::READ))
return luaL_error(L, "Could not open file.");
}
catch (love::Exception & e)
{
return luaL_error(L, "%s", e.what());
}
}
lua_pushcclosure(L, Filesystem::lines_i, 3);
return 1;
}
static const luaL_Reg functions[] = {
{ "getSize", w_File_getSize },
{ "open", w_File_open },
{ "close", w_File_close },
{ "read", w_File_read },
{ "write", w_File_write },
{ "eof", w_File_eof },
{ "tell", w_File_tell },
{ "seek", w_File_seek },
{ "lines", w_File_lines },
{ 0, 0 }
};
extern "C" int luaopen_file(lua_State * L)
{
return luax_register_type(L, "File", functions);
}
} // physfs
} // filesystem
} // love
/**
* Copyright (c) 2006-2012 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "wrap_File.h"
#include "common/Data.h"
#include "common/Exception.h"
#include "common/int.h"
namespace love
{
namespace filesystem
{
namespace physfs
{
File *luax_checkfile(lua_State *L, int idx)
{
return luax_checktype<File>(L, idx, "File", FILESYSTEM_FILE_T);
}
int w_File_getSize(lua_State *L)
{
File *t = luax_checkfile(L, 1);
int64 size = t->getSize();
// Push nil on failure or if size does not fit into a double precision floating-point number.
if (size == -1 || size >= 0x20000000000000LL)
lua_pushnil(L);
else
lua_pushnumber(L, (lua_Number)size);
return 1;
}
int w_File_open(lua_State *L)
{
File *file = luax_checkfile(L, 1);
File::Mode mode;
if (!File::getConstant(luaL_checkstring(L, 2), mode))
return luaL_error(L, "Incorrect file open mode: %s", luaL_checkstring(L, 2));
try
{
lua_pushboolean(L, file->open(mode) ? 1 : 0);
}
catch(Exception e)
{
return luaL_error(L, e.what());
}
return 1;
}
int w_File_close(lua_State *L)
{
File *file = luax_checkfile(L, 1);
lua_pushboolean(L, file->close() ? 1 : 0);
return 1;
}
int w_File_read(lua_State *L)
{
File *file = luax_checkfile(L, 1);
Data *d = 0;
int64 size = (int64)luaL_optnumber(L, 2, (lua_Number) file->getSize());
try
{
d = file->read(size);
}
catch(Exception e)
{
return luaL_error(L, e.what());
}
lua_pushlstring(L, (const char *) d->getData(), d->getSize());
lua_pushnumber(L, d->getSize());
d->release();
return 2;
}
int w_File_write(lua_State *L)
{
File *file = luax_checkfile(L, 1);
bool result;
if (file->getMode() == File::CLOSED)
return luaL_error(L, "File is not open.");
if (lua_isstring(L, 2))
{
try
{
result = file->write(lua_tostring(L, 2), luaL_optint(L, 3, lua_objlen(L, 2)));
}
catch(Exception e)
{
return luaL_error(L, e.what());
}
}
else if (luax_istype(L, 2, DATA_T))
{
try
{
love::Data *data = luax_totype<love::Data>(L, 2, "Data", DATA_T);
result = file->write(data, luaL_optint(L, 3, data->getSize()));
}
catch(Exception e)
{
return luaL_error(L, e.what());
}
}
else
{
return luaL_error(L, "String or data expected.");
}
lua_pushboolean(L, result);
return 1;
}
int w_File_eof(lua_State *L)
{
File *file = luax_checkfile(L, 1);
luax_pushboolean(L, file->eof());
return 1;
}
int w_File_tell(lua_State *L)
{
File *file = luax_checkfile(L, 1);
int64 pos = file->tell();
// Push nil on failure or if pos does not fit into a double precision floating-point number.
if (pos == -1 || pos >= 0x20000000000000LL)
lua_pushnil(L);
else
lua_pushnumber(L, (lua_Number)pos);
return 1;
}
int w_File_seek(lua_State *L)
{
File *file = luax_checkfile(L, 1);
lua_Number pos = luaL_checknumber(L, 2);
// Push false on negative and precision-problematic numbers.
// Better fail than seek to an unknown position.
if (pos < 0.0 || pos >= 9007199254740992.0)
luax_pushboolean(L, false);
else
luax_pushboolean(L, file->seek((uint64)pos));
return 1;
}
int w_File_lines(lua_State *L)
{
File *file;
if (luax_istype(L, 1, FILESYSTEM_FILE_T))
{
file = luax_checktype<File>(L, 1, "File", FILESYSTEM_FILE_T);
lua_pushnumber(L, 0); // File position.
luax_pushboolean(L, file->getMode() != File::CLOSED); // Save current file mode.
}
else
return luaL_error(L, "Expected File.");
if (file->getMode() != File::READ)
{
if (file->getMode() != File::CLOSED)
file->close();
try
{
if (!file->open(File::READ))
return luaL_error(L, "Could not open file.");
}
catch(love::Exception &e)
{
return luaL_error(L, "%s", e.what());
}
}
lua_pushcclosure(L, Filesystem::lines_i, 3);
return 1;
}
static const luaL_Reg functions[] =
{
{ "getSize", w_File_getSize },
{ "open", w_File_open },
{ "close", w_File_close },
{ "read", w_File_read },
{ "write", w_File_write },
{ "eof", w_File_eof },
{ "tell", w_File_tell },
{ "seek", w_File_seek },
{ "lines", w_File_lines },
{ 0, 0 }
};
extern "C" int luaopen_file(lua_State *L)
{
return luax_register_type(L, "File", functions);
}
} // physfs
} // filesystem
} // love
+52 -50
View File
@@ -1,50 +1,52 @@
/**
* Copyright (c) 2006-2012 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_FILESYSTEM_PHYSFS_WRAP_FILE_H
#define LOVE_FILESYSTEM_PHYSFS_WRAP_FILE_H
// LOVE
#include <common/runtime.h>
#include "Filesystem.h"
#include "File.h"
namespace love
{
namespace filesystem
{
namespace physfs
{
File * luax_checkfile(lua_State * L, int idx);
int w_File_getSize(lua_State * L);
int w_File_open(lua_State * L);
int w_File_close(lua_State * L);
int w_File_read(lua_State * L);
int w_File_write(lua_State * L);
int w_File_eof(lua_State * L);
int w_File_tell(lua_State * L);
int w_File_seek(lua_State * L);
int w_File_lines(lua_State * L);
extern "C" int luaopen_file(lua_State * L);
} // physfs
} // filesystem
} // love
#endif // LOVE_FILESYSTEM_PHYSFS_WRAP_FILE_H
/**
* Copyright (c) 2006-2012 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_FILESYSTEM_PHYSFS_WRAP_FILE_H
#define LOVE_FILESYSTEM_PHYSFS_WRAP_FILE_H
// LOVE
#include "common/runtime.h"
#include "Filesystem.h"
#include "File.h"
namespace love
{
namespace filesystem
{
namespace physfs
{
File *luax_checkfile(lua_State *L, int idx);
int w_File_getSize(lua_State *L);
int w_File_open(lua_State *L);
int w_File_close(lua_State *L);
int w_File_read(lua_State *L);
int w_File_write(lua_State *L);
int w_File_eof(lua_State *L);
int w_File_tell(lua_State *L);
int w_File_seek(lua_State *L);
int w_File_lines(lua_State *L);
extern "C" int luaopen_file(lua_State *L);
} // physfs
} // filesystem
} // love
#endif // LOVE_FILESYSTEM_PHYSFS_WRAP_FILE_H
+71 -69
View File
@@ -1,69 +1,71 @@
/**
* Copyright (c) 2006-2012 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "wrap_FileData.h"
#include <common/wrap_Data.h>
namespace love
{
namespace filesystem
{
namespace physfs
{
FileData * luax_checkfiledata(lua_State * L, int idx)
{
return luax_checktype<FileData>(L, idx, "FileData", FILESYSTEM_FILE_DATA_T);
}
int w_FileData_getFilename(lua_State * L)
{
FileData * t = luax_checkfiledata(L, 1);
lua_pushstring(L, t->getFilename().c_str());
return 1;
}
int w_FileData_getExtension(lua_State * L)
{
FileData * t = luax_checkfiledata(L, 1);
lua_pushstring(L, t->getExtension().c_str());
return 1;
}
static const luaL_Reg w_FileData_functions[] = {
// Data
{ "getPointer", w_Data_getPointer },
{ "getSize", w_Data_getSize },
{ "getFilename", w_FileData_getFilename },
{ "getExtension", w_FileData_getExtension },
{ 0, 0 }
};
extern "C" int luaopen_filedata(lua_State * L)
{
return luax_register_type(L, "FileData", w_FileData_functions);
}
} // physfs
} // filesystem
} // love
/**
* Copyright (c) 2006-2012 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "wrap_FileData.h"
#include "common/wrap_Data.h"
namespace love
{
namespace filesystem
{
namespace physfs
{
FileData *luax_checkfiledata(lua_State *L, int idx)
{
return luax_checktype<FileData>(L, idx, "FileData", FILESYSTEM_FILE_DATA_T);
}
int w_FileData_getFilename(lua_State *L)
{
FileData *t = luax_checkfiledata(L, 1);
lua_pushstring(L, t->getFilename().c_str());
return 1;
}
int w_FileData_getExtension(lua_State *L)
{
FileData *t = luax_checkfiledata(L, 1);
lua_pushstring(L, t->getExtension().c_str());
return 1;
}
static const luaL_Reg w_FileData_functions[] =
{
// Data
{ "getPointer", w_Data_getPointer },
{ "getSize", w_Data_getSize },
{ "getFilename", w_FileData_getFilename },
{ "getExtension", w_FileData_getExtension },
{ 0, 0 }
};
extern "C" int luaopen_filedata(lua_State *L)
{
return luax_register_type(L, "FileData", w_FileData_functions);
}
} // physfs
} // filesystem
} // love
+45 -43
View File
@@ -1,43 +1,45 @@
/**
* Copyright (c) 2006-2012 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_FILESYSTEM_PHYSFS_WRAP_FILE_DATA_H
#define LOVE_FILESYSTEM_PHYSFS_WRAP_FILE_DATA_H
// LOVE
#include <common/runtime.h>
#include <filesystem/FileData.h>
namespace love
{
namespace filesystem
{
namespace physfs
{
FileData * luax_checkfiledata(lua_State * L, int idx);
int w_FileData_getFilename(lua_State * L);
int w_FileData_getExtension(lua_State * L);
extern "C" int luaopen_filedata(lua_State * L);
} // physfs
} // filesystem
} // love
#endif // LOVE_FILESYSTEM_PHYSFS_WRAP_FILE_DATA_H
/**
* Copyright (c) 2006-2012 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_FILESYSTEM_PHYSFS_WRAP_FILE_DATA_H
#define LOVE_FILESYSTEM_PHYSFS_WRAP_FILE_DATA_H
// LOVE
#include "common/runtime.h"
#include "filesystem/FileData.h"
namespace love
{
namespace filesystem
{
namespace physfs
{
FileData *luax_checkfiledata(lua_State *L, int idx);
int w_FileData_getFilename(lua_State *L);
int w_FileData_getExtension(lua_State *L);
extern "C" int luaopen_filedata(lua_State *L);
} // physfs
} // filesystem
} // love
#endif // LOVE_FILESYSTEM_PHYSFS_WRAP_FILE_DATA_H
+443 -440
View File
@@ -1,440 +1,443 @@
/**
* Copyright (c) 2006-2012 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
// LOVE
#include "wrap_Filesystem.h"
namespace love
{
namespace filesystem
{
namespace physfs
{
static Filesystem * instance = 0;
bool hack_setupWriteDirectory()
{
if (instance != 0)
return instance->setupWriteDirectory();
return false;
}
int w_init(lua_State * L)
{
const char * arg0 = luaL_checkstring(L, 1);
try
{
instance->init(arg0);
}
catch (Exception & e)
{
return luaL_error(L, e.what());
}
return 0;
}
int w_setRelease(lua_State * L)
{
// no error checking needed, everything, even nothing
// can be converted to a boolean
instance->setRelease(luax_toboolean(L, 1));
return 0;
}
int w_setIdentity(lua_State * L)
{
const char * arg = luaL_checkstring(L, 1);
if (!instance->setIdentity(arg))
return luaL_error(L, "Could not set write directory.");
return 0;
}
int w_getIdentity(lua_State * L)
{
lua_pushstring(L, instance->getIdentity());
return 1;
}
int w_setSource(lua_State * L)
{
const char * arg = luaL_checkstring(L, 1);
if (!instance->setSource(arg))
return luaL_error(L, "Could not set source.");
return 0;
}
int w_newFile(lua_State * L)
{
const char * filename = luaL_checkstring(L, 1);
File * t;
try
{
t = instance->newFile(filename);
}
catch (Exception e)
{
return luaL_error(L, e.what());
}
luax_newtype(L, "File", FILESYSTEM_FILE_T, (void*)t);
return 1;
}
int w_newFileData(lua_State * L)
{
if (!lua_isstring(L, 1))
return luaL_error(L, "String expected.");
if (!lua_isstring(L, 2))
return luaL_error(L, "String expected.");
size_t length = 0;
const char * str = lua_tolstring(L, 1, &length);
const char * filename = lua_tostring(L, 2);
const char * decstr = lua_isstring(L, 3) ? lua_tostring(L, 3) : 0;
FileData::Decoder decoder = FileData::FILE;
if (decstr)
FileData::getConstant(decstr, decoder);
FileData * t = 0;
switch(decoder)
{
case FileData::FILE:
t = instance->newFileData((void*)str, (int)length, filename);
break;
case FileData::BASE64:
t = instance->newFileData(str, filename);
break;
default:
return luaL_error(L, "Unrecognized FileData decoder: %s", decstr);
}
luax_newtype(L, "FileData", FILESYSTEM_FILE_DATA_T, (void*)t);
return 1;
}
int w_getWorkingDirectory(lua_State * L)
{
lua_pushstring(L, instance->getWorkingDirectory());
return 1;
}
int w_getUserDirectory(lua_State * L)
{
lua_pushstring(L, instance->getUserDirectory());
return 1;
}
int w_getAppdataDirectory(lua_State * L)
{
lua_pushstring(L, instance->getAppdataDirectory());
return 1;
}
int w_getSaveDirectory(lua_State * L)
{
lua_pushstring(L, instance->getSaveDirectory());
return 1;
}
int w_exists(lua_State * L)
{
const char * arg = luaL_checkstring(L, 1);
lua_pushboolean(L, instance->exists(arg) ? 1 : 0);
return 1;
}
int w_isDirectory(lua_State * L)
{
const char * arg = luaL_checkstring(L, 1);
lua_pushboolean(L, instance->isDirectory(arg) ? 1 : 0);
return 1;
}
int w_isFile(lua_State * L)
{
const char * arg = luaL_checkstring(L, 1);
lua_pushboolean(L, instance->isFile(arg) ? 1 : 0);
return 1;
}
int w_mkdir(lua_State * L)
{
const char * arg = luaL_checkstring(L, 1);
lua_pushboolean(L, instance->mkdir(arg) ? 1 : 0);
return 1;
}
int w_remove(lua_State * L)
{
const char * arg = luaL_checkstring(L, 1);
lua_pushboolean(L, instance->remove(arg) ? 1 : 0);
return 1;
}
int w_read(lua_State * L)
{
try
{
return instance->read(L);
}
catch (Exception e)
{
return luaL_error(L, e.what());
}
}
int w_write(lua_State * L)
{
try
{
return instance->write(L);
}
catch (Exception e)
{
return luaL_error(L, e.what());
}
}
int w_enumerate(lua_State * L)
{
return instance->enumerate(L);
}
int w_lines(lua_State * L)
{
File * file;
if(lua_isstring(L, 1))
{
file = instance->newFile(lua_tostring(L, 1));
try
{
if (!file->open(File::READ))
return luaL_error(L, "Could not open file.");
}
catch (love::Exception & e)
{
return luaL_error(L, "%s", e.what());
}
luax_newtype(L, "File", FILESYSTEM_FILE_T, file);
}
else
return luaL_error(L, "Expected filename.");
lua_pushcclosure(L, Filesystem::lines_i, 1);
return 1;
}
int w_load(lua_State * L)
{
try
{
return instance->load(L);
}
catch (love::Exception & e)
{
return luaL_error(L, e.what());
}
}
int w_getLastModified(lua_State * L)
{
return instance->getLastModified(L);
}
int loader(lua_State * L)
{
const char * filename = lua_tostring(L, -1);
std::string tmp(filename);
tmp += ".lua";
int size = tmp.size();
for (int i=0;i<size-4;i++)
{
if (tmp[i] == '.')
{
tmp[i] = '/';
}
}
// Check whether file exists.
if (instance->exists(tmp.c_str()))
{
lua_pop(L, 1);
lua_pushstring(L, tmp.c_str());
// Ok, load it.
return instance->load(L);
}
tmp = filename;
size = tmp.size();
for (int i=0;i<size;i++)
{
if (tmp[i] == '.')
{
tmp[i] = '/';
}
}
if (instance->isDirectory(tmp.c_str()))
{
tmp += "/init.lua";
if (instance->exists(tmp.c_str()))
{
lua_pop(L, 1);
lua_pushstring(L, tmp.c_str());
// Ok, load it.
return instance->load(L);
}
}
lua_pushfstring(L, "\n\tno file \"%s\" in LOVE game directories.\n", (tmp + ".lua").c_str());
return 1;
}
inline const char * library_extension()
{
#ifdef LOVE_WINDOWS
return ".dll";
#else
return ".so";
#endif
}
int extloader(lua_State * L)
{
const char * filename = lua_tostring(L, -1);
std::string tokenized_name(filename);
std::string tokenized_function(filename);
for (unsigned int i = 0; i < tokenized_name.size(); i++)
{
if (tokenized_name[i] == '.')
{
tokenized_name[i] = '/';
tokenized_function[i] = '_';
}
}
tokenized_name += library_extension();
void * handle = SDL_LoadObject((std::string(instance->getAppdataDirectory()) + LOVE_PATH_SEPARATOR LOVE_APPDATA_FOLDER LOVE_PATH_SEPARATOR + tokenized_name).c_str());
if (!handle && instance->isRelease())
handle = SDL_LoadObject((std::string(instance->getSaveDirectory()) + LOVE_PATH_SEPARATOR + tokenized_name).c_str());
if (!handle)
{
lua_pushfstring(L, "\n\tno extension \"%s\" in LOVE paths.\n", filename);
return 1;
}
void * func = SDL_LoadFunction(handle, ("loveopen_" + tokenized_function).c_str());
if (!func)
func = SDL_LoadFunction(handle, ("luaopen_" + tokenized_function).c_str());
if (!func)
{
SDL_UnloadObject(handle);
lua_pushfstring(L, "\n\textension \"%s\" is incompatible.\n", filename);
return 1;
}
lua_pushcfunction(L, (lua_CFunction) func);
return 1;
}
// List of functions to wrap.
static const luaL_Reg functions[] = {
{ "init", w_init },
{ "setRelease", w_setRelease },
{ "setIdentity", w_setIdentity },
{ "getIdentity", w_getIdentity },
{ "setSource", w_setSource },
{ "newFile", w_newFile },
{ "getWorkingDirectory", w_getWorkingDirectory },
{ "getUserDirectory", w_getUserDirectory },
{ "getAppdataDirectory", w_getAppdataDirectory },
{ "getSaveDirectory", w_getSaveDirectory },
{ "exists", w_exists },
{ "isDirectory", w_isDirectory },
{ "isFile", w_isFile },
{ "mkdir", w_mkdir },
{ "remove", w_remove },
{ "read", w_read },
{ "write", w_write },
{ "enumerate", w_enumerate },
{ "lines", w_lines },
{ "load", w_load },
{ "getLastModified", w_getLastModified },
{ "newFileData", w_newFileData },
{ 0, 0 }
};
static const lua_CFunction types[] = {
luaopen_file,
luaopen_filedata,
0
};
extern "C" int luaopen_love_filesystem(lua_State * L)
{
if (instance == 0)
{
try
{
instance = new Filesystem();
love::luax_register_searcher(L, loader, 1);
love::luax_register_searcher(L, extloader, 2);
}
catch (Exception & e)
{
return luaL_error(L, e.what());
}
}
else
{
instance->retain();
love::luax_register_searcher(L, loader, 1);
love::luax_register_searcher(L, extloader, 2);
}
WrappedModule w;
w.module = instance;
w.name = "filesystem";
w.flags = MODULE_FILESYSTEM_T;
w.functions = functions;
w.types = types;
return luax_register_module(L, w);
}
} // physfs
} // filesystem
} // love
/**
* Copyright (c) 2006-2012 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
// LOVE
#include "wrap_Filesystem.h"
namespace love
{
namespace filesystem
{
namespace physfs
{
static Filesystem *instance = 0;
bool hack_setupWriteDirectory()
{
if (instance != 0)
return instance->setupWriteDirectory();
return false;
}
int w_init(lua_State *L)
{
const char *arg0 = luaL_checkstring(L, 1);
try
{
instance->init(arg0);
}
catch(Exception &e)
{
return luaL_error(L, e.what());
}
return 0;
}
int w_setRelease(lua_State *L)
{
// no error checking needed, everything, even nothing
// can be converted to a boolean
instance->setRelease(luax_toboolean(L, 1));
return 0;
}
int w_setIdentity(lua_State *L)
{
const char *arg = luaL_checkstring(L, 1);
if (!instance->setIdentity(arg))
return luaL_error(L, "Could not set write directory.");
return 0;
}
int w_getIdentity(lua_State *L)
{
lua_pushstring(L, instance->getIdentity());
return 1;
}
int w_setSource(lua_State *L)
{
const char *arg = luaL_checkstring(L, 1);
if (!instance->setSource(arg))
return luaL_error(L, "Could not set source.");
return 0;
}
int w_newFile(lua_State *L)
{
const char *filename = luaL_checkstring(L, 1);
File *t;
try
{
t = instance->newFile(filename);
}
catch(Exception e)
{
return luaL_error(L, e.what());
}
luax_newtype(L, "File", FILESYSTEM_FILE_T, (void *)t);
return 1;
}
int w_newFileData(lua_State *L)
{
if (!lua_isstring(L, 1))
return luaL_error(L, "String expected.");
if (!lua_isstring(L, 2))
return luaL_error(L, "String expected.");
size_t length = 0;
const char *str = lua_tolstring(L, 1, &length);
const char *filename = lua_tostring(L, 2);
const char *decstr = lua_isstring(L, 3) ? lua_tostring(L, 3) : 0;
FileData::Decoder decoder = FileData::FILE;
if (decstr)
FileData::getConstant(decstr, decoder);
FileData *t = 0;
switch (decoder)
{
case FileData::FILE:
t = instance->newFileData((void *)str, (int)length, filename);
break;
case FileData::BASE64:
t = instance->newFileData(str, filename);
break;
default:
return luaL_error(L, "Unrecognized FileData decoder: %s", decstr);
}
luax_newtype(L, "FileData", FILESYSTEM_FILE_DATA_T, (void *)t);
return 1;
}
int w_getWorkingDirectory(lua_State *L)
{
lua_pushstring(L, instance->getWorkingDirectory());
return 1;
}
int w_getUserDirectory(lua_State *L)
{
lua_pushstring(L, instance->getUserDirectory());
return 1;
}
int w_getAppdataDirectory(lua_State *L)
{
lua_pushstring(L, instance->getAppdataDirectory());
return 1;
}
int w_getSaveDirectory(lua_State *L)
{
lua_pushstring(L, instance->getSaveDirectory());
return 1;
}
int w_exists(lua_State *L)
{
const char *arg = luaL_checkstring(L, 1);
lua_pushboolean(L, instance->exists(arg) ? 1 : 0);
return 1;
}
int w_isDirectory(lua_State *L)
{
const char *arg = luaL_checkstring(L, 1);
lua_pushboolean(L, instance->isDirectory(arg) ? 1 : 0);
return 1;
}
int w_isFile(lua_State *L)
{
const char *arg = luaL_checkstring(L, 1);
lua_pushboolean(L, instance->isFile(arg) ? 1 : 0);
return 1;
}
int w_mkdir(lua_State *L)
{
const char *arg = luaL_checkstring(L, 1);
lua_pushboolean(L, instance->mkdir(arg) ? 1 : 0);
return 1;
}
int w_remove(lua_State *L)
{
const char *arg = luaL_checkstring(L, 1);
lua_pushboolean(L, instance->remove(arg) ? 1 : 0);
return 1;
}
int w_read(lua_State *L)
{
try
{
return instance->read(L);
}
catch(Exception e)
{
return luaL_error(L, e.what());
}
}
int w_write(lua_State *L)
{
try
{
return instance->write(L);
}
catch(Exception e)
{
return luaL_error(L, e.what());
}
}
int w_enumerate(lua_State *L)
{
return instance->enumerate(L);
}
int w_lines(lua_State *L)
{
File *file;
if (lua_isstring(L, 1))
{
file = instance->newFile(lua_tostring(L, 1));
try
{
if (!file->open(File::READ))
return luaL_error(L, "Could not open file.");
}
catch(love::Exception &e)
{
return luaL_error(L, "%s", e.what());
}
luax_newtype(L, "File", FILESYSTEM_FILE_T, file);
}
else
return luaL_error(L, "Expected filename.");
lua_pushcclosure(L, Filesystem::lines_i, 1);
return 1;
}
int w_load(lua_State *L)
{
try
{
return instance->load(L);
}
catch(love::Exception &e)
{
return luaL_error(L, e.what());
}
}
int w_getLastModified(lua_State *L)
{
return instance->getLastModified(L);
}
int loader(lua_State *L)
{
const char *filename = lua_tostring(L, -1);
std::string tmp(filename);
tmp += ".lua";
int size = tmp.size();
for (int i=0; i<size-4; i++)
{
if (tmp[i] == '.')
{
tmp[i] = '/';
}
}
// Check whether file exists.
if (instance->exists(tmp.c_str()))
{
lua_pop(L, 1);
lua_pushstring(L, tmp.c_str());
// Ok, load it.
return instance->load(L);
}
tmp = filename;
size = tmp.size();
for (int i=0; i<size; i++)
{
if (tmp[i] == '.')
{
tmp[i] = '/';
}
}
if (instance->isDirectory(tmp.c_str()))
{
tmp += "/init.lua";
if (instance->exists(tmp.c_str()))
{
lua_pop(L, 1);
lua_pushstring(L, tmp.c_str());
// Ok, load it.
return instance->load(L);
}
}
lua_pushfstring(L, "\n\tno file \"%s\" in LOVE game directories.\n", (tmp + ".lua").c_str());
return 1;
}
inline const char *library_extension()
{
#ifdef LOVE_WINDOWS
return ".dll";
#else
return ".so";
#endif
}
int extloader(lua_State *L)
{
const char *filename = lua_tostring(L, -1);
std::string tokenized_name(filename);
std::string tokenized_function(filename);
for (unsigned int i = 0; i < tokenized_name.size(); i++)
{
if (tokenized_name[i] == '.')
{
tokenized_name[i] = '/';
tokenized_function[i] = '_';
}
}
tokenized_name += library_extension();
void *handle = SDL_LoadObject((std::string(instance->getAppdataDirectory()) + LOVE_PATH_SEPARATOR LOVE_APPDATA_FOLDER LOVE_PATH_SEPARATOR + tokenized_name).c_str());
if (!handle && instance->isRelease())
handle = SDL_LoadObject((std::string(instance->getSaveDirectory()) + LOVE_PATH_SEPARATOR + tokenized_name).c_str());
if (!handle)
{
lua_pushfstring(L, "\n\tno extension \"%s\" in LOVE paths.\n", filename);
return 1;
}
void *func = SDL_LoadFunction(handle, ("loveopen_" + tokenized_function).c_str());
if (!func)
func = SDL_LoadFunction(handle, ("luaopen_" + tokenized_function).c_str());
if (!func)
{
SDL_UnloadObject(handle);
lua_pushfstring(L, "\n\textension \"%s\" is incompatible.\n", filename);
return 1;
}
lua_pushcfunction(L, (lua_CFunction) func);
return 1;
}
// List of functions to wrap.
static const luaL_Reg functions[] =
{
{ "init", w_init },
{ "setRelease", w_setRelease },
{ "setIdentity", w_setIdentity },
{ "getIdentity", w_getIdentity },
{ "setSource", w_setSource },
{ "newFile", w_newFile },
{ "getWorkingDirectory", w_getWorkingDirectory },
{ "getUserDirectory", w_getUserDirectory },
{ "getAppdataDirectory", w_getAppdataDirectory },
{ "getSaveDirectory", w_getSaveDirectory },
{ "exists", w_exists },
{ "isDirectory", w_isDirectory },
{ "isFile", w_isFile },
{ "mkdir", w_mkdir },
{ "remove", w_remove },
{ "read", w_read },
{ "write", w_write },
{ "enumerate", w_enumerate },
{ "lines", w_lines },
{ "load", w_load },
{ "getLastModified", w_getLastModified },
{ "newFileData", w_newFileData },
{ 0, 0 }
};
static const lua_CFunction types[] =
{
luaopen_file,
luaopen_filedata,
0
};
extern "C" int luaopen_love_filesystem(lua_State *L)
{
if (instance == 0)
{
try
{
instance = new Filesystem();
love::luax_register_searcher(L, loader, 1);
love::luax_register_searcher(L, extloader, 2);
}
catch(Exception &e)
{
return luaL_error(L, e.what());
}
}
else
{
instance->retain();
love::luax_register_searcher(L, loader, 1);
love::luax_register_searcher(L, extloader, 2);
}
WrappedModule w;
w.module = instance;
w.name = "filesystem";
w.flags = MODULE_FILESYSTEM_T;
w.functions = functions;
w.types = types;
return luax_register_module(L, w);
}
} // physfs
} // filesystem
} // love
+75 -74
View File
@@ -1,74 +1,75 @@
/**
* Copyright (c) 2006-2012 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_FILESYSTEM_PHYSFS_WRAP_FILESYSTEM_H
#define LOVE_FILESYSTEM_PHYSFS_WRAP_FILESYSTEM_H
// LOVE
#include "Filesystem.h"
#include "wrap_File.h"
#include "wrap_FileData.h"
// SDL
#include <SDL_loadso.h>
namespace love
{
namespace filesystem
{
namespace physfs
{
bool hack_setupWriteDirectory();
int w_init(lua_State * L);
int w_setRelease(lua_State * L);
int w_setIdentity(lua_State * L);
int w_getIdentity(lua_State * L);
int w_setSource(lua_State * L);
int w_newFile(lua_State * L);
int w_newFileData(lua_State * L);
int w_getWorkingDirectory(lua_State * L);
int w_getUserDirectory(lua_State * L);
int w_getAppdataDirectory(lua_State * L);
int w_getSaveDirectory(lua_State * L);
int w_exists(lua_State * L);
int w_isDirectory(lua_State * L);
int w_isFile(lua_State * L);
int w_mkdir(lua_State * L);
int w_remove(lua_State * L);
int w_open(lua_State * L);
int w_close(lua_State * L);
int w_read(lua_State * L);
int w_write(lua_State * L);
int w_eof(lua_State * L);
int w_tell(lua_State * L);
int w_seek(lua_State * L);
int w_enumerate(lua_State * L);
int w_lines(lua_State * L);
int w_load(lua_State * L);
int w_getLastModified(lua_State * L);
int loader(lua_State * L);
int extloader(lua_State * L);
extern "C" LOVE_EXPORT int luaopen_love_filesystem(lua_State * L);
} // physfs
} // filesystem
} // love
#endif // LOVE_FILESYSTEM_PHYSFS_WRAP_FILESYSTEM_H
/**
* Copyright (c) 2006-2012 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_FILESYSTEM_PHYSFS_WRAP_FILESYSTEM_H
#define LOVE_FILESYSTEM_PHYSFS_WRAP_FILESYSTEM_H
// LOVE
#include "Filesystem.h"
#include "wrap_File.h"
#include "wrap_FileData.h"
// SDL
#include <SDL_loadso.h>
namespace love
{
namespace filesystem
{
namespace physfs
{
bool hack_setupWriteDirectory();
int w_init(lua_State *L);
int w_setRelease(lua_State *L);
int w_setIdentity(lua_State *L);
int w_getIdentity(lua_State *L);
int w_setSource(lua_State *L);
int w_newFile(lua_State *L);
int w_newFileData(lua_State *L);
int w_getWorkingDirectory(lua_State *L);
int w_getUserDirectory(lua_State *L);
int w_getAppdataDirectory(lua_State *L);
int w_getSaveDirectory(lua_State *L);
int w_exists(lua_State *L);
int w_isDirectory(lua_State *L);
int w_isFile(lua_State *L);
int w_mkdir(lua_State *L);
int w_remove(lua_State *L);
int w_open(lua_State *L);
int w_close(lua_State *L);
int w_read(lua_State *L);
int w_write(lua_State *L);
int w_eof(lua_State *L);
int w_tell(lua_State *L);
int w_seek(lua_State *L);
int w_enumerate(lua_State *L);
int w_lines(lua_State *L);
int w_load(lua_State *L);
int w_getLastModified(lua_State *L);
int loader(lua_State *L);
int extloader(lua_State *L);
extern "C" LOVE_EXPORT int luaopen_love_filesystem(lua_State *L);
} // physfs
} // filesystem
} // love
#endif // LOVE_FILESYSTEM_PHYSFS_WRAP_FILESYSTEM_H