First steps toward getting full system to work with
[gem5.git] / base / inifile.cc
index e01032d024cfe5f549718ce5c0961ed8c74f546e..eb5a1335f51f49628cb9099023abfe1e76b860e5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003 The Regents of The University of Michigan
+ * Copyright (c) 2001-2005 The Regents of The University of Michigan
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  */
 
 #define USE_CPP
-// #define CPP_PIPE
-
 
 #ifdef USE_CPP
 #include <sys/signal.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 
-#if defined(__OpenBSD__)
 #include <libgen.h>
-#endif
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -45,9 +41,6 @@
 
 #include <fstream>
 #include <iostream>
-#if __GNUC__ >= 3
-#include <ext/stdio_filebuf.h>
-#endif
 
 #include <vector>
 #include <string>
@@ -76,8 +69,6 @@ IniFile::~IniFile()
 bool
 IniFile::loadCPP(const string &file, vector<char *> &cppArgs)
 {
-    int fd[2];
-
     // Open the file just to verify that we can.  Otherwise if the
     // file doesn't exist or has bad permissions the user will get
     // confusing errors from cpp/g++.
@@ -88,29 +79,27 @@ IniFile::loadCPP(const string &file, vector<char *> &cppArgs)
 
     tmpf.close();
 
-    const char *cfile = file.c_str();
-    char *dir = basename(cfile);
+    char *cfile = strncpy(new char[file.size() + 1], file.c_str(),
+                          file.size());
+    char *dir = dirname(cfile);
     char *dir_arg = NULL;
-    if (*dir != '.' && dir != cfile) {
+    if (*dir != '.') {
         string arg = "-I";
         arg += dir;
 
         dir_arg = new char[arg.size() + 1];
-        strcpy(dir_arg, arg.c_str());
+        strncpy(dir_arg, arg.c_str(), arg.size());
     }
 
-#ifdef CPP_PIPE
-    if (pipe(fd) == -1)
-        return false;
-#else
+    delete [] cfile;
+
     char tempfile[] = "/tmp/configXXXXXX";
-    fd[0] = fd[1] = mkstemp(tempfile);
-#endif
+    int tmp_fd = mkstemp(tempfile);
 
     int pid = fork();
 
     if (pid == -1)
-        return 1;
+        return false;
 
     if (pid == 0) {
         char filename[FILENAME_MAX];
@@ -141,12 +130,12 @@ IniFile::loadCPP(const string &file, vector<char *> &cppArgs)
         args[nextArg++] = NULL;
 
         close(STDOUT_FILENO);
-        if (dup2(fd[1], STDOUT_FILENO) == -1)
+        if (dup2(tmp_fd, STDOUT_FILENO) == -1)
             exit(1);
 
         execvp("g++", args);
 
-        exit(1);
+        exit(0);
     }
 
     int retval;
@@ -158,33 +147,13 @@ IniFile::loadCPP(const string &file, vector<char *> &cppArgs)
     if (!WIFEXITED(retval) || WEXITSTATUS(retval) != 0)
         return false;
 
-#ifdef CPP_PIPE
-    close(fd[1]);
-#else
-    lseek(fd[0], 0, SEEK_SET);
-#endif
+    close(tmp_fd);
 
     bool status = false;
 
-#if __GNUC__ >= 3
-    using namespace __gnu_cxx;
-    stdio_filebuf<char> fbuf(fd[0], ios_base::in, true,
-        static_cast<stdio_filebuf<char>::int_type>(BUFSIZ));
-
-    if (fbuf.is_open()) {
-        istream f(&fbuf);
-        status = load(f);
-    }
-
-#else
-    ifstream f(fd[0]);
-    if (f.is_open())
-        status = load(f);
-#endif
+    status = load(tempfile);
 
-#ifndef CPP_PIPE
     unlink(tempfile);
-#endif
 
     return status;
 }
@@ -365,47 +334,6 @@ IniFile::find(const string &sectionName, const string &entryName,
     return true;
 }
 
-bool
-IniFile::findDefault(const string &_section, const string &entry,
-                     string &value) const
-{
-    string section = _section;
-    while (!findAppend(section, entry, value)) {
-        if (!find(section, "default", section)) {
-            return false;
-        }
-    }
-
-    return true;
-}
-
-bool
-IniFile::findAppend(const string &_section, const string &entry,
-                    string &value) const
-{
-    string section = _section;
-    bool ret = false;
-    bool first = true;
-
-    do {
-        string val;
-        if (find(section, entry, val)) {
-            ret = true;
-            if (first) {
-                value = val;
-                first = false;
-            } else {
-                value += " ";
-                value += val;
-            }
-
-        }
-    } while (find(section, "append", section));
-
-    return ret;
-}
-
-
 bool
 IniFile::sectionExists(const string &sectionName) const
 {