/[debian]/mimetic/branches/upstream/current/mimetic/utils.cxx
ViewVC logotype

Contents of /mimetic/branches/upstream/current/mimetic/utils.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1729 - (show annotations)
Fri May 15 15:30:29 2009 UTC (12 years, 2 months ago) by gregoa
File size: 2995 byte(s)
[svn-upgrade] Integrating new upstream version, mimetic (0.9.6)
1 /***************************************************************************
2 copyright : (C) 2002-2008 by Stefano Barbato
3 email : stefano@codesink.org
4
5 $Id: utils.cxx,v 1.4 2008-10-07 15:43:55 tat Exp $
6 ***************************************************************************/
7
8 /***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16 #include <mimetic/utils.h>
17 #include <sstream>
18 #include <stdlib.h>
19
20 namespace mimetic
21 {
22
23 using namespace std;
24
25
26 /**
27 * same as std::endl but NOT flush the buffer
28 */
29 std::ostream& nl(std::ostream& os)
30 {
31 return os.put('\n');
32 }
33
34 /**
35 * writes "\r\n" to the ostream \p ps
36 */
37 std::ostream& crlf(std::ostream& os)
38 {
39 return os.write("\r\n", 2);
40 }
41
42 namespace utils
43 {
44
45 struct Int
46 {
47 Int(int n)
48 : m_i(n)
49 {
50 stringstream ss;
51 ss << m_i;
52 ss >> m_si;
53 }
54 Int(const std::string& ns)
55 {
56 stringstream ss;
57 ss << ns;
58 ss >> m_i;
59 if(ss.fail())
60 m_i = 0;
61 stringstream ss2;
62 ss2 << m_i;
63 ss2 >> m_si;
64 }
65 operator int() const
66 {
67 return m_i;
68 }
69
70 operator string() const
71 {
72 return m_si;
73 }
74 private:
75 int m_i;
76 std::string m_si;
77 };
78
79
80 bool string_is_blank(const std::string& s)
81 {
82 size_t i, len = s.length();
83
84 for(i = 0; i < len; ++i)
85 if(!isblank(s[i]))
86 return false;
87 return true;
88 }
89
90 /// extract the filename from a fqn
91 string extractFilename(const string& fqn)
92 {
93 string::size_type pos;
94 if((pos = fqn.find_last_of(PATH_SEPARATOR)) != string::npos)
95 {
96 return fqn.substr(++pos);
97 } else
98 return fqn;
99 }
100
101
102
103 string int2hex(unsigned int n)
104 {
105 if(n == 0)
106 return "0";
107 static char tb[] = {
108 '0', '1', '2', '3',
109 '4', '5', '6', '7',
110 '8', '9', 'a', 'b',
111 'c', 'd', 'e', 'f'
112 };
113 size_t sz = sizeof(n), zeros = 0;
114 string r;
115 char cp;
116 for(size_t i = 0; i < sz*2; ++i)
117 {
118 cp = (n >> (i*4)) & 0xF;
119 if(cp == 0)
120 zeros++;
121 else {
122 if(zeros)
123 r.insert((string::size_type)0, zeros, '0');
124 zeros = 0;
125 r.insert((string::size_type)0, 1, tb[cp]);
126 }
127 }
128 return r;
129 }
130
131
132 string int2str(int n)
133 {
134 Int i(n);
135 return i;
136 }
137
138 int str2int(const string& str)
139 {
140 return ::atoi(str.c_str());
141 }
142
143 } // ns utils
144
145 } // ns mimetic

  ViewVC Help
Powered by ViewVC 1.1.26