]> git.lyx.org Git - lyx.git/blobdiff - src/support/os_os2.C
* lyxfunctional.h: delete compare_memfun and helper classes
[lyx.git] / src / support / os_os2.C
index b61a5b9c552ec81a6a2887ee4536cd7347271ea5..bf73d8e505354c85a52c918c283b9b099df39818 100644 (file)
@@ -1,12 +1,17 @@
-// os_os2.C
+/**
+ * \file os_os2.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Ruurd A. Reitsma
+ *
+ * Full author contact details are available in file CREDITS.
+ *
+ * Various OS specific functions
+ */
 
-// Various OS specific functions
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation "os.h"
-#endif
-
 #include "os.h"
 #include "support/filetools.h"
 #define INCL_DOSFILEMGR
 #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;
+#include <boost/scoped_array.hpp>
+
+using boost::scoped_array;
+
+namespace {
+
+string binpath_;
+string binname_;
+string tmpdir_;
+os::shell_type shell_ = os::UNIX;
+unsigned long cp_ = 0;
+
+}
+
+
+namespace os {
+
 
-void os::init(int * argc, char ** argv[]) {
+void init(int * argc, char ** argv[])
+{
        if (argc != 0 /* This is a hack! */) {
                _wildcard(argc, argv);
                PTIB ptib = new TIB[1];
@@ -30,12 +48,12 @@ void os::init(int * argc, char ** argv[]) {
                APIRET rc = DosGetInfoBlocks(&ptib, &ppib);
                if (rc != NO_ERROR)
                        exit(rc);
-               char* tmp = new char[256];
+               scoped_array<char> tmp(new char[256]);
                // This is the only reliable way to retrieve the executable name.
                rc = DosQueryModuleName(ppib->pib_hmte, 256L, tmp);
                if (rc != NO_ERROR)
                        exit(rc);
-               string p(tmp);
+               string p = tmp.get();
                p = slashify_path(p);
                binname_ = OnlyFilename(p);
                binname_.erase(binname_.length()-4, string::npos);
@@ -50,16 +68,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 +90,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 +111,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 +134,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,19 +191,61 @@ 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]))
                && p[1] == ':');
 }
+
+
+// returns a string suitable to be passed to popen when
+// reading a pipe
+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 shell_;
+}
+
+} // end namespace os