From 501f1dd61b6c0beb927151ecf331f78848261b59 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Mon, 10 Feb 2003 10:22:05 +0000 Subject: [PATCH] clean up a bit git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@6079 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/support/os.h | 71 +++++++---------------- src/support/os_os2.C | 127 +++++++++++++++++++++++++++++------------ src/support/os_unix.C | 110 ++++++++++++++++++++++++----------- src/support/os_win32.C | 120 ++++++++++++++++++++++++++------------ 4 files changed, 275 insertions(+), 153 deletions(-) diff --git a/src/support/os.h b/src/support/os.h index fef5345e06..573010a834 100644 --- a/src/support/os.h +++ b/src/support/os.h @@ -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 diff --git a/src/support/os_os2.C b/src/support/os_os2.C index bc05f319c2..a313ccfbac 100644 --- a/src/support/os_os2.C +++ b/src/support/os_os2.C @@ -16,13 +16,22 @@ #define INCL_DOSERRORS #include -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 (temp1.c_str()); - char * tmp2 = const_cast (temp2.c_str()); + char * tmp1 = const_cast (temp1.c_str()); + char * tmp2 = const_cast (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 const &p1, string const &p2) { // 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(p[0])) @@ -174,16 +202,41 @@ bool os::is_absolute_path(string const & p) } -// 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 diff --git a/src/support/os_unix.C b/src/support/os_unix.C index 26778e85df..d46eaffe3c 100644 --- a/src/support/os_unix.C +++ b/src/support/os_unix.C @@ -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 ** argv[]) /* :cp_(0), _shell(os::UNIX) */ { 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 diff --git a/src/support/os_win32.C b/src/support/os_win32.C index cbe6a7235b..5dd12e0376 100644 --- a/src/support/os_win32.C +++ b/src/support/os_win32.C @@ -12,19 +12,27 @@ #include #include +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 ** argv[]) { 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] << " [" @@ -91,7 +113,8 @@ string os::external_path(string const & p) { // 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 & p) 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 -- 2.39.2