]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - tools/src/tools/Utils/common/Collections.h
Initial revision
[karo-tx-redboot.git] / tools / src / tools / Utils / common / Collections.h
1 //####COPYRIGHTBEGIN####
2 //                                                                          
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
5 //
6 // This program is part of the eCos host tools.
7 //
8 // This program is free software; you can redistribute it and/or modify it 
9 // under the terms of the GNU General Public License as published by the Free 
10 // Software Foundation; either version 2 of the License, or (at your option) 
11 // any later version.
12 // 
13 // This program is distributed in the hope that it will be useful, but WITHOUT 
14 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
15 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for 
16 // more details.
17 // 
18 // You should have received a copy of the GNU General Public License along with
19 // this program; if not, write to the Free Software Foundation, Inc., 
20 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 //
22 // ----------------------------------------------------------------------------
23 //                                                                          
24 //####COPYRIGHTEND####
25
26 // ----------------------------------------------------------------------------
27 // This file defines some useful collection classes:
28 //   String (a slightly extended string class, based on TCHAR)
29 //   StringArray (array of the above)
30 //   PtrArray (array of pointers)
31 //   IntArray (array of ints)
32 //   Buffer (untyped memory)
33 // ----------------------------------------------------------------------------
34
35 #ifndef _ECOS_COLLECTIONS_H
36 #define _ECOS_COLLECTIONS_H
37 #ifdef _MSC_VER
38   // Some standard warning-suppressions to avoid STL header verbosity:
39   #pragma warning (push)
40   #pragma warning(disable:4018) // signed/unsigned mismatch
41   #pragma warning(disable:4097) // typedef-name 'string' used as synonym for class-name 
42   #pragma warning(disable:4100) // unreferenced formal parameter
43   #pragma warning(disable:4146) // unary minus operator applied to unsigned type, result still unsigned
44   #pragma warning(disable:4189) // local variable is initialized but not referenced
45   #pragma warning(disable:4244) // conversion from 'unsigned int' to 'char', possible loss of data
46   #pragma warning(disable:4250) // CdlConfigurationBody' : inherits 'CdlToplevelBody::is_active' via dominance
47   #pragma warning(disable:4284) // return type for (ie; not a UDT or reference to a UDT.  Will produce errors if applied using infix notation)
48   #pragma warning(disable:4290) // C++ Exception Specification ignored
49   #pragma warning(disable:4503) // decorated name length exceeded, name was truncated
50   #pragma warning(disable:4511) // copy constructor could not be generated
51   #pragma warning(disable:4512) // assignment operator could not be generated
52   #pragma warning(disable:4663) // C++ language change: to explicitly specialize class template...
53 #endif
54
55 #include <string>
56 #include <vector>
57
58 #include "eCosStd.h"
59
60 class String;
61
62 // An array of strings:
63 typedef std::vector<String> StringArray;
64 // An array of integers:
65 typedef std::vector<int>    IntArray;
66
67 // Some extensions to the STL string class.
68 // The semantics of the like-named functions is as for the MFC class CString.
69 // The instantiation of the string class is based on TCHAR (the typedef is just below)
70 // which of course will be a wide character when building UNICODE on Windows.
71
72 typedef std::basic_string<TCHAR> string;
73 class String : public string {
74 public:
75         void Replace (LPCTSTR psz1,LPCTSTR psz2,bool bObserveEscapes=false);
76   // Standard ctors
77   String() : string(),m_pszBuf(0){}
78   String(const String& rhs) : string(rhs),m_pszBuf(0){}
79   String(const String& rhs, size_type pos, size_type n) : string(rhs,pos,n),m_pszBuf(0){}
80   String(const TCHAR *s, size_type n) : string(s?s:_T(""),n),m_pszBuf(0){}
81   String(const TCHAR *s) : string(s?s:_T("")),m_pszBuf(0){}
82   String(size_type n, TCHAR c) : string(n,c),m_pszBuf(0){}
83   String(const_iterator first, const_iterator last) : string(first,last),m_pszBuf(0){}
84   virtual ~String() { delete [] m_pszBuf; }
85
86   // Comparators
87   bool operator==(const String& str) const {return 0==compare(str); }
88   bool operator==(const LPCTSTR psz) const {return 0==compare(psz); }
89   
90   // Implicit conversion to LPCTSTR
91         operator LPCTSTR () const { return c_str(); }
92
93   // Access to the buffer
94   LPTSTR  GetBuffer (unsigned int nLength=0);
95   void ReleaseBuffer();
96   
97   // Format the contents of a string, as printf would do it:
98   void Format(LPCTSTR pszFormat,...);
99   static String SFormat(LPCTSTR pszFormat,...); 
100
101   // Tokenize (split into pieces at separator cSep).
102   // The bObserveStrings argument controls whether double quotes can be used to group words
103   int Chop(StringArray &ar,TCHAR cSep=_TCHAR(' '),bool bObserveStrings=true) const;
104
105   // UNICODE-ANSI conversions:
106   char * GetCString () const;
107   static String CStrToUnicodeStr(const char *psz);
108
109   void vFormat(LPCTSTR  pszFormat, va_list marker);
110
111 protected:
112
113   TCHAR *m_pszBuf;
114   int   m_nBufferLength;
115 };
116
117 // Use this class to allocate chunks of untyped memory without needing to worry about memory leaks:
118 class Buffer {
119 public:
120   Buffer(unsigned int nSize) : m_nSize(nSize), pData(malloc(nSize)) {}
121   ~Buffer() { free(pData); }
122   void *Data() { return pData; }
123   void Resize(int nSize) { pData=realloc(pData,nSize); m_nSize=nSize; }
124   unsigned int Size() const { return m_nSize; }
125 protected:
126   unsigned int m_nSize;
127   void *pData;
128 };
129
130 // An array of untyped pointers:
131 typedef std::vector<void *> PtrArray;
132
133 #ifdef _MSC_VER
134   #pragma warning (pop)
135 #endif
136
137 #endif