Skip to content

Commit

Permalink
std_filename update
Browse files Browse the repository at this point in the history
  • Loading branch information
arnholm committed Nov 13, 2018
1 parent 303e3ae commit 3e94daa
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 17 deletions.
40 changes: 38 additions & 2 deletions xcsg/std_filename.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "std_filename.h"

std_filename::std_filename()
{}

std_filename::std_filename(const std::string& file_path)
: m_path(file_path)
{}
Expand Down Expand Up @@ -37,6 +40,18 @@ std::string std_filename::GetPath() const
return m_path.parent_path().string();
}

std::vector<std::string> std_filename::GetDirs() const
{
boost::filesystem::path dir = m_path.parent_path();

std::vector<std::string> dirs;
for(auto& p : dir) {
std::string token = p.string();
if(token != "\\" && token != "/") dirs.push_back(token);
}
return std::move(dirs);
}

std::string std_filename::GetFullPath() const
{
return m_path.string();
Expand All @@ -63,7 +78,28 @@ void std_filename::SetPath(const std::string& path)
m_path = boost::filesystem::path(path) / boost::filesystem::path(m_path.stem().string() + m_path.extension().string());
}

std::string std_filename::DisplayName(bool show_path)
void std_filename::RemoveLastDir()
{
boost::filesystem::path dir = m_path.parent_path();
dir.remove_filename();
m_path = dir / boost::filesystem::path(m_path.stem().string() + m_path.extension().string());
}

std::string std_filename::get_current_directory()
{
return boost::filesystem::current_path().string();
}

void std_filename::set_current_directory(const std::string& dir_path)
{
boost::filesystem::current_path(dir_path);
}

void std_filename::create_directory(const std::string& dir_path, bool throw_if_exists)
{
return ((show_path)? GetFullPath() : GetFullName());
if(boost::filesystem::exists(dir_path)) {
if(throw_if_exists) throw std::logic_error("Directory already exists: " + dir_path);
return;
}
boost::filesystem::create_directory(dir_path);
}
24 changes: 19 additions & 5 deletions xcsg/std_filename.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#ifndef std_filename_H
#define std_filename_H
#ifndef STD_FILENAME_H
#define STD_FILENAME_H

// std_filename is intended to mimic some of the most common features in
// wxWidgets::wxFileName, but using std::string and boost for implementation
// The function names use wxWidgets CamelCase and are supposed to have the same meaning as in wxFileName

#include <string>
#include <vector>
#include <boost/filesystem.hpp>

class std_filename {
public:
std_filename();
std_filename(const std::string& file_path);
virtual ~std_filename();

Expand All @@ -34,6 +36,9 @@ class std_filename {
// Returns the full path with name and extension.
std::string GetFullPath() const;

// return the directories as a vector of strings
std::vector<std::string> GetDirs() const;

// The full name is the file name and extension (but without the path).
void SetFullName(const std::string& fullname);

Expand All @@ -46,10 +51,19 @@ class std_filename {
// Sets the path.
void SetPath(const std::string& path);

/* Extensions, not part of wxFileName */
// Removes last directory component from the path.
void RemoveLastDir();

// EXTENSIONS (not in wxFileName)

// returns the current directory
static std::string get_current_directory();

// sets the current directory
static void set_current_directory(const std::string& dir_path);

// return a "display name" as GetFullPath() or GetFullName()
std::string DisplayName(bool show_path);
// create directory
static void create_directory(const std::string& dir_path, bool throw_if_exists = true);

private:
boost::filesystem::path m_path;
Expand Down
25 changes: 15 additions & 10 deletions xcsg/xcsg_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ using namespace std;

#include "std_filename.h"

static std::string DisplayName(const std_filename& fname, bool show_path)
{
return ((show_path)? fname.GetFullPath() : fname.GetFullName());
}

xcsg_main::xcsg_main(const boost_command_line& cmd)
: m_cmd(cmd)
{}
Expand Down Expand Up @@ -73,7 +78,7 @@ bool xcsg_main::run()
if(tree.read_xml(xcsg_file)) {

std_filename file(xcsg_file);
cout << "xcsg processing: " << std_filename(xcsg_file).DisplayName(show_path) << endl;
cout << "xcsg processing: " << DisplayName(file,show_path) << endl;

cf_xmlNode root;
if(tree.get_root(root)) {
Expand Down Expand Up @@ -184,13 +189,13 @@ bool xcsg_main::run_xsolid(cf_xmlNode& node,const std::string& xcsg_file)
out_triangles exporter(triangulate.carve_polyset());

amf_file amf;
if(m_cmd.count("csg")>0) cout << "Created OpenSCAD file: " << std_filename(exporter.write_csg(xcsg_file)).DisplayName(show_path) << endl;
if(m_cmd.count("amf")>0) cout << "Created AMF file : " << std_filename(amf.write(triangulate.carve_polyset(),xcsg_file)).DisplayName(show_path) << endl;
if(m_cmd.count("obj")>0) cout << "Created OBJ file : " << std_filename(exporter.write_obj(xcsg_file)).DisplayName(show_path) << endl;
if(m_cmd.count("off")>0) cout << "Created OFF file(s) : " << std_filename(exporter.write_off(xcsg_file)).DisplayName(show_path) << endl;
if(m_cmd.count("csg")>0) cout << "Created OpenSCAD file: " << DisplayName(std_filename(exporter.write_csg(xcsg_file)),show_path) << endl;
if(m_cmd.count("amf")>0) cout << "Created AMF file : " << DisplayName(std_filename(amf.write(triangulate.carve_polyset(),xcsg_file)),show_path) << endl;
if(m_cmd.count("obj")>0) cout << "Created OBJ file : " << DisplayName(std_filename(exporter.write_obj(xcsg_file)),show_path) << endl;
if(m_cmd.count("off")>0) cout << "Created OFF file(s) : " << DisplayName(std_filename(exporter.write_off(xcsg_file)),show_path) << endl;
// write STL last so it is the most recent updated format
if(m_cmd.count("stl")>0) cout << "Created STL file : " << std_filename(exporter.write_stl(xcsg_file,true)).DisplayName(show_path) << endl;
else if(m_cmd.count("astl")>0) cout << "Created STL file : " << std_filename(exporter.write_stl(xcsg_file,false)).DisplayName(show_path) << endl;
if(m_cmd.count("stl")>0) cout << "Created STL file : " << DisplayName(std_filename(exporter.write_stl(xcsg_file,true)),show_path) << endl;
else if(m_cmd.count("astl")>0) cout << "Created STL file : " << DisplayName(std_filename(exporter.write_stl(xcsg_file,false)),show_path) << endl;

}
else {
Expand Down Expand Up @@ -234,19 +239,19 @@ bool xcsg_main::run_xshape2d(cf_xmlNode& node,const std::string& xcsg_file)
std::shared_ptr<polygon2d> poly = *i;
openscad.write_polygon(poly);
}
cout << "Created OpenSCAD file: " << std_filename(openscad.path()).DisplayName(show_path) << endl;
cout << "Created OpenSCAD file: " << DisplayName(std_filename(openscad.path()),show_path) << endl;
}

// write SVG?
if(m_cmd.count("svg")>0) {
svg_file svg;
cout << "Created SVG file: " << std_filename(svg.write(polyset,xcsg_file)).DisplayName(show_path) << endl;
cout << "Created SVG file: " << DisplayName(std_filename(svg.write(polyset,xcsg_file)),show_path) << endl;
}

// write DXF last so it is the most recent updated format
if(m_cmd.count("dxf")>0) {
dxf_file dxf;
cout << "Created DXF file: " << std_filename(dxf.write(polyset,xcsg_file)).DisplayName(show_path) << endl;
cout << "Created DXF file: " << DisplayName(std_filename(dxf.write(polyset,xcsg_file)),show_path) << endl;
}

}
Expand Down

0 comments on commit 3e94daa

Please sign in to comment.