|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- // Utility.cpp
- //
- // Copyright (C) 2011, ARM Research Labs, LLC.
- // See www.armresearch.com for the copyright terms.
- //
- // This file implements the common functionality for the configuration
- // utilities.
-
- #include <cerrno>
- #include <cstring>
- #include <unistd.h>
- #include <pwd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
-
- #include <stdexcept>
- #include <sstream>
- #include <iostream>
- #include <fstream>
- #include <vector>
-
- #include "Utility.hpp"
-
- using namespace std;
-
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Configuration. ////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
-
- const std::string Utility::DirectorySeparator("/");
-
- /// SNF user name.
- const string SNFUserName = "snfuser";
-
- /// SNF group name.
- const string SNFGroupName = "snfuser";
-
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
- // End of configuration. /////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
-
- Utility::Utility() {
-
- }
-
- bool
- Utility::FileExists(const std::string File) {
-
- if (Verbose()) {
-
- cout << "Check whether " << File << " exists...";
-
- }
-
- bool Exists;
- std::ifstream Input;
-
- errno = 0;
- Input.open(File.c_str());
-
- if (ENOENT == errno) {
-
- Exists = false;
-
- } else {
-
- Exists = true;
-
- }
-
- Input.close();
-
- OutputVerboseEnd();
-
- return Exists;
-
- }
-
- void
- Utility::Copy(std::string From, std::string To) {
-
- if (Verbose()) {
-
- cout << "Copy " << From << " to " << To << "...";
-
- }
-
- if (!Explain()) {
-
- std::ifstream Input;
-
- Input.open(From.c_str());
- if (!Input) {
- std::string Temp;
-
- Temp = "Error opening the file " + From;
- Temp += " to read from: ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
- }
-
- std::ofstream Output;
-
- Output.open(To.c_str(), std::ios::trunc);
- if (!Output) {
- std::string Temp;
-
- Temp = "Error opening the file " + To;
- Temp += " to copy to: ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
- }
-
- if (!Input.eof()) { // Copy if there are characters.
- // Copying an empty file causes
- Output << Input.rdbuf(); // failbit to be set.
-
- }
-
- if (Output.bad() || Output.fail()) {
- std::string Temp;
-
- Temp = "Error copying " + From;
- Temp += " to " + To;
- Temp += ": ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
- }
-
- Input.close();
- if (!Input) {
- std::string Temp;
-
- Temp = "Error closing the file " + From;
- Temp += ": ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
- }
-
- Output.close();
- if (!Output) {
- std::string Temp;
-
- Temp = "Error closing the file " + To;
- Temp += ": ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
- }
-
- }
-
- OutputVerboseEnd();
-
- }
-
-
- void
- Utility::SetOwnerGroup(std::string File) {
-
- struct passwd *SNFPasswd;
- uid_t SNFUid;
- struct group *SNFGroup;
- gid_t SNFGid;
-
- if (Verbose()) {
-
- cout << "Set owner:group of " << File << " to "
- << SNFUserName << ":" << SNFGroupName << "...";
-
- }
-
- if (!Explain()) {
-
- SNFPasswd = getpwnam(SNFUserName.c_str());
- if (SNFPasswd == 0) {
- string Temp;
-
- Temp = "Error getting info for Sniffer user " + SNFUserName;
- Temp += ": ";
- Temp += strerror(errno);
- throw runtime_error(Temp);
- }
- SNFUid = SNFPasswd->pw_uid;
- SNFGid = SNFPasswd->pw_gid;
-
- if (chown(File.c_str(), SNFUid, SNFGid) != 0) {
- string Temp;
-
- Temp = "Error changing group and owner of file " + File;
- Temp += " to " + SNFUserName + ":" + SNFGroupName;
- Temp += ": ";
- Temp += strerror(errno);
- throw runtime_error(Temp);
- }
-
- }
-
- OutputVerboseEnd();
-
- }
-
- void
- Utility::SetMode(std::string File, mode_t mode) {
-
- if (Verbose()) {
-
- cout << "Set mode of " << File << " to "
- << std::oct << mode << "...";
-
- }
-
- if (!Explain()) {
-
- if (chmod(File.c_str(), mode) != 0) {
- ostringstream Temp;
-
- Temp << "Error changing permissions of file " << File
- << " to " << mode << ": " << strerror(errno);
- throw runtime_error(Temp.str());
- }
-
- }
-
- OutputVerboseEnd();
-
- }
-
- void
- Utility::MkDir(std::string &Dir) {
-
- if (Verbose()) {
-
- cout << "Create directory " << Dir << "...";
-
- }
-
- if (!Explain()) {
-
- if (mkdir(Dir.c_str(), 0) != 0) {
- ostringstream Temp;
-
- Temp << "Error creating directory " << Dir << ": " << strerror(errno);
- throw runtime_error(Temp.str());
- }
-
- }
-
- OutputVerboseEnd();
-
- }
-
- void
- Utility::ReplaceXmlAttribute(std::string *Content, std::string ElementName, std::string AttributeName, std::string AttributeValue) {
-
- std::string::size_type ElementContentBegin; // Index of start of the element content.
- std::string::size_type ElementContentEnd; // One past the end of the element content.
-
- ElementContentBegin = Content->find("<" + ElementName); // Get indices of element content.
- ElementContentEnd = Content->find("</" + ElementName);
-
- if (std::string::npos == ElementContentBegin) {
- std::string Temp;
-
- Temp = "Unable to find the start of element '" + ElementName;
- Temp += "'.";
- throw std::runtime_error(Temp);
-
- }
-
- if (std::string::npos != Content->find("<" + ElementName, ElementContentBegin + 1)) {
- std::string Temp;
-
- Temp = "Found two elements named '" + ElementName;
- Temp += "'; there must be only one.";
- throw std::runtime_error(Temp);
-
- }
-
- if (std::string::npos == ElementContentEnd) {
-
- ElementContentEnd = Content->find("/>", ElementContentBegin);
-
- }
-
- if (std::string::npos == ElementContentEnd){
- std::string Temp;
-
- Temp = "Unable to find the end of element '" + ElementName;
- Temp += "'.";
- throw std::runtime_error(Temp);
-
- }
-
- ElementContentBegin += ElementName.length() + 1; // Skip element name.
-
- std::string::size_type ProvisionalAttributeNameBegin = ElementContentBegin;
- std::string::size_type AttributeNameBegin ;
- bool FoundAttribute = false;
- std::string PrevChar;
-
- while (ProvisionalAttributeNameBegin < ElementContentEnd) { // Find start of attribute name.
-
- ProvisionalAttributeNameBegin = Content->find(AttributeName, ProvisionalAttributeNameBegin);
-
- if ( (ProvisionalAttributeNameBegin == std::string::npos) || (ProvisionalAttributeNameBegin >= ElementContentEnd) ) {
-
- break;
-
- }
-
- PrevChar = Content->at(ProvisionalAttributeNameBegin - 1);
- if (std::string::npos == PrevChar.find_first_not_of(" \t\r\n")) { // Check for whitespace before the attribute.
-
- if (FoundAttribute) {
- std::string Temp;
-
- Temp = "Found two attributes named '" + AttributeName;
- Temp += "' in element '" + ElementName;
- Temp += "'; there must be only one.";
- throw std::runtime_error(Temp);
-
- }
-
- FoundAttribute = true;
- AttributeNameBegin = ProvisionalAttributeNameBegin;
-
- }
-
- ProvisionalAttributeNameBegin = ProvisionalAttributeNameBegin + AttributeName.length(); // Skip.
-
- }
-
- if (!FoundAttribute) {
- std::string Temp;
-
- Temp = "Unable to find the attribute '" + AttributeName;
- Temp += "' in element '" + ElementName;
- Temp += "'.";
- throw std::runtime_error(Temp);
-
- }
-
- std::string::size_type EqualIndex; // Index of "=".
- std::string::size_type DelimiterIndex; // Index of delimiter of attribute value.
- std::string DelimiterValue; // Attribute value delimiter value.
- std::string::size_type AttributeValueBegin; // Index of start of attribute value.
- std::string::size_type AttributeValueEnd; // One past the end of the attribute value.
-
- EqualIndex = // Get index of first delimiter.
- Content->find_first_not_of(" \t\r\n", AttributeNameBegin + AttributeName.length());
-
- if ( (EqualIndex == std::string::npos) || (EqualIndex >= ElementContentEnd) ) {
- std::string Temp;
-
- Temp = "Unable to find \"=\" after '" + AttributeName;
- Temp += "' in element '" + ElementName;
- Temp += "'.";
- throw std::runtime_error(Temp);
-
- }
-
- DelimiterIndex = // Get index of first delimiter.
- Content->find_first_not_of(" \t\r\n", EqualIndex + 1);
-
- if ( (DelimiterIndex == std::string::npos) || (DelimiterIndex >= ElementContentEnd) ) {
- std::string Temp;
-
- Temp = "Unable to find start of value of attribute '" + AttributeName;
- Temp += "' in element '" + ElementName;
- Temp += "'.";
- throw std::runtime_error(Temp);
-
- }
-
- DelimiterValue = Content->at(DelimiterIndex);
-
- AttributeValueBegin = DelimiterIndex + 1;
-
- AttributeValueEnd = Content->find(DelimiterValue, AttributeValueBegin);
-
- if ( (AttributeValueEnd == std::string::npos) || (AttributeValueEnd >= ElementContentEnd) ) {
- std::string Temp;
-
- Temp = "Unable to find the end of the value of '" + AttributeName;
- Temp += "' in element '" + ElementName;
- Temp += "'.";
- throw std::runtime_error(Temp);
-
- }
-
- Content->replace(AttributeValueBegin,
- AttributeValueEnd - AttributeValueBegin,
- "");
- Content->insert(AttributeValueBegin, AttributeValue);
-
- }
-
- bool
- Utility::CheckForString(std::string Line, std::string SearchString) {
-
- string::size_type Indx;
-
- Indx = Line.find_first_not_of(" \t"); // Trim leading whitespace.
- if (string::npos != Indx) {
-
- Line = Line.substr(Indx);
-
- }
-
- if (Line.substr(0, SearchString.length()) == SearchString) {
-
- return true;
-
- }
-
- return false;
-
- }
-
- std::string
- Utility::Trim(std::string String) {
-
- std::string Whitespace(" \n\r\t");
-
- std::string::size_type End = String.find_last_not_of(Whitespace);
-
- if (End == std::string::npos) {
-
- return std::string();
-
- }
-
- std::string::size_type Start = String.find_first_not_of(Whitespace);
- if (Start == std::string::npos) {
-
- Start = 0;
-
- }
-
- return String.substr(Start, (End - Start) + 1);
-
- }
-
- void
- Utility::SetVerbose(bool Mode) {
-
- VerboseRequested = Mode;
-
- }
-
- bool
- Utility::Verbose() {
-
- return (VerboseRequested || ExplainRequested);
-
- }
-
- void
- Utility::SetExplain(bool Mode) {
-
- ExplainRequested = Mode;
-
- }
-
- bool
- Utility::Explain() {
-
- return ExplainRequested;
-
- }
-
- void
- Utility::SetHelp(bool Mode) {
-
- HelpRequested = Mode;
-
- }
-
- bool
- Utility::Help() {
-
- return HelpRequested;
-
- }
-
- void
- Utility::OutputVerboseEnd() {
-
- if (Verbose() && !Explain()) {
-
- cout << "done.\n";
-
- } else if (Explain()) {
-
- cout << "\n";
-
- }
-
- }
|