diff --git a/xcsg/std_filename.cpp b/xcsg/std_filename.cpp index 9df3d84..60a7ab1 100644 --- a/xcsg/std_filename.cpp +++ b/xcsg/std_filename.cpp @@ -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) {} @@ -37,6 +40,18 @@ std::string std_filename::GetPath() const return m_path.parent_path().string(); } +std::vector std_filename::GetDirs() const +{ + boost::filesystem::path dir = m_path.parent_path(); + + std::vector 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(); @@ -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); } diff --git a/xcsg/std_filename.h b/xcsg/std_filename.h index d97e4aa..bc02dab 100644 --- a/xcsg/std_filename.h +++ b/xcsg/std_filename.h @@ -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 +#include #include class std_filename { public: + std_filename(); std_filename(const std::string& file_path); virtual ~std_filename(); @@ -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 GetDirs() const; + // The full name is the file name and extension (but without the path). void SetFullName(const std::string& fullname); @@ -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; diff --git a/xcsg/xcsg_main.cpp b/xcsg/xcsg_main.cpp index 8354be5..9092e1d 100644 --- a/xcsg/xcsg_main.cpp +++ b/xcsg/xcsg_main.cpp @@ -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) {} @@ -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)) { @@ -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 { @@ -234,19 +239,19 @@ bool xcsg_main::run_xshape2d(cf_xmlNode& node,const std::string& xcsg_file) std::shared_ptr 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; } }