goat3d

changeset 31:4058337fbb92

texture filename cleanup when setting a texture to a material
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 30 Sep 2013 04:26:21 +0300
parents 0fe02696fb1e
children d24f63e8031e
files src/goat3d.cc
diffstat 1 files changed, 31 insertions(+), 1 deletions(-) [+]
line diff
     1.1 --- a/src/goat3d.cc	Sun Sep 29 23:05:44 2013 +0300
     1.2 +++ b/src/goat3d.cc	Mon Sep 30 04:26:21 2013 +0300
     1.3 @@ -1,9 +1,17 @@
     1.4  #include <string.h>
     1.5  #include <errno.h>
     1.6 +#include <ctype.h>
     1.7 +#include <string>
     1.8  #include "goat3d.h"
     1.9  #include "goat3d_impl.h"
    1.10  #include "log.h"
    1.11  
    1.12 +#ifndef _MSC_VER
    1.13 +#include <alloca.h>
    1.14 +#else
    1.15 +#include <malloc.h>
    1.16 +#endif
    1.17 +
    1.18  struct goat3d {
    1.19  	Scene *scn;
    1.20  	unsigned int flags;
    1.21 @@ -20,6 +28,8 @@
    1.22  static long write_file(const void *buf, size_t bytes, void *uptr);
    1.23  static long seek_file(long offs, int whence, void *uptr);
    1.24  
    1.25 +static std::string clean_filename(const char *str);
    1.26 +
    1.27  extern "C" {
    1.28  
    1.29  struct goat3d *goat3d_create(void)
    1.30 @@ -197,7 +207,7 @@
    1.31  
    1.32  void goat3d_set_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib, const char *mapname)
    1.33  {
    1.34 -	(*mtl)[attrib].map = std::string(mapname);
    1.35 +	(*mtl)[attrib].map = clean_filename(mapname);
    1.36  }
    1.37  
    1.38  const char *goat3d_get_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib)
    1.39 @@ -706,3 +716,23 @@
    1.40  	}
    1.41  	return ftell((FILE*)uptr);
    1.42  }
    1.43 +
    1.44 +static std::string clean_filename(const char *str)
    1.45 +{
    1.46 +	const char *last_slash = strrchr(str, '/');
    1.47 +	if(!last_slash) {
    1.48 +		last_slash = strrchr(str, '\\');
    1.49 +	}
    1.50 +
    1.51 +	if(last_slash) {
    1.52 +		str = last_slash + 1;
    1.53 +	}
    1.54 +
    1.55 +	char *buf = (char*)alloca(strlen(str) + 1);
    1.56 +	char *dest = buf;
    1.57 +	while(*str) {
    1.58 +		char c = *str++;
    1.59 +		*dest++ = tolower(c);
    1.60 +	}
    1.61 +	return buf;
    1.62 +}