CFx SDK Documentation  2023 SP0
TrVisInfoString.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2017, Open Design Alliance (the "Alliance").
3 // All rights reserved.
4 //
5 // This software and its documentation and related materials are owned by
6 // the Alliance. The software may only be incorporated into application
7 // programs owned by members of the Alliance, subject to a signed
8 // Membership Agreement and Supplemental Software License Agreement with the
9 // Alliance. The structure and organization of this software are the valuable
10 // trade secrets of the Alliance and its suppliers. The software is also
11 // protected by copyright law and international treaty provisions. Application
12 // programs incorporating this software must include the following statement
13 // with their copyright notices:
14 //
15 // This application incorporates Teigha(R) software pursuant to a license
16 // agreement with Open Design Alliance.
17 // Teigha(R) Copyright (C) 2002-2017 by Open Design Alliance.
18 // All rights reserved.
19 //
20 // By use of this software, its documentation or related materials, you
21 // acknowledge and accept the above terms.
23 // GLES2 information string wrapper
24 
25 #ifndef ODTRVISINFOSTRING
26 #define ODTRVISINFOSTRING
27 
28 #include "TD_PackPush.h"
29 
30 #include "OdAlloc.h"
31 
36 {
37  struct InfoString
38  {
39  char *m_pInfoString;
40  int m_nCounter;
41  } *m_pBuffer;
42  private:
43  int calcLen(const char *pData) const
44  {
45  if (!pData)
46  return 0;
47  int len = 0;
48  while (pData[len] != 0)
49  len++;
50  return len;
51  }
52  int find(const char *pStr, char findChr, int startIdx = 0) const
53  {
54  int idx = startIdx;
55  while (pStr[idx] != 0)
56  {
57  if (pStr[idx] == findChr)
58  return idx;
59  idx++;
60  }
61  return -1;
62  }
63  int find(const char *pStr, const char *pSubstr, int startIdx = 0) const
64  {
65  int idx = startIdx, substrIdx;
66  while (pStr[idx] != 0)
67  {
68  substrIdx = 0;
69  while (pSubstr[substrIdx] != 0)
70  {
71  if (pSubstr[substrIdx] != pStr[idx + substrIdx])
72  break;
73  substrIdx++;
74  }
75  if (pSubstr[substrIdx] == 0)
76  return idx;
77  idx++;
78  }
79  return -1;
80  }
81  void erase(InfoString *pBuffer)
82  {
83  if (pBuffer->m_pInfoString)
84  ::odrxFree(pBuffer->m_pInfoString);
85  ::odrxFree(pBuffer);
86  }
87  bool dereference(const InfoString *pExclusive = NULL)
88  {
89  if (m_pBuffer != pExclusive)
90  {
91  if (m_pBuffer == NULL)
92  return true;
93  if (--m_pBuffer->m_nCounter == 0)
94  erase(m_pBuffer);
95  m_pBuffer = NULL;
96  return true;
97  }
98  return false;
99  }
100  void doSetNewBuf(const char *pData)
101  {
102  dereference();
103  InfoString *newBuf = (InfoString*)::odrxAlloc(sizeof(InfoString));
104  int strLen = calcLen(pData);
105  if (strLen)
106  {
107  newBuf->m_pInfoString = (char*)::odrxAlloc(strLen + 1);
108  for (int nChar = 0; nChar < strLen; nChar++)
109  newBuf->m_pInfoString[nChar] = pData[nChar];
110  newBuf->m_pInfoString[strLen] = 0;
111  }
112  else
113  newBuf->m_pInfoString = NULL;
114  newBuf->m_nCounter = 1;
115  m_pBuffer = newBuf;
116  }
117  void doSetCopyBuf(InfoString *pBuf)
118  {
119  if (dereference(pBuf) && pBuf)
120  {
121  m_pBuffer = pBuf;
122  pBuf->m_nCounter++;
123  }
124  }
125  public:
127  : m_pBuffer(NULL)
128  { }
129  OdTrVisInfoString(const char *pStr)
130  : m_pBuffer(NULL)
131  { doSetNewBuf(pStr); }
133  { doSetCopyBuf(is.m_pBuffer); }
134 
136  { dereference(); }
137 
138  void set(const char *pStr) { doSetNewBuf(pStr); }
139  void set(const OdTrVisInfoString &is) { doSetCopyBuf(is.m_pBuffer); }
140 
141  const char *get() const { return (m_pBuffer) ? m_pBuffer->m_pInfoString : NULL; }
142  operator const char *() const { return get(); }
143 
144  OdTrVisInfoString& operator =(const OdTrVisInfoString &is) { set(is); return *this; }
145  OdTrVisInfoString& operator =(const char *pStr) { set(pStr); return *this; }
146 
147  bool checkExtension(const char *pExtension) const;
148 
149  static bool checkExtensionExternal(const char *pExtensions, const char *pExtension);
150 };
151 
152 inline bool OdTrVisInfoString::checkExtension(const char *pExtension) const
153 {
154  const char *pExtensions = get();
155  if (!pExtension || !pExtensions)
156  return false;
157  int extLen = calcLen(pExtension);
158  if (!extLen || (find(pExtension, ' ') != -1))
159  return false;
160  int extsLen = calcLen(pExtensions);
161  if (!extsLen)
162  return false;
163  int start = 0, cur, end;
164  for (;;)
165  {
166  cur = find(pExtensions, pExtension, start);
167  if (cur == -1)
168  break;
169  end = extLen + cur;
170  if (cur == 0 || pExtensions[cur - 1] == ' ')
171  {
172  if (end == extsLen || pExtensions[end] == ' ')
173  return true;
174  }
175  start = end;
176  }
177  return false;
178 }
179 
180 inline bool OdTrVisInfoString::checkExtensionExternal(const char *pExtensions, const char *pExtension)
181 {
182  InfoString tmpBuf = { NULL, 2 }; (*(const void**)&tmpBuf.m_pInfoString) = pExtensions;
183  OdTrVisInfoString tmpString; tmpString.m_pBuffer = &tmpBuf;
184  return tmpString.checkExtension(pExtension);
185 }
186 
187 #include "TD_PackPop.h"
188 
189 #endif // ODTRVISINFOSTRING
#define NULL
Definition: GsProperties.h:177
ALLOCDLL_EXPORT void * odrxAlloc(size_t nBytes)
ALLOCDLL_EXPORT void odrxFree(void *pMemBlock)
bool checkExtension(const char *pExtension) const
OdTrVisInfoString(const OdTrVisInfoString &is)
void set(const OdTrVisInfoString &is)
OdTrVisInfoString & operator=(const OdTrVisInfoString &is)
static bool checkExtensionExternal(const char *pExtensions, const char *pExtension)
void set(const char *pStr)
OdTrVisInfoString(const char *pStr)
const char * get() const