This moves the stuff into a namespace and makes the "private" variables invisible from the outside.
Plus a few changes where I could not resist (e.g. | is for bool the same as ||, but somehow I find || more natural...) Plus a few whitespace changes. Probably not for 1.3, but it would be nice if somebody checked that this on Windows and on OS2 Andre' -- Those who desire to give up Freedom in order to gain Security, will not have, nor do they deserve, either one. (T. Jefferson)
Index: os.h =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/os.h,v retrieving revision 1.7 diff -u -p -r1.7 os.h --- os.h 23 Sep 2002 16:03:11 -0000 1.7 +++ os.h 7 Jan 2003 18:02:16 -0000 @@ -9,71 +9,44 @@ #include "LString.h" -/// Do we need a base class for this? -class os { -public: +/// wrap OS specific stuff +namespace os { // enum shell_type { UNIX, // Do we have to distinguish sh and csh? CMD_EXE }; - // - static void init(int * argc, char ** argv[]); - - // - static string binpath() {return binpath_;} - - // - static string binname() {return binname_;} - - // system_tempdir actually doesn't belong here. + // do some work just once + void init(int * argc, char ** argv[]); + // returns path of LyX binary + string binpath(); + // returns name of LyX binary + string binname(); + // system_tempdir actually doesn't belong here // I put it here only to avoid a global variable. - static void setTmpDir(string p) {tmpdir_ = p;} - + void setTmpDir(string const & p); + // + string getTmpDir(); // - static string getTmpDir() {return tmpdir_;} - + string current_root(); // - static string current_root(); - - // - static os::shell_type shell() {return _shell;} - + shell_type shell(); // DBCS aware! - static string::size_type common_path(string const &p1, - string const &p2); - + string::size_type common_path(string const & p1, string const & p2); // no-op on UNIX, '\\'->'/' on OS/2 and Win32, ':'->'/' on MacOS, etc. - static string slashify_path(string p); + string slashify_path(string const & p); // converts a host OS path to unix style - static string external_path(string const &p); + string external_path(string const & p); // converts a unix path to host OS style - static string internal_path(string const &p); + string internal_path(string const & p); // is path absolute? - static bool is_absolute_path(string const & p); - // returns a string suitable to be passed to fopen when - // reading a file - static char const * read_mode(); + bool is_absolute_path(string const & p); + // returns a string suitable to be passed to popen when // same for popen(). - static char const * popen_read_mode(); + char const * popen_read_mode(); // - static void warn(string mesg); -private: - static string binpath_; - static string binname_; - static string tmpdir_; - static os::shell_type _shell; - // Used only on OS/2 to determine file system encoding. - static unsigned long cp_; - - // Never initialize static variables in the header! - // Anyway I bet this class will never be constructed. - os() {}; - - // Ignore warning! - ~os() {}; - + void warn(string const & mesg); }; #endif Index: os_os2.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/os_os2.C,v retrieving revision 1.5 diff -u -p -r1.5 os_os2.C --- os_os2.C 23 Sep 2002 16:03:11 -0000 1.5 +++ os_os2.C 7 Jan 2003 18:02:16 -0000 @@ -16,13 +16,22 @@ #define INCL_DOSERRORS #include <os2.h> -string os::binpath_ = string(); -string os::binname_ = string(); -string os::tmpdir_ = string(); -os::shell_type os::_shell = os::UNIX; -unsigned long os::cp_ = 0; +namespace { -void os::init(int * argc, char ** argv[]) { +string binpath_; +string binname_; +string tmpdir_; +os::shell_type shell_ = os::UNIX; +unsigned long cp_ = 0; + +} + + +namespace os { + + +void init(int * argc, char ** argv[]) +{ if (argc != 0 /* This is a hack! */) { _wildcard(argc, argv); PTIB ptib = new TIB[1]; @@ -35,7 +44,7 @@ void os::init(int * argc, char ** argv[] rc = DosQueryModuleName(ppib->pib_hmte, 256L, tmp); if (rc != NO_ERROR) exit(rc); - string p(tmp); + string p = tmp; p = slashify_path(p); binname_ = OnlyFilename(p); binname_.erase(binname_.length()-4, string::npos); @@ -50,16 +59,19 @@ void os::init(int * argc, char ** argv[] sh = "cmd.exe"; } sh = lowercase(sh); // DosMapCase() is an overkill here - if (contains(sh, "cmd.exe") - || contains(sh, "4os2.exe")) - _shell = os::CMD_EXE; + if (contains(sh, "cmd.exe") || contains(sh, "4os2.exe")) + shell_ = os::CMD_EXE; else - _shell = os::UNIX; + shell_ = os::UNIX; } + static bool initialized = false; - if (initialized) return; + if (initialized) + return; initialized = true; - ULONG CPList[3] = {0}, CPList_size; + + ULONG CPList[3] = {0}; + ULONG CPList_size; APIRET rc = DosQueryCp(3 * sizeof(ULONG), CPList, &CPList_size); if (rc != NO_ERROR) exit(rc); @@ -69,13 +81,18 @@ void os::init(int * argc, char ** argv[] cp_ = CPList[1]; } -void os::warn(string /*mesg*/) { + +void warn(string const & /*mesg*/) +{ return; } -string os::current_root() { + +string current_root() +{ APIRET rc; - ULONG drv_num, drv_map; + ULONG drv_num; + ULONG drv_map; rc = DosQueryCurrentDisk(&drv_num, &drv_map); if (rc != NO_ERROR) exit(rc); @@ -85,19 +102,22 @@ string os::current_root() { return tmp; } -string::size_type os::common_path(string const &p1, string const &p2) { + +string::size_type common_path(string const & p1, string const & p2) +{ static bool initialized = false; if (!initialized) { init(0, 0); initialized = true; } + COUNTRYCODE cntry; cntry.country = 0; cntry.codepage = cp_; string temp1 = slashify_path(p1); string temp2 = slashify_path(p2); - char * tmp1 = const_cast<char*> (temp1.c_str()); - char * tmp2 = const_cast<char*> (temp2.c_str()); + char * tmp1 = const_cast<char *> (temp1.c_str()); + char * tmp2 = const_cast<char *> (temp2.c_str()); /* rc = */ DosMapCase(p1.length(), &cntry, tmp1); // if (rc != NO_ERROR) // exit(rc); @@ -105,20 +125,26 @@ string::size_type os::common_path(string // if (rc != NO_ERROR) // exit(rc); // This algorithm works only if paths are slashified on DBCS systems. - string::size_type i = 0, - p1_len = p1.length(), - p2_len = p2.length(); - while (i < p1_len && i < p2_len && tmp1[i] == tmp2[i]) ++i; + string::size_type i = 0; + string::size_type p1_len = p1.length(); + string::size_type p2_len = p2.length(); + while (i < p1_len && i < p2_len && tmp1[i] == tmp2[i]) + ++i; if ((i < p1_len && i < p2_len) || (i < p1_len && tmp1[i] != '/' && i == p2_len) - || (i < p2_len && tmp2[i] != '/' && i == p1_len)) { - if (i) --i; // here was the last match - while (i && tmp1[i] != '/') --i; + || (i < p2_len && tmp2[i] != '/' && i == p1_len)) + { + if (i) + --i; // here was the last match + while (i && tmp1[i] != '/') + --i; } return i; } -string os::slashify_path(string p) { + +string slashify_path(string const & p) +{ static bool initialized = false; static bool leadbyte[256] = {false}; if (!initialized) { @@ -156,17 +182,19 @@ string os::slashify_path(string p) { } -string os::external_path(string const &p) { +string external_path(string const & p) +{ return p; } -string os::internal_path(string const &p) { +string internal_path(string const & p) +{ return p; } -bool os::is_absolute_path(string const & p) +bool is_absolute_path(string const & p) { return (p.length() > 1 && isalpha(static_cast<unsigned char>(p[0])) @@ -174,16 +202,41 @@ bool os::is_absolute_path(string const & } -// returns a string suitable to be passed to fopen when -// reading a file -char const * os::read_mode() +// returns a string suitable to be passed to popen when +// reading a pipe +char const * popen_read_mode() { return "r"; } -// returns a string suitable to be passed to popen when -// reading a pipe -char const * os::popen_read_mode() + +string binpath() { - return "r"; + return binpath_; } + + +string binname() +{ + return binname_; +} + + +void setTmpDir(string const & p) +{ + tmpdir_ = p; +} + + +string getTmpDir() +{ + return tmpdir_; +} + + +shell_type shell() +{ + return shell_; +} + +} // end namespace os Index: os_unix.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/os_unix.C,v retrieving revision 1.6 diff -u -p -r1.6 os_unix.C --- os_unix.C 23 Sep 2002 16:03:11 -0000 1.6 +++ os_unix.C 7 Jan 2003 18:02:16 -0000 @@ -7,23 +7,28 @@ #include "support/filetools.h" #include "support/lstrings.h" -string os::binpath_ = string(); -string os::binname_ = string(); -string os::tmpdir_ = string(); -os::shell_type os::_shell = os::UNIX; -unsigned long os::cp_ = 0; +namespace { -void os::init(int * /*argc*/, char ** argv[]) /* :cp_(0), _shell(os::UNIX) */ { - static bool initialized = false; +string binpath_; +string binname_; +string tmpdir_; + +} + +namespace os { + +void init(int * /*argc*/, char ** argv[]) +{ + static bool initialized = false; if (initialized) return; - initialized = true; + string tmp = *argv[0]; binname_ = OnlyFilename(tmp); tmp = ExpandPath(tmp); // This expands ./ and ~/ - if (!os::is_absolute_path(tmp)) { + if (!is_absolute_path(tmp)) { string binsearchpath = GetEnvPath("PATH"); // This will make "src/lyx" work always :-) binsearchpath += ";."; @@ -38,55 +43,96 @@ void os::init(int * /*argc*/, char ** ar binpath_ = tmp; } -void os::warn(string /*mesg*/) { + +void warn(string const & /*mesg*/) +{ return; } -string os::current_root() { - return string("/"); + +string current_root() +{ + return "/"; } -string::size_type os::common_path(string const &p1, string const &p2) { - string::size_type i = 0, - p1_len = p1.length(), - p2_len = p2.length(); - while (i < p1_len && i < p2_len && p1[i] == p2[i]) ++i; + +string::size_type common_path(string const & p1, string const & p2) +{ + string::size_type i = 0; + string::size_type p1_len = p1.length(); + string::size_type p2_len = p2.length(); + while (i < p1_len && i < p2_len && p1[i] == p2[i]) + ++i; if ((i < p1_len && i < p2_len) || (i < p1_len && p1[i] != '/' && i == p2_len) - || (i < p2_len && p2[i] != '/' && i == p1_len)) { - if (i) --i; // here was the last match - while (i && p1[i] != '/') --i; + || (i < p2_len && p2[i] != '/' && i == p1_len)) + { + if (i) + --i; // here was the last match + while (i && p1[i] != '/') + --i; } return i; } -string os::slashify_path(string p) { + +string slashify_path(string const & p) +{ return p; } -string os::external_path(string const &p) { + +string external_path(string const & p) +{ return p; } -string os::internal_path(string const &p) { + +string internal_path(string const & p) +{ return p; } -bool os::is_absolute_path(string const & p) + +bool is_absolute_path(string const & p) { - return (!p.empty() && p[0] == '/'); + return !p.empty() && p[0] == '/'; } -// returns a string suitable to be passed to fopen when -// reading a file -char const * os::read_mode() + +char const * popen_read_mode() { return "r"; } -// returns a string suitable to be passed to popen when -// reading a pipe -char const * os::popen_read_mode() + +string binpath() { - return "r"; + return binpath_; } + + +string binname() +{ + return binname_; +} + + +void setTmpDir(string const & p) +{ + tmpdir_ = p; +} + + +string getTmpDir() +{ + return tmpdir_; +} + + +shell_type shell() +{ + return UNIX; +} + +} // end namespace os Index: os_win32.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/support/os_win32.C,v retrieving revision 1.8 diff -u -p -r1.8 os_win32.C --- os_win32.C 27 Nov 2002 10:30:28 -0000 1.8 +++ os_win32.C 7 Jan 2003 18:02:16 -0000 @@ -12,19 +12,27 @@ #include <io.h> #include <sys/cygwin.h> +using std::endl; -string os::binpath_ = string(); -string os::binname_ = string(); -string os::tmpdir_ = string(); -os::shell_type os::_shell = os::UNIX; -unsigned long os::cp_ = 0; -using std::endl; +namespace { + +string binpath_; +string binname_; +string tmpdir_; + +} + -void os::init(int * /* argc */, char ** argv[]) { +namespace os { + +void init(int * /* argc */, char ** argv[]) +{ static bool initialized = false; - if (initialized) return; + if (initialized) + return; initialized = true; + string tmp = *argv[0]; binname_ = OnlyFilename(tmp); tmp = ExpandPath(tmp); // This expands ./ and ~/ @@ -45,39 +53,53 @@ void os::init(int * /* argc */, char ** binpath_ = tmp; } -void os::warn(string mesg) { + +void warn(string const & mesg) +{ MessageBox(0, mesg.c_str(), "LyX error", MB_OK|MB_ICONSTOP|MB_SYSTEMMODAL); - } +} + -string os::current_root() { - return string("/"); +string current_root() +{ + return "/"; } -string::size_type os::common_path(string const &p1, string const &p2) { - string::size_type i = 0, - p1_len = p1.length(), - p2_len = p2.length(); - while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i])) ++i; + +string::size_type common_path(string const & p1, string const & p2) +{ + string::size_type i = 0; + string::size_type p1_len = p1.length(); + string::size_type p2_len = p2.length(); + while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i])) + ++i; if ((i < p1_len && i < p2_len) || (i < p1_len && p1[i] != '/' && i == p2_len) - || (i < p2_len && p2[i] != '/' && i == p1_len)) { - if (i) --i; // here was the last match - while (i && p1[i] != '/') --i; + || (i < p2_len && p2[i] != '/' && i == p1_len)) + { + if (i) + --i; // here was the last match + while (i && p1[i] != '/') + --i; } return i; } -string os::slashify_path(string p) { - return subst(p, '\\', '/'); + +string slashify_path(string const & p) +{ + return subst(p, '\\', '/'); } -string os::external_path(string const & p) { - string dos_path=p; + +string external_path(string const & p) +{ + string dos_path = p; if (is_absolute_path(p)) { char dp[255]; cygwin_conv_to_full_win32_path(p.c_str(), dp); - dos_path=subst(dp,'\\','/'); + dos_path = subst(dp,'\\','/'); } lyxerr[Debug::LATEX] << "<Win32 path correction> [" @@ -91,7 +113,8 @@ string os::external_path(string const & // files are mentioned in Win32/DOS syntax. Because LyX uses the dep file // entries to check if any file has been changed we must retranslate // the Win32/DOS pathnames into Cygwin pathnames. -string os::internal_path(string const &p) { +string internal_path(string const & p) +{ char pp[256]; cygwin_conv_to_posix_path(p.c_str(), pp); string const posix_path = MakeLatexName(pp); @@ -102,13 +125,14 @@ string os::internal_path(string const &p return posix_path; } + // (Claus H.) On Win32 both Unix and Win32/DOS pathnames are used. // Therefore an absolute path could be either a pathname starting // with a slash (Unix) or a pathname starting with a drive letter // followed by a colon. Because a colon is not valid in pathes in Unix // and at another location in Win32 testing just for the existance // of the colon in the 2nd position seems to be enough! -bool os::is_absolute_path(string const & p) +bool is_absolute_path(string const & p) { if (p.empty()) return false; @@ -116,19 +140,45 @@ bool os::is_absolute_path(string const & bool isDosPath = (p.length() > 1 && p[1] == ':'); bool isUnixPath = (p[0] == '/'); - return isDosPath | isUnixPath; + return isDosPath || isUnixPath; } -// returns a string suitable to be passed to fopen when -// reading a file -char const * os::read_mode() -{ - return "rb"; -} // returns a string suitable to be passed to popen when // reading a pipe -char const * os::popen_read_mode() +char const * popen_read_mode() { return "r"; } + + +string binpath() +{ + return binpath_; +} + + +string binname() +{ + return binname_; +} + + +void setTmpDir(string const & p) +{ + tmpdir_ = p; +} + + +string getTmpDir() +{ + return tmpdir_; +} + + +shell_type shell() +{ + return UNIX; +} + +} // end namespace os