QCAD
Open Source 2D CAD
Loading...
Searching...
No Matches
opennurbs_string.h
Go to the documentation of this file.
1/* $NoKeywords: $ */
2/*
3//
4// Copyright (c) 1993-2007 Robert McNeel & Associates. All rights reserved.
5// Rhinoceros is a registered trademark of Robert McNeel & Assoicates.
6//
7// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
8// ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
9// MERCHANTABILITY ARE HEREBY DISCLAIMED.
10//
11// For complete openNURBS copyright information see <http://www.opennurbs.org>.
12//
14*/
15
16#if !defined(ON_STRING_INC_)
17#define ON_STRING_INC_
18
19/*
20 This class is intended to be used to determine if a file's
21 contents have changed.
22*/
24{
25public:
28
29 // zeros all fields.
30 void Zero();
31
32 /*
33 Returns:
34 True if checksum is set.
35 */
36 bool IsSet() const;
37
38 // C++ default operator=, operator==,
39 // and copy constructor work fine.
40
41 /*
42 Descripton:
43 Set check sum values for a buffer
44 Parameters:
45 size - [in] number of bytes in buffer
46 buffer - [in]
47 time - [in] value to save in m_time
48 Returns:
49 True if checksum is set.
50 */
51 bool SetBufferCheckSum(
52 size_t size,
53 const void* buffer,
54 time_t time
55 );
56
57 /*
58 Descripton:
59 Set check sum values for a file.
60 Parameters:
61 fp - [in] pointer to a file opened with ON:FileOpen(...,L"rb")
62 Returns:
63 True if checksum is set.
64 */
65 bool SetFileCheckSum(
66 FILE* fp
67 );
68
69 /*
70 Descripton:
71 Set check sum values for a file.
72 Parameters:
73 filename - [in] name of file.
74 Returns:
75 True if checksum is set.
76 */
77 bool SetFileCheckSum(
78 const wchar_t* filename
79 );
80
81 /*
82 Description:
83 Test buffer to see if it has a matching checksum.
84 Paramters:
85 size - [in] size in bytes
86 buffer - [in]
87 Returns:
88 True if the buffer has a matching checksum.
89 */
90 bool CheckBuffer(
91 size_t size,
92 const void* buffer
93 ) const;
94
95 /*
96 Description:
97 Test buffer to see if it has a matching checksum.
98 Paramters:
99 fp - [in] pointer to file opened with ON::OpenFile(...,L"rb")
100 bSkipTimeCheck - [in] if true, the time of last
101 modification is not checked.
102 Returns:
103 True if the file has a matching checksum.
104 */
105 bool CheckFile(
106 FILE* fp,
107 bool bSkipTimeCheck = false
108 ) const;
109
110 /*
111 Description:
112 Test buffer to see if it has a matching checksum.
113 Paramters:
114 filename - [in]
115 bSkipTimeCheck - [in] if true, the time of last
116 modification is not checked.
117 Returns:
118 True if the file has a matching checksum.
119 */
120 bool CheckFile(
121 const wchar_t* filename,
122 bool bSkipTimeCheck = false
123 ) const;
124
125 bool Write(class ON_BinaryArchive&) const;
126 bool Read(class ON_BinaryArchive&);
127
128public:
129 size_t m_size;
130 time_t m_time; // UCT seconds since Jan 1, 1970
131 ON__UINT32 m_crc[8]; // crc's
132};
133
135//
136// ON_String is a char (a.k.a single byte or ascii) string
137//
138// ON_wString is a wide char (a.k.a double byte or unicode) string
139//
140
141class ON_String; // char (a.k.a single byte or ascii) string
142class ON_wString; // wide character (a.k.a double byte or unicode) string
143
148
150{
151public:
152
153// Constructors
154 ON_String();
155 ON_String( const ON_String& );
156
157 ON_String( const char* );
158 ON_String( const char*, int /*length*/ ); // from substring
159 ON_String( char, int = 1 /* repeat count */ );
160
161 ON_String( const unsigned char* );
162 ON_String( const unsigned char*, int /*length*/ ); // from substring
163 ON_String( unsigned char, int = 1 /* repeat count */ );
164
165 ON_String( const wchar_t* );
166 ON_String( const wchar_t*, int /*length*/ ); // from substring
167
168 ON_String( const ON_wString& );
169
170#if defined(ON_OS_WINDOWS)
171 // Windows support
172 bool LoadResourceString( HINSTANCE, UINT); // load from Windows string resource
173 // 2047 chars max
174#endif
175
176 void Create();
177 void Destroy(); // releases any memory and initializes to default empty string
178 void EmergencyDestroy();
179
180 /*
181 Description:
182 Enables reference counting. I limited cases, this is useful
183 for large strings or strings that are frequently passed around.
184 Reference counted strings must be carefully managed in
185 when multi-threading is used.
186 Parameters:
187 If EnableReferenceCounting()
188 is not called, then the string will not be referanceThe default is to not use
189 reference counted strings.
190 */
191 void EnableReferenceCounting( bool bEnable );
192
193 /*
194 Returns:
195 True if the string is reference counted.
196 */
197 bool IsReferenceCounted() const;
198
199
200 // Attributes & Operations
201 // as an array of characters
202 int Length() const;
203 bool IsEmpty() const; // returns true if length == 0
204 void Empty(); // sets length to zero - if possible, memory is retained
205
206 char& operator[](int);
207 char operator[](int) const;
208 char GetAt(int) const;
209 void SetAt(int, char);
210 void SetAt(int, unsigned char);
211 operator const char*() const; // as a C string
212
213 // overloaded assignment
214 ON_String& operator=(const ON_String&);
215 ON_String& operator=(char);
216 ON_String& operator=(const char*);
217 ON_String& operator=(unsigned char);
218 ON_String& operator=(const unsigned char*);
219 ON_String& operator=(const wchar_t*);
220 ON_String& operator=(const ON_wString&);
221
222 // operator+()
223 ON_String operator+(const ON_String&) const;
224 ON_String operator+(char) const;
225 ON_String operator+(unsigned char) const;
226 ON_String operator+(const char*) const;
227 ON_String operator+(const unsigned char*) const;
228
229 // string comparison
230 bool operator==(const ON_String&) const;
231 bool operator==(const char*)const ;
232 bool operator!=(const ON_String&)const ;
233 bool operator!=(const char*)const ;
234 bool operator<(const ON_String&)const ;
235 bool operator<(const char*)const ;
236 bool operator>(const ON_String&)const ;
237 bool operator>(const char*)const ;
238 bool operator<=(const ON_String&)const ;
239 bool operator<=(const char*)const ;
240 bool operator>=(const ON_String&)const ;
241 bool operator>=(const char*)const ;
242
243 // string concatenation
244 void Append( const char*, int ); // append specified number of characters
245 void Append( const unsigned char*, int ); // append specified number of characters
246 const ON_String& operator+=(const ON_String&);
247 const ON_String& operator+=(char);
248 const ON_String& operator+=(unsigned char);
249 const ON_String& operator+=(const char*);
250 const ON_String& operator+=(const unsigned char*);
251
252 // string comparison
253 // If this < string, returns < 0.
254 // If this = string, returns 0.
255 // If this < string, returns > 0.
256 int Compare( const char* ) const;
257 int Compare( const unsigned char* ) const;
258
259 int CompareNoCase( const char* ) const;
260 int CompareNoCase( const unsigned char* ) const;
261
262 // Description:
263 // Simple case sensitive wildcard matching. A question mark (?) in the
264 // pattern matches a single character. An asterisk (*) in the pattern
265 // mathes zero or more occurances of any character.
266 //
267 // Parameters:
268 // pattern - [in] pattern string where ? and * are wild cards.
269 //
270 // Returns:
271 // true if the string mathes the wild card pattern.
272 bool WildCardMatch( const char* ) const;
273 bool WildCardMatch( const unsigned char* ) const;
274
275 // Description:
276 // Simple case insensitive wildcard matching. A question mark (?) in the
277 // pattern matches a single character. An asterisk (*) in the pattern
278 // mathes zero or more occurances of any character.
279 //
280 // Parameters:
281 // pattern - [in] pattern string where ? and * are wild cards.
282 //
283 // Returns:
284 // true if the string mathes the wild card pattern.
285 bool WildCardMatchNoCase( const char* ) const;
286 bool WildCardMatchNoCase( const unsigned char* ) const;
287
288 /*
289 Description:
290 Replace all substrings that match token1 with token2
291 Parameters:
292 token1 - [in]
293 token2 - [in]
294 Returns:
295 Number of times token1 was replaced with token2.
296 */
297 int Replace( const char* token1, const char* token2 );
298 int Replace( const unsigned char* token1, const unsigned char* token2 );
299 int Replace( char token1, char token2 );
300 int Replace( unsigned char token1, unsigned char token2 );
301
302
303 // simple sub-string extraction
304 ON_String Mid(
305 int, // index of first char
306 int // count
307 ) const;
308 ON_String Mid(
309 int // index of first char
310 ) const;
311 ON_String Left(
312 int // number of chars to keep
313 ) const;
314 ON_String Right(
315 int // number of chars to keep
316 ) const;
317
318 // upper/lower/reverse conversion
319 void MakeUpper();
320 void MakeLower();
321 void MakeReverse();
322 void TrimLeft(const char* = NULL);
323 void TrimRight(const char* = NULL);
324 void TrimLeftAndRight(const char* = NULL);
325
326 // remove occurrences of chRemove
327 int Remove( const char chRemove);
328
329 // searching (return starting index, or -1 if not found)
330 // look for a single character match
331 int Find(char) const;
332 int Find(unsigned char) const;
333 int ReverseFind(char) const;
334 int ReverseFind(unsigned char) const;
335
336 // look for a specific sub-string
337 int Find(const char*) const;
338 int Find(const unsigned char*) const;
339
340 // simple formatting
341 void ON_MSC_CDECL Format( const char*, ...);
342 void ON_MSC_CDECL Format( const unsigned char*, ...);
343
344 // Low level access to string contents as character array
345 void ReserveArray(size_t); // make sure internal array has at least
346 // the requested capacity.
347 void ShrinkArray(); // shrink internal storage to minimum size
348 void SetLength(size_t); // set length (<=capacity)
349 char* Array();
350 const char* Array() const;
351
352 /*
353 Returns:
354 Total number of bytes of memory used by this class.
355 (For use in ON_Object::SizeOf() overrides.
356 */
357 unsigned int SizeOf() const;
358
359// Implementation
360public:
361 ~ON_String();
362
363protected:
364 char* m_s; // pointer to ref counted string array
365 // m_s - 12 bytes points at the string's ON_aStringHeader
366
367 // implementation helpers
368 struct ON_aStringHeader* Header() const;
369 void CreateArray(int);
370 void CopyArray();
371 void CopyToArray( const ON_String& );
372 void CopyToArray( int, const char* );
373 void CopyToArray( int, const unsigned char* );
374 void CopyToArray( int, const wchar_t* );
375 void AppendToArray( const ON_String& );
376 void AppendToArray( int, const char* );
377 void AppendToArray( int, const unsigned char* );
378 static int Length(const char*); // handles NULL pointers without crashing
379 static int Length(const unsigned char*); // handles NULL pointers without crashing
380};
381
382
387//
388// ON_wString
389//
390
392{
393public:
394
395// Constructors
396 ON_wString();
397 ON_wString( const ON_wString& );
398
399 ON_wString( const ON_String& );
400
401 ON_wString( const char* );
402 ON_wString( const char*, int /*length*/ ); // from substring
403 ON_wString( char, int = 1 /* repeat count */ );
404
405 ON_wString( const unsigned char* );
406 ON_wString( const unsigned char*, int /*length*/ ); // from substring
407 ON_wString( unsigned char, int = 1 /* repeat count */ );
408
409 ON_wString( const wchar_t* );
410 ON_wString( const wchar_t*, int /*length*/ ); // from substring
411 ON_wString( wchar_t, int = 1 /* repeat count */ );
412
413#if defined(ON_OS_WINDOWS)
414 // Windows support
415 bool LoadResourceString(HINSTANCE, UINT); // load from string resource
416 // 2047 characters max
417#endif
418
419 void Create();
420 void Destroy(); // releases any memory and initializes to default empty string
421 void EmergencyDestroy();
422
423 /*
424 Description:
425 Enables reference counting. I limited cases, this is useful
426 for large strings or strings that are frequently passed around.
427 Reference counted strings must be carefully managed in
428 when multi-threading is used.
429 Parameters:
430 If EnableReferenceCounting()
431 is not called, then the string will not be referanceThe default is to not use
432 reference counted strings.
433 */
434 void EnableReferenceCounting( bool bEnable );
435
436 /*
437 Returns:
438 True if the string is reference counted.
439 */
440 bool IsReferenceCounted() const;
441
442// Attributes & Operations
443 // as an array of characters
444 int Length() const;
445 bool IsEmpty() const;
446 void Empty(); // sets length to zero - if possible, memory is retained
447
448 wchar_t& operator[](int);
449 wchar_t operator[](int) const;
450 wchar_t GetAt(int) const;
451 void SetAt(int, char);
452 void SetAt(int, unsigned char);
453 void SetAt(int, wchar_t);
454 operator const wchar_t*() const; // as a UNICODE string
455
456 // overloaded assignment
457 const ON_wString& operator=(const ON_wString&);
458 const ON_wString& operator=(const ON_String&);
459 const ON_wString& operator=(char);
460 const ON_wString& operator=(const char*);
461 const ON_wString& operator=(unsigned char);
462 const ON_wString& operator=(const unsigned char*);
463 const ON_wString& operator=(wchar_t);
464 const ON_wString& operator=(const wchar_t*);
465
466 // string concatenation
467 void Append( const char*, int ); // append specified number of characters
468 void Append( const unsigned char*, int ); // append specified number of characters
469 void Append( const wchar_t*, int ); // append specified number of characters
470 const ON_wString& operator+=(const ON_wString&);
471 const ON_wString& operator+=(const ON_String&);
472 const ON_wString& operator+=(char);
473 const ON_wString& operator+=(unsigned char);
474 const ON_wString& operator+=(wchar_t);
475 const ON_wString& operator+=(const char*);
476 const ON_wString& operator+=(const unsigned char*);
477 const ON_wString& operator+=(const wchar_t*);
478
479 // operator+()
480 ON_wString operator+(const ON_wString&) const;
481 ON_wString operator+(const ON_String&) const;
482 ON_wString operator+(char) const;
483 ON_wString operator+(unsigned char) const;
484 ON_wString operator+(wchar_t) const;
485 ON_wString operator+(const char*) const;
486 ON_wString operator+(const unsigned char*) const;
487 ON_wString operator+(const wchar_t*) const;
488
489 // string comparison
490 bool operator==(const ON_wString&) const;
491 bool operator==(const wchar_t*) const;
492 bool operator!=(const ON_wString&) const;
493 bool operator!=(const wchar_t*) const;
494 bool operator<(const ON_wString&) const;
495 bool operator<(const wchar_t*) const;
496 bool operator>(const ON_wString&) const;
497 bool operator>(const wchar_t*) const;
498 bool operator<=(const ON_wString&) const;
499 bool operator<=(const wchar_t*) const;
500 bool operator>=(const ON_wString&) const;
501 bool operator>=(const wchar_t*) const;
502
503 // string comparison
504 // If this < string, returns < 0.
505 // If this == string, returns 0.
506 // If this < string, returns > 0.
507 int Compare( const char* ) const;
508 int Compare( const unsigned char* ) const;
509 int Compare( const wchar_t* ) const;
510
511 int CompareNoCase( const char* ) const;
512 int CompareNoCase( const unsigned char* ) const;
513 int CompareNoCase( const wchar_t* ) const;
514
515 // Description:
516 // Simple case sensitive wildcard matching. A question mark (?) in the
517 // pattern matches a single character. An asterisk (*) in the pattern
518 // mathes zero or more occurances of any character.
519 //
520 // Parameters:
521 // pattern - [in] pattern string where ? and * are wild cards.
522 //
523 // Returns:
524 // true if the string mathes the wild card pattern.
525 bool WildCardMatch( const wchar_t* ) const;
526
527 // Description:
528 // Simple case insensitive wildcard matching. A question mark (?) in the
529 // pattern matches a single character. An asterisk (*) in the pattern
530 // mathes zero or more occurances of any character.
531 //
532 // Parameters:
533 // pattern - [in] pattern string where ? and * are wild cards.
534 //
535 // Returns:
536 // true if the string mathes the wild card pattern.
537 bool WildCardMatchNoCase( const wchar_t* ) const;
538
539 /*
540 Description:
541 Replace all substrings that match token1 with token2
542 Parameters:
543 token1 - [in]
544 token2 - [in]
545 Returns:
546 Number of times toke1 was replaced with token2
547 */
548 int Replace( const wchar_t* token1, const wchar_t* token2 );
549 int Replace( wchar_t token1, wchar_t token2 );
550
551 /*
552 Description:
553 Replaces all characters in the string whose values are
554 not '0-9', 'A-Z', or 'a-z' with a percent sign followed
555 by a 2 digit hex value.
556 */
557 void UrlEncode();
558
559 /*
560 Description:
561 Replaces all %xx where xx a two digit hexadecimal number,
562 with a single character. Returns false if the orginal
563 string contained
564 */
565 bool UrlDecode();
566
567 /*
568 Description:
569 Replace all white-space characters with the token.
570 If token is zero, the string will end up with
571 internal 0's
572 Parameters:
573 token - [in]
574 whitespace - [in] if not null, this is a 0 terminated
575 string that lists the characters considered to be
576 white space. If null, then (1,2,...,32,127) is used.
577 Returns:
578 Number of whitespace characters replaced.
579 See Also:
580 ON_wString::RemoveWhiteSpace
581 */
582 int ReplaceWhiteSpace( wchar_t token, const wchar_t* whitespace = 0 );
583
584 /*
585 Description:
586 Removes all white-space characters with the token.
587 Parameters:
588 whitespace - [in] if not null, this is a 0 terminated
589 string that lists the characters considered to be
590 white space. If null, then (1,2,...,32,127) is used.
591 Returns:
592 Number of whitespace characters removed.
593 See Also:
594 ON_wString::ReplaceWhiteSpace
595 */
596 int RemoveWhiteSpace( const wchar_t* whitespace = 0 );
597
598 // simple sub-string extraction
599 ON_wString Mid(
600 int, // index of first char
601 int // count
602 ) const;
603 ON_wString Mid(
604 int // index of first char
605 ) const;
606 ON_wString Left(
607 int // number of chars to keep
608 ) const;
609 ON_wString Right(
610 int // number of chars to keep
611 ) const;
612
613 // upper/lower/reverse conversion
614 void MakeUpper();
615 void MakeLower();
616 void MakeReverse();
617 void TrimLeft(const wchar_t* = NULL);
618 void TrimRight(const wchar_t* = NULL);
619 void TrimLeftAndRight(const wchar_t* = NULL);
620
621 /*
622 Description:
623 Remove all occurrences of c.
624 */
625 int Remove( wchar_t c);
626
627 // searching (return starting index, or -1 if not found)
628 // look for a single character match
629 int Find(char) const;
630 int Find(unsigned char) const;
631 int Find(wchar_t) const;
632 int ReverseFind(char) const;
633 int ReverseFind(unsigned char) const;
634 int ReverseFind(wchar_t) const;
635
636 // look for a specific sub-string
637 int Find(const char*) const;
638 int Find(const unsigned char*) const;
639 int Find(const wchar_t*) const;
640
641
642 // simple formatting - be careful with %s in format string
643 void ON_MSC_CDECL Format( const char*, ...);
644 void ON_MSC_CDECL Format( const unsigned char*, ...);
645 void ON_MSC_CDECL Format( const wchar_t*, ...);
646
647 // Low level access to string contents as character array
648 void ReserveArray(size_t); // make sure internal array has at least
649 // the requested capacity.
650 void ShrinkArray(); // shrink internal storage to minimum size
651 void SetLength(size_t); // set length (<=capacity)
652 wchar_t* Array();
653 const wchar_t* Array() const;
654
655 /*
656 Returns:
657 Total number of bytes of memory used by this class.
658 (For use in ON_Object::SizeOf() overrides.
659 */
660 unsigned int SizeOf() const;
661
662// Implementation
663public:
664 ~ON_wString();
665
666protected:
667 wchar_t* m_s; // pointer to ref counted string array
668 // m_s - 12 bytes points at the string's ON_wStringHeader
669
670 // implementation helpers
671 struct ON_wStringHeader* Header() const;
672 void CreateArray(int);
673 void CopyArray();
674 void CopyToArray( const ON_wString& );
675 void CopyToArray( int, const char* );
676 void CopyToArray( int, const unsigned char* );
677 void CopyToArray( int, const wchar_t* );
678 void AppendToArray( const ON_wString& );
679 void AppendToArray( int, const char* );
680 void AppendToArray( int, const unsigned char* );
681 void AppendToArray( int, const wchar_t* );
682 static int Length(const char*); // handles NULL pointers without crashing
683 static int Length(const unsigned char*); // handles NULL pointers without crashing
684 static int Length(const wchar_t*); // handles NULL pointers without crashing
685};
686
688{
689public:
690 ON_UnitSystem(); // default constructor units are millimeters.
692
693 ON_UnitSystem(ON::unit_system);
694 ON_UnitSystem& operator=(ON::unit_system);
695
696 bool IsValid() const;
697
698 void Default(); // millimeters = default unit system
699
700 bool Read( class ON_BinaryArchive& );
701 bool Write( class ON_BinaryArchive& ) const;
702 void Dump( class ON_TextLog& ) const;
703
704 ON::unit_system m_unit_system;
705
706 // The m_custom_unit_... settings apply when m_unit_system = ON::custom_unit_system
707 double m_custom_unit_scale; // 1 meter = m_custom_unit_scale custom units
708 ON_wString m_custom_unit_name; // name of custom units
709
710 // Custom units example:
711 // 1 Nautical league = 5556 meters
712 // So, if you wanted your unit system to be nautical leagues
713 // your ON_UnitSystem would be
714 // m_unit_system = ON::custom_unit_system
715 // m_custom_unit_scale = 1.0/5556.0 = 0.0001799856...
716 // m_custom_unit_name = L"Nautical leagues"
717};
718
719
720#endif
bool operator<(const RPainterPath &p1, const RPainterPath &p2)
This operator allows us to sort painter paths based on z-level.
Definition RPainterPath.cpp:765
Definition opennurbs_archive.h:152
Definition opennurbs_string.h:24
time_t m_time
Definition opennurbs_string.h:130
size_t m_size
Definition opennurbs_string.h:129
Definition opennurbs_string.h:150
char * m_s
Definition opennurbs_string.h:364
Definition opennurbs_textlog.h:20
Definition opennurbs_string.h:688
double m_custom_unit_scale
Definition opennurbs_string.h:707
ON::unit_system m_unit_system
Definition opennurbs_string.h:704
ON_wString m_custom_unit_name
Definition opennurbs_string.h:708
Definition opennurbs_string.h:392
wchar_t * m_s
Definition opennurbs_string.h:667
#define ON_CLASS
Definition opennurbs_defines.h:91
#define NULL
Definition opennurbs_system.h:256
unsigned int ON__UINT32
Definition opennurbs_system.h:326
#define ON_MSC_CDECL
Definition opennurbs_system.h:240
Definition opennurbs_string.cpp:23
Definition opennurbs_wstring.cpp:121