#include <list>
#include <physfs.h>
#include <sys/stat.h>
+#include <stdio.h>
#include "addon_manager.hpp"
#include "config.h"
#include "log.hpp"
#ifdef HAVE_LIBCURL
namespace {
- size_t my_curl_string_append(void *ptr, size_t size, size_t nmemb, void *string_ptr)
+ size_t my_curl_string_append(void *ptr, size_t size, size_t nmemb, void *string_ptr)
{
std::string& s = *static_cast<std::string*>(string_ptr);
std::string buf(static_cast<char*>(ptr), size * nmemb);
log_debug << "read " << size * nmemb << " bytes of data..." << std::endl;
return size * nmemb;
}
-
- size_t my_curl_physfs_write(void *ptr, size_t size, size_t nmemb, void *f_p)
+
+ size_t my_curl_physfs_write(void *ptr, size_t size, size_t nmemb, void *f_p)
{
PHYSFS_file* f = static_cast<PHYSFS_file*>(f_p);
PHYSFS_sint64 written = PHYSFS_write(f, ptr, size, nmemb);
}
#endif
-AddonManager&
+AddonManager&
AddonManager::get_instance()
{
static AddonManager instance;
curl_global_cleanup();
#endif
}
-
-std::vector<Addon>
+
+std::vector<Addon>
AddonManager::get_addons() const
{
std::vector<Addon> addons;
// make sure it looks like an archive
static const std::string archiveExt = ".zip";
if (fileName.compare(fileName.length()-archiveExt.length(), archiveExt.length(), archiveExt) != 0) continue;
-
+
// make sure it exists
struct stat stats;
if (stat(fileName.c_str(), &stats) != 0) continue;
-
+
// make sure it's an actual file
if (!S_ISREG(stats.st_mode)) continue;
std::string::size_type n = fileName.rfind(dirSep) + 1;
if (n == std::string::npos) n = 0;
std::string title = fileName.substr(n, fileName.length() - n - archiveExt.length());
-
+
Addon addon;
addon.title = title;
addon.fname = fileName;
addon.isInstalled = true;
- addons.push_back(addon);
+ addons.push_back(addon);
}
#ifdef HAVE_LIBCURL
static const std::string archiveExt = ".zip";
if (url.compare(url.length()-archiveExt.length(), archiveExt.length(), archiveExt) != 0) continue;
- // extract nice title
+ // extract nice title
std::string::size_type n = url.rfind('/') + 1;
if (n == std::string::npos) n = 0;
std::string title = url.substr(n, url.length() - n - archiveExt.length());
- // construct file name
+ // construct file name
std::string fname = url.substr(n);
// make sure it does not contain weird characters
addon.url = url;
addon.isInstalled = false;
- addons.push_back(addon);
+ addons.push_back(addon);
}
#endif
-
+
return addons;
}
void
-AddonManager::install(const Addon&
- #ifdef HAVE_LIBCURL
- addon
- #endif
- )
+AddonManager::install(const Addon& addon)
{
#ifdef HAVE_LIBCURL
PHYSFS_close(f);
free(url);
-
+
static const std::string writeDir = PHYSFS_getWriteDir();
static const std::string dirSep = PHYSFS_getDirSeparator();
std::string fullFilename = writeDir + dirSep + addon.fname;
log_debug << "Finished downloading \"" << fullFilename << "\"" << std::endl;
PHYSFS_addToSearchPath(fullFilename.c_str(), 1);
+#else
+ (void) addon;
#endif
}
-void
+void
AddonManager::remove(const Addon& addon)
{
PHYSFS_removeFromSearchPath(addon.fname.c_str());
- unlink(addon.fname.c_str());
+ PHYSFS_delete(addon.fname.c_str());
}
-