Skip to content

Commit 7d2ca5b

Browse files
authored
ofGetExtensionLower to uniformize file extension handling in OF Core (#8105)
1 parent 7f777da commit 7d2ca5b

File tree

8 files changed

+34
-20
lines changed

8 files changed

+34
-20
lines changed

addons/ofxGui/src/ofxBaseGui.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,17 +228,17 @@ float ofxBaseGui::getTextVCenteredInRect(const ofRectangle& container){
228228
}
229229

230230

231-
void ofxBaseGui::saveToFile(const std::string& filename){
232-
auto extension = ofToLower(ofFilePath::getFileExt(filename));
233-
if(extension == "xml"){
231+
void ofxBaseGui::saveToFile(const of::filesystem::path & filename){
232+
auto extension = ofGetExtensionLower(filename);
233+
if(extension == ".xml"){
234234
ofXml xml;
235235
if(ofFile(filename, ofFile::Reference).exists()){
236236
xml.load(filename);
237237
}
238238
saveTo(xml);
239239
xml.save(filename);
240240
}else
241-
if(extension == "json"){
241+
if(extension == ".json"){
242242
ofJson json = ofLoadJson(filename);
243243
saveTo(json);
244244
ofSavePrettyJson(filename, json);
@@ -247,14 +247,14 @@ void ofxBaseGui::saveToFile(const std::string& filename){
247247
}
248248
}
249249

250-
void ofxBaseGui::loadFromFile(const std::string& filename){
251-
auto extension = ofToLower(ofFilePath::getFileExt(filename));
252-
if(extension == "xml"){
250+
void ofxBaseGui::loadFromFile(const of::filesystem::path & filename){
251+
auto extension = ofGetExtensionLower(filename);
252+
if(extension == ".xml"){
253253
ofXml xml;
254254
xml.load(filename);
255255
loadFrom(xml);
256256
}else
257-
if(extension == "json"){
257+
if(extension == ".json"){
258258
ofFile jsonFile(filename);
259259
ofJson json = ofLoadJson(jsonFile);
260260
loadFrom(json);

addons/ofxGui/src/ofxBaseGui.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class ofxBaseGui {
1212
virtual ~ofxBaseGui();
1313
void draw();
1414

15-
void saveToFile(const std::string& filename);
16-
void loadFromFile(const std::string& filename);
15+
void saveToFile(const of::filesystem::path & filename);
16+
void loadFromFile(const of::filesystem::path & filename);
1717

1818
template<class T>
1919
void saveTo(T & serializer){

examples/input_output/fileOpenSaveDialogExample/src/ofApp.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void ofApp::keyReleased(int key){
6161
}
6262

6363
//
64-
ofFileDialogResult saveFileResult = ofSystemSaveDialog(ofGetTimestampString() + "." + ofToLower(originalFileExtension), "Save your file");
64+
ofFileDialogResult saveFileResult = ofSystemSaveDialog(ofGetTimestampString() + "." + originalFileExtension, "Save your file");
6565
if (saveFileResult.bSuccess){
6666
processedImages[0].save(saveFileResult.filePath);
6767
}
@@ -89,13 +89,13 @@ void ofApp::processOpenFileSelection(ofFileDialogResult openFileResult){
8989
loadedImages.clear();
9090

9191
ofLogVerbose("The file exists - now checking the type via file extension");
92-
string fileExtension = ofToUpper(file.getExtension());
93-
92+
auto extension = ofGetExtensionLower(file.path());
93+
9494
//We only want images
95-
if (fileExtension == "JPG" || fileExtension == "PNG") {
95+
if (extension == ".jpg" || extension == ".png") {
9696

9797
//Save the file extension to use when we save out
98-
originalFileExtension = fileExtension;
98+
originalFileExtension = extension;
9999

100100
//Load the selected image
101101
ofImage image;

libs/openFrameworks/gl/ofCubeMap.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "ofCubeMapShaders.h"
1515
#include "ofFbo.h"
1616
#include "ofTexture.h"
17+
#include "ofFileUtils.h"
1718

1819
#ifdef TARGET_ANDROID
1920
#include "ofAppAndroidWindow.h"
@@ -311,7 +312,7 @@ bool ofCubeMap::load( ofCubeMapSettings aSettings ) {
311312

312313
clear();
313314

314-
std::string ext = ofToLower(aSettings.filePath.extension().string());
315+
auto ext = ofGetExtensionLower(aSettings.filePath);
315316
bool hdr = (ext == ".hdr" || ext == ".exr");
316317

317318
if( hdr && !doesSupportHdr() ) {

libs/openFrameworks/graphics/ofCairoRenderer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ void ofCairoRenderer::setup(const of::filesystem::path & _filename, Type _type,
4848
streamBuffer.clear();
4949

5050
if (type == FROM_FILE_EXTENSION) {
51-
auto ext = filename.extension();
52-
if (ext == of::filesystem::path { ".svg" } || ext == of::filesystem::path { ".SVG" }) {
51+
auto ext = ofGetExtensionLower(filename);
52+
if (ext == ".svg") {
5353
type = SVG;
54-
} else if (ext == of::filesystem::path { ".pdf" } || ext == of::filesystem::path { ".PDF" }) {
54+
} else if (ext == ".pdf") {
5555
type = PDF;
5656
} else { // default to image
5757
type = IMAGE;

libs/openFrameworks/graphics/ofImage.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "ofImage.h"
22
#include "ofAppRunner.h"
33
#include "ofPixels.h"
4+
#include "ofFileUtils.h"
45

56
#include <FreeImage.h>
67

@@ -364,7 +365,8 @@ bool ofLoadImage(ofTexture & tex, const of::filesystem::path & path, const ofIma
364365
//----------------------------------------------------------------
365366
bool ofLoadImage(ofTexture & tex, const of::filesystem::path& path, bool bFlipInY, const ofImageLoadSettings &settings){
366367
bool loaded = false;
367-
std::string ext = ofToLower(path.extension().string());
368+
auto ext = ofGetExtensionLower(path);
369+
368370
bool hdr = (ext == ".hdr" || ext == ".exr");
369371
if( hdr ) {
370372
ofFloatPixels pixels;

libs/openFrameworks/utils/ofFileUtils.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,3 +2030,13 @@ std::string ofPathToString(const fs::path & path) {
20302030
}
20312031
return {};
20322032
}
2033+
2034+
//--------------------------------------------------
2035+
// Function used internally in OF core. API can change later
2036+
std::string ofGetExtensionLower(const fs::path & path) {
2037+
auto ext = path.extension().generic_string();
2038+
std::transform(ext.begin(), ext.end(), ext.begin(),
2039+
[](unsigned char c){ return std::tolower(c); });
2040+
2041+
return ext;
2042+
}

libs/openFrameworks/utils/ofFileUtils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,7 @@ void ofSetDataPathRoot(const of::filesystem::path & root);
12461246

12471247
std::string ofPathToString(const of::filesystem::path & path);
12481248

1249+
std::string ofGetExtensionLower(const of::filesystem::path & path);
12491250

12501251
/*! \cond PRIVATE */
12511252
namespace of {

0 commit comments

Comments
 (0)