Skip to content

Commit

Permalink
use ustring for filenames, don't crash on unreadable files
Browse files Browse the repository at this point in the history
  • Loading branch information
hurzl committed Aug 17, 2012
1 parent 1a7cdf7 commit f2d1633
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 36 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Expand Up @@ -90,7 +90,7 @@ dnl AC_SUBST(LIBREPRAP_CFLAGS)
dnl AC_SUBST(LIBREPRAP_LIBS)

AC_SUBST(DISTCHECK_LIBREPRAP_FLAGS)

case "$host_os" in
mingw*)
GL_LIBS="-lopengl32 -lglu32 -lglut32"
Expand Down
62 changes: 34 additions & 28 deletions src/files.cpp
Expand Up @@ -48,11 +48,11 @@ static double read_double(ifstream &file) {



filetype_t File::getFileType(string filename)
filetype_t File::getFileType(ustring filename)
{

// Extract file extension (i.e. "stl")
string extension = filename.substr(filename.find_last_of(".")+1);
ustring extension = filename.substr(filename.find_last_of(".")+1);

if(extension == "wrl" || extension == "WRL") {
return VRML;
Expand All @@ -71,26 +71,31 @@ filetype_t File::getFileType(string filename)
}

// ASCII files start with "solid [Name_of_file]"
string first_word;
file >> first_word;

// Find bad Solid Works STL header
// 'solid binary STL from Solid Edge, Unigraphics Solutions Inc.'
string second_word;
if(first_word == "solid")
file >> second_word;

file.close();
if(first_word == "solid" && second_word != "binary") { // ASCII
return ASCII_STL;
} else {
return BINARY_STL;
ustring first_word;
try {
file >> first_word;

// Find bad Solid Works STL header
// 'solid binary STL from Solid Edge, Unigraphics Solutions Inc.'
ustring second_word;
if(first_word == "solid")
file >> second_word;

file.close();
if(first_word == "solid" && second_word != "binary") { // ASCII
return ASCII_STL;
} else {
return BINARY_STL;
}
} catch (Glib::ConvertError e) {
cerr << _("Error: Unable to decode file - ") << filename << endl;
return NONE_STL;
}

}


bool File::loadSTLtriangles_binary(string filename,
bool File::loadSTLtriangles_binary(ustring filename,
uint max_triangles, bool readnormals,
vector<Triangle> &triangles)
{
Expand Down Expand Up @@ -170,11 +175,12 @@ bool File::loadSTLtriangles_binary(string filename,
}

// returns "filename" of the shape found in text
string File::parseSTLtriangles_ascii (istream &text,
uint max_triangles, bool readnormals,
vector<Triangle> &triangles)
ustring File::parseSTLtriangles_ascii (istream &text,
uint max_triangles, bool readnormals,
vector<Triangle> &triangles)
{
cerr << "loading ascii " << endl;
//cerr << "loading ascii " << endl;
//cerr << " locale " << std::locale().name() << endl;

string filename = "";
// uint step = 1;
Expand Down Expand Up @@ -212,7 +218,7 @@ string File::parseSTLtriangles_ascii (istream &text,

// uint itr = 0;
while(!(text).eof()) { // Loop around all triangles
string facet;
string facet;
text >> facet;
//cerr << text.tellg() << " - " << fsize << " - " <<facet << endl;
if (step > 1) {
Expand Down Expand Up @@ -262,7 +268,7 @@ string File::parseSTLtriangles_ascii (istream &text,
// Grab the 3 vertices - each one of the form "vertex %f %f %f"
Vector3d vertices[3];
for(int i=0; i<3; i++) {
string vertex;
string vertex;
text >> vertex
>> vertices[i].x()
>> vertices[i].y()
Expand Down Expand Up @@ -294,12 +300,12 @@ string File::parseSTLtriangles_ascii (istream &text,

triangles.push_back(triangle);
}
cerr << "loaded " << filename << endl;
return filename;
//cerr << "loaded " << filename << endl;
return ustring(filename);
}


bool File::loadVRMLtriangles(string filename,
bool File::loadVRMLtriangles(ustring filename,
uint max_triangles,
vector<Triangle> &triangles)

Expand All @@ -311,7 +317,7 @@ bool File::loadVRMLtriangles(string filename,
cerr << _("Error: Unable to open vrml file - ") << filename << endl;
return false;
}
string word;
ustring word;
std::vector<float> vertices;
std::vector<int> indices;
bool finished = false;
Expand Down Expand Up @@ -376,7 +382,7 @@ bool File::loadVRMLtriangles(string filename,



bool File::saveBinarySTL(string filename, const vector<Triangle> &triangles,
bool File::saveBinarySTL(ustring filename, const vector<Triangle> &triangles,
const Matrix4d &T)
{

Expand Down
15 changes: 8 additions & 7 deletions src/files.h
Expand Up @@ -24,6 +24,7 @@

#include "triangle.h"

using namespace Glib;

enum filetype_t{
ASCII_STL,
Expand All @@ -42,25 +43,25 @@ class File
File(){};
virtual ~File(){};

static filetype_t getFileType(string filename);
static filetype_t getFileType(ustring filename);


static bool loadSTLtriangles_binary(string filename,
static bool loadSTLtriangles_binary(ustring filename,
uint max_triangles, bool readnormals,
vector<Triangle> &triangles);

static string parseSTLtriangles_ascii(istream &text,
uint max_triangles, bool readnormals,
vector<Triangle> &triangles);
static ustring parseSTLtriangles_ascii(istream &text,
uint max_triangles, bool readnormals,
vector<Triangle> &triangles);


static bool loadVRMLtriangles(string filename,
static bool loadVRMLtriangles(ustring filename,
uint max_triangles,
vector<Triangle> &triangles);



static bool saveBinarySTL(string filename, const vector<Triangle> &triangles,
static bool saveBinarySTL(ustring filename, const vector<Triangle> &triangles,
const Matrix4d &T);

};

0 comments on commit f2d1633

Please sign in to comment.