// Eingabefile mit zeilenweisen Dez- / Hex- / Oct-Zahlen
// in Dezimal verwandelt ausgeben
//
// Aufruf: hexoct filename
//
// Klaus Kusche, 2011

#include <iostream>
#include <fstream>
#include <sstream>
#include <cctype>
#include <cstring>
#include <cstdlib>

using namespace std;

const int LINE_WIDTH = 80;

int main(int argc, const char *argv[])
{
  char line[LINE_WIDTH + 1];   // +1 für '\0'
  int zahl, len;
  char last;

  if (argc != 2) {
    cerr << "Aufruf: " << argv[0] << " infilename" << endl;
    exit(EXIT_FAILURE);
  }
  
  ifstream inf(argv[1]);
  if (!inf) {
    cerr << argv[0] << ": Cannot open " << argv[1] << " for reading" << endl;
    exit(EXIT_FAILURE);
  }
  
  while (inf.getline(line, sizeof(line))) {  // bis eof oder Lesefehler

    // Leg einen stringstream an, aus dem man den Inhalt von line lesen kann
    stringstream sstr(line);   
    
    len = strlen(line);
    if (len == 0) break;            // Leere Zeile: Ende   
    last = tolower(line[len - 1]);  // Letzter Buchstabe der Zeile = Format-Angabe
    
    if (last == 'h') sstr >> hex >> zahl;
    else if (last == 'o') sstr >> oct >> zahl;
    else sstr >> dec >> zahl;
    
    if (sstr.fail())  // fail: Letztes Lesen ging schief
      cout << line << ": Konvertierungsfehler!" << endl;
    else
      cout << line << ": " << zahl << endl;
  }

  exit(EXIT_SUCCESS);
}
