#include "CLog.h" #include "SFString.h" #include #include #include using namespace std; namespace X3DTK { X3DOfstream::X3DOfstream(const char *path) : fstream() { _cwd = (char *)malloc(65535); findPath(path); SFString p(_cwd); p += "/x3dtk.log"; open(static_cast(p), fstream::in | fstream::out | fstream::trunc); } X3DOfstream::~X3DOfstream() { free(_cwd); removeLog(); } void X3DOfstream::setPath(const char *path) { removeLog(); findPath(path); SFString p(_cwd); p += "/x3dtk.log"; open(static_cast(p), fstream::in | fstream::out | fstream::trunc); } void X3DOfstream::findPath(const char *path) { char *wd = (char *)malloc(65535); char *value = getcwd(wd, 65535); if (value == 0) { cx3d << "X3DOfstream: unable to get current working directory!" << endl; return; } if (chdir(path) != 0) { cx3d << "X3DOfstream: unable to set the current working directory to !" << path << endl; return; } value = getcwd(_cwd, 65535); if (value == 0) { cx3d << "X3DOfstream: unable to get current working directory!" << endl; return; } // Goes back to the initial directory. if (chdir(wd) != 0) { cx3d << "X3DOfstream: unable to set the current working directory to !" << _cwd << endl; return; } free(wd); } void X3DOfstream::removeLog() { long l, m; seekg(0, ios::beg); l = tellg(); seekg(0, ios::end); m = tellg(); close(); if (l == m) { SFString p(_cwd); p += "/x3dtk.log"; remove(static_cast(p)); } } X3DOfstream cx3d("."); }