src/tools/tResourceManager.cpp File Reference

#include "aa_config.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <libxml/nanohttp.h>
#include "tConfiguration.h"
#include "tDirectories.h"
#include "tResourceManager.h"
#include "tString.h"

Include dependency graph for tResourceManager.cpp:

Go to the source code of this file.

Functions

static int myHTTPFetch (const char *URI, const char *filename, const char *savepath)
static int myFetch (const char *URIs, const char *filename, const char *savepath)
static void RInclude (std::istream &s)
static bool st_checkAuthor (tString const &Author)
static bool st_checkCategory (tString const &Category)
static bool st_checkName (tString const &Name)
static bool st_checkExtension (tString const &Extension)
static bool st_checkType (tString const &Type)
static bool st_checkVersion (tString const &Version)

Variables

static tSettingItem< tStringconf_res_repo ("RESOURCE_REPOSITORY_CLIENT", tResourceManager::AccessRepoClient())
static tConfItemFunc s_RInclude ("RINCLUDE",&RInclude)


Function Documentation

static int myFetch ( const char *  URIs,
const char *  filename,
const char *  savepath 
) [static]

Definition at line 98 of file tResourceManager.cpp.

References free, malloc, myHTTPFetch(), and rv.

Referenced by tResourceManager::locateResource().

00098                                                                                  {
00099     const char *r = URIs, *p, *n;
00100     char *u;
00101     size_t len;
00102     int rv = -1;
00103     // r = unprocessed data             p = end-of-item + 1             u = item
00104     // n = to-be r                              len = length of item    savepath = result filepath
00105 
00106     while (r[0] != '\0') {
00107         while (r[0] == ' ') ++r;                        // skip spaces at the start of the item
00108         (p = strchr(r, ';')) ? 0 : (p = strchr(r, '\0'));
00109         n = (p[0] == '\0') ? p : (p + 1);       // next item starts after the semicolon
00110         // NOTE: skip semicolons, *NOT* nulls
00111         while (p[-1] == ' ') --p;                       // skip spaces at the end of the item
00112         len = (size_t)(p - r);
00113         if (len > 0) {                                          // skip this for null-length items
00114             u = (char*)malloc((len + 1) * sizeof(char));
00115             strncpy(u, r, len);
00116             u[len] = '\0';                                      // u now contains the individual URI
00117             rv = myHTTPFetch(u, filename, savepath);    // TODO: handle other protocols?
00118             free(u);
00119             if (rv == 0) return 0;              // If successful, return the file retrieved
00120         }
00121         r = n;                                                          // move onto the next item
00122     }
00123 
00124     return rv;  // last error
00125 }

Here is the call graph for this function:

Here is the caller graph for this function:

static int myHTTPFetch ( const char *  URI,
const char *  filename,
const char *  savepath 
) [static]

Definition at line 53 of file tResourceManager.cpp.

References con, fd, free, malloc, and NULL.

Referenced by myFetch().

00054 {
00055     void *ctxt = NULL;
00056     char *buf = NULL;
00057     FILE* fd;
00058     int len, rc;
00059 
00060     con << tOutput( "$resource_downloading", URI );
00061     // con << "Downloading " << URI << "...\n";
00062 
00063     ctxt = xmlNanoHTTPOpen(URI, NULL);
00064     if (ctxt == NULL) {
00065         con << "ERROR: ctxt is NULL\n";
00066         return 1;
00067     }
00068 
00069     if ( (rc = xmlNanoHTTPReturnCode(ctxt)) != 200 ) {
00070         con << tOutput( rc == 404 ? "$resource_fetcherror_404" : "$resource_fetcherror", rc );
00071         return 2;
00072     }
00073 
00074     fd = fopen(savepath, "wb");
00075     if (fd < 0) {
00076         xmlNanoHTTPClose(ctxt);
00077         con << tOutput( "$resource_no_write", savepath );
00078         return 3;
00079     }
00080 
00081     //xmlNanoHTTPFetchContent( ctxt, &buf, &len );
00082     int maxlen = 10000;
00083     buf = (char*)malloc(maxlen);
00084     while( (len = xmlNanoHTTPRead(ctxt, buf, maxlen)) > 0 ) {
00085         fwrite(buf, len, 1, fd);
00086     }
00087     free(buf);
00088 
00089     xmlNanoHTTPClose(ctxt);
00090     fclose(fd);
00091 
00092 
00093     con << "OK\n";
00094 
00095     return 0;
00096 }

Here is the caller graph for this function:

static void RInclude ( std::istream &  s  )  [static]

Definition at line 228 of file tResourceManager.cpp.

References con, tConfItemBase::LoadAll(), tResourceManager::locateResource(), and NULL.

00229 {
00230     tString file;
00231     s >> file;
00232 
00233     tString rclcl = tResourceManager::locateResource(NULL, file);
00234     if ( rclcl ) {
00235         std::ifstream rc(rclcl);
00236         tConfItemBase::LoadAll(rc);
00237         return;
00238     }
00239 
00240     con << tOutput( "$config_rinclude_not_found", file );
00241 }

Here is the call graph for this function:

static bool st_checkAuthor ( tString const &  Author  )  [static]

Definition at line 245 of file tResourceManager.cpp.

References tERR_WARN.

Referenced by tResourcePath::tResourcePath().

00245                                                   {
00246     if(Author.empty() || Author[0] < 'A' || Author[0] > 'z' || Author[0] > 'Z' && Author[0] < 'a' || Author.find('/') != tString::npos) {
00247         tERR_WARN("Resource authors must start with a letter and may not contain slashes");
00248         return false;
00249     }
00250     return true;
00251 }

Here is the caller graph for this function:

static bool st_checkCategory ( tString const &  Category  )  [static]

Definition at line 252 of file tResourceManager.cpp.

References tERR_WARN.

Referenced by tResourcePath::tResourcePath().

00252                                                       {
00253     if(Category[0] == '/' || *Category.rbegin() == '/' || Category.find("/.") != tString::npos) {
00254         tERR_WARN("Resource categories must not start or end with a slash or dot or contain the sequence \"./\".");
00255         return false;
00256     }
00257     return true;
00258 }

Here is the caller graph for this function:

static bool st_checkExtension ( tString const &  Extension  )  [static]

Definition at line 266 of file tResourceManager.cpp.

References tERR_WARN.

Referenced by tResourcePath::tResourcePath().

00266                                                         {
00267     if(Extension.empty() || Extension.find_first_of("/.") != tString::npos) {
00268         tERR_WARN("Resource extensions must not contain slashes or dots");
00269         return false;
00270     }
00271     return true;
00272 }

Here is the caller graph for this function:

static bool st_checkName ( tString const &  Name  )  [static]

Definition at line 259 of file tResourceManager.cpp.

References tERR_WARN.

Referenced by tResourcePath::tResourcePath().

00259                                               {
00260     if(Name.empty() || Name[0] == '.' || Name.find_first_of("-/") != tString::npos) {
00261         tERR_WARN("Resource names must not start with a dot or contain slashes or minus signs");
00262         return false;
00263     }
00264     return true;
00265 }

Here is the caller graph for this function:

static bool st_checkType ( tString const &  Type  )  [static]

Definition at line 273 of file tResourceManager.cpp.

References tERR_WARN.

Referenced by tResourcePath::tResourcePath().

00273                                               {
00274     if(Type.empty() || Type.find_first_of("/.") != tString::npos) {
00275         tERR_WARN("Resource types must not contain slashes or dots");
00276         return false;
00277     }
00278     return true;
00279 }

Here is the caller graph for this function:

static bool st_checkVersion ( tString const &  Version  )  [static]

Definition at line 280 of file tResourceManager.cpp.

References tERR_WARN.

Referenced by tResourcePath::tResourcePath().

00280                                                     {
00281     if(Version.empty() || Version.find('/') != tString::npos) {
00282         tERR_WARN("Resource versions must not contain slashes");
00283         return false;
00284     }
00285     return true;
00286 }

Here is the caller graph for this function:


Variable Documentation

tSettingItem<tString> conf_res_repo("RESOURCE_REPOSITORY_CLIENT", tResourceManager::AccessRepoClient()) [static]

tConfItemFunc s_RInclude("RINCLUDE",&RInclude) [static]


Generated on Sat Mar 15 23:12:28 2008 for Armagetron Advanced by  doxygen 1.5.4