/*************************************************************************** * Copyright (C) 2007 by Torben Nielsen * * torben@t-hoerup.dk * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "acpiparser.h" #include #include #include #include #include AcpiParser::AcpiParser() { mNumstates = 0; mCurrentState = 0; } AcpiParser::~AcpiParser() { } void AcpiParser::ReadAcpiFile() { char buffer[1024]; std::ifstream in; in.open("/proc/acpi/processor/CPU0/throttling"); if (!in) throw std::runtime_error("Could not open file"); int linecounter = 0; mStateDescriptions.clear(); while (! in.eof() ) { in.getline(buffer,1024); std::string line(buffer); linecounter++; unsigned int pos = line.rfind(' '); if (pos == std::string::npos) continue; pos++; std::string value = line.substr(pos, line.size() - pos); //std::cout << "Value: (" << linecounter << ") = " << value << std::endl; switch (linecounter) { case 1: mNumstates = atoi(value.c_str()); break; case 2: { std::string tmp = value.substr(1, value.size() -1); mCurrentState = atoi(tmp.c_str() ); //std::cout << "tmp: >" << tmp << "< = " << std::endl; break; } case 3: break; default: mStateDescriptions.push_back( value ); } } in.close(); //std::cout << "Current state:" << mCurrentState <= mStateDescriptions.size() ) return ""; return mStateDescriptions[stateNumber]; } void AcpiParser::SetState(int newState) { if (newState > mNumstates) throw std::invalid_argument("newState too small"); std::ofstream out; out.open("/proc/acpi/processor/CPU0/throttling"); if (!out) throw std::runtime_error("Could not open file"); std::stringstream buf; buf << newState << "\n"; out << buf.str(); out.close(); }