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