simulavr  1.1.0
helper.cpp
Go to the documentation of this file.
1  /*
2  ****************************************************************************
3  *
4  * simulavr - A simulator for the Atmel AVR family of microcontrollers.
5  * Copyright (C) 2001, 2002, 2003 Klaus Rudolph
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  ****************************************************************************
22  *
23  * $Id$
24  */
25 
26 #include <iostream>
27 #include <sstream>
28 #include "helper.h"
29 
30 using namespace std;
31 
32 HexChar::HexChar(unsigned char x) { val=x; }
33 HexShort::HexShort(unsigned short x) { val=x; }
34 DecLong::DecLong(unsigned long x) { val=x; }
35 
36 ostream &operator << (ostream &os, const HexChar &h) {
37  os << "0x";
38  os.width(2);
39  os.fill('0');
40  os << hex << (unsigned int) h.val << dec ;
41  return os;
42 }
43 
44 ostream &operator << (ostream &os, const HexShort &h) {
45  os << "0x" ;
46  os.width(4);
47  os.fill('0');
48  os << hex << (unsigned int) h.val << dec ;
49  return os;
50 }
51 
52 ostream &operator << (ostream &os, const DecLong &h) {
53  os.width(9);
54  os.fill(' ');
55  os << dec << (unsigned long) h.val << dec ;
56  return os;
57 }
58 
59 std::string int2str(int i) {
60  stringstream s;
61  s << i;
62  return s.str();
63 }
64 
65 std::string int2hex(int i) {
66  stringstream s;
67  s << hex << i;
68  return s.str();
69 }
70 
71 std::string readline(istream &is) {
72  std::string out;
73  char c=0;
74  while (!is.eof() && c!='\n') {
75  is.read(&c, 1);
76  if (is.gcount())
77  out+=c;
78  }
79  return out;
80 }
81 
82 vector<std::string> split(const std::string &inp, std::string splitc) {
83  vector<std::string> out;
84  std::string cur;
85  for (size_t i=0; i < inp.size(); i++) {
86  char c=inp[i];
87  if (splitc.find(c)==splitc.npos)
88  cur+=c;
89  else {
90  if (cur.size()) {
91  out.push_back(cur);
92  cur="";
93  }
94  }
95  }
96  if (cur.size())
97  out.push_back(cur);
98  return out;
99 }
unsigned short val
Definition: helper.h:40
DecLong(unsigned long v)
Definition: helper.cpp:34
unsigned long val
Definition: helper.h:46
STL namespace.
std::string readline(istream &is)
Reads one line from a stream.
Definition: helper.cpp:71
vector< std::string > split(const std::string &inp, std::string splitc)
Splits a string into a vector of strings at delimiters splitc.
Definition: helper.cpp:82
unsigned char val
Definition: helper.h:33
Definition: helper.h:31
std::string int2hex(int i)
Convert int into hex string.
Definition: helper.cpp:65
ostream & operator<<(ostream &os, const HexChar &h)
Definition: helper.cpp:36
Definition: helper.h:44
std::string int2str(int i)
Convert an int into a string.
Definition: helper.cpp:59
HexShort(unsigned short x)
Definition: helper.cpp:33
HexChar(unsigned char x)
Definition: helper.cpp:32