+/*
+ Replaces the filename extension.
+ eg:
+ std::string image = "a/image.png"
+ replace_ext(image, "jpg")
+ -> image = "a/image.jpg"
+ Returns true on success.
+*/
+inline bool replace_ext(std::string &path, const char *ext)
+{
+ if(ext == NULL)
+ return false;
+ // Find place of last dot, fail if \ or / found.
+ s32 last_dot_i = -1;
+ for(s32 i=path.size()-1; i>=0; i--)
+ {
+ if(path[i] == '.')
+ {
+ last_dot_i = i;
+ break;
+ }
+
+ if(path[i] == '\\' || path[i] == '/')
+ break;
+ }
+ // If not found, return an empty string
+ if(last_dot_i == -1)
+ return false;
+ // Else make the new path
+ path = path.substr(0, last_dot_i+1) + ext;
+ return true;
+}
+
+/*
+ Find out the full path of an image by trying different filename
+ extensions.
+
+ If failed, return "".
+*/
+inline std::string getImagePath(std::string path)
+{
+ // A NULL-ended list of possible image extensions
+ const char *extensions[] = {
+ "png", "jpg", "bmp", "tga",
+ "pcx", "ppm", "psd", "wal", "rgb",
+ NULL
+ };
+
+ const char **ext = extensions;
+ do{
+ bool r = replace_ext(path, *ext);
+ if(r == false)
+ return "";
+ if(fs::PathExists(path))
+ return path;
+ }
+ while((++ext) != NULL);
+
+ return "";
+}
+