0026823: Configuration - use EGL on another platform without GLX
[occt.git] / src / Standard / Standard_CString.cxx
CommitLineData
b311480e 1// Copyright (c) 1998-1999 Matra Datavision
91322f44 2// Copyright (c) 1999-2013 OPEN CASCADE SAS
b311480e 3//
973c2be1 4// This file is part of Open CASCADE Technology software library.
b311480e 5//
d5f74e42 6// This library is free software; you can redistribute it and/or modify it under
7// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 8// by the Free Software Foundation, with special exception defined in the file
9// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10// distribution for complete text of the license and disclaimer of any warranty.
b311480e 11//
973c2be1 12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
7fd59977 14
15// Update JR 12-09-1997 :
16// - three methods of HashCoding of strings : we may keep the value
17// of the hashcode of the string itself. This value is used when
18// resizing of a Map or copying an item from a Map to another Map.
19// - three methods of HashCoding of strings converted to uppercase.
20
69ff08ff 21#include <Standard_Type.hxx>
7fd59977 22
91322f44 23#include <Standard_CLocaleSentry.hxx>
7fd59977 24#include <Standard_CString.hxx>
91322f44 25#include <Standard_Type.hxx>
7fd59977 26#include <Standard_OStream.hxx>
7fd59977 27#include <string.h>
91322f44 28#include <stdarg.h>
7fd59977 29
30//============================================================================
7fd59977 31//==== HashCode of a CString
32//============================================================================
91322f44 33Standard_Integer HashCode (const Standard_CString Value,
7fd59977 34 const Standard_Integer Upper )
35{
29cb310a 36 Standard_Integer aLen = (Standard_Integer)strlen(Value);
37 return HashCode (HashCodes (Value, aLen), Upper);
7fd59977 38}
39
7fd59977 40//============================================================================
41//==== HashCode of a CString
42//============================================================================
29cb310a 43Standard_Integer HashCodes (const Standard_CString Value,
44 const Standard_Integer Len)
7fd59977 45{
29cb310a 46 // compute DJB2 hash of a string
47 Standard_Integer hash = 0;
48 const Standard_Character *c = Value;
49 for (Standard_Integer i = 0; i < Len; i++, c++)
50 {
51 /* hash = hash * 33 ^ c */
52 hash = ((hash << 5) + hash) ^ (*c);
53 }
7fd59977 54
29cb310a 55 return hash;
7fd59977 56}
57
29cb310a 58//======================================================================
59// Locale-independent equivalents of C functions dealing with conversion
60// of string to real and vice-versa
61//======================================================================
7fd59977 62
91322f44 63#ifdef __APPLE__
64 // There are a lot of *_l functions availalbe on Mac OS X - we use them
65 #define SAVE_TL()
66#elif defined(_WIN32)
67 // MSVCRT has equivalents with slightly different syntax
68 #define SAVE_TL()
69 #define strtod_l(thePtr, theNextPtr, theLocale) _strtod_l(thePtr, theNextPtr, theLocale)
70 #define vprintf_l(theLocale, theFormat, theArgPtr) _vprintf_l(theFormat, theLocale, theArgPtr)
71 #define vsprintf_l(theBuffer, theLocale, theFormat, theArgPtr) _vsprintf_l(theBuffer, theFormat, theLocale, theArgPtr)
72 #define vfprintf_l(theFile, theLocale, theFormat, theArgPtr) _vfprintf_l(theFile, theFormat, theLocale, theArgPtr)
73#else
74 // glibc provides only limited xlocale implementation:
75 // strtod_l/strtol_l/strtoll_l functions with explicitly specified locale
76 // and newlocale/uselocale/freelocale to switch locale within current thread only.
77 // So we switch to C locale temporarily
78 #define SAVE_TL() Standard_CLocaleSentry aLocaleSentry;
79 #ifndef HAVE_XLOCALE_H
0304f711 80 // glibc version for android platform use locale-independent implementation of
81 // strtod, strtol, strtoll functions. For other system with locale-depended
82 // implementations problems may appear if "C" locale is not set explicitly.
d8d01f6e 83 #if !defined(__ANDROID__) && !defined(__QNX__)
0304f711 84 #error System does not support xlocale. Import/export could be broken if C locale did not specified by application.
85 #endif
91322f44 86 #define strtod_l(thePtr, theNextPtr, theLocale) strtod(thePtr, theNextPtr)
87 #endif
88 #define vprintf_l(theLocale, theFormat, theArgPtr) vprintf(theFormat, theArgPtr)
89 #define vsprintf_l(theBuffer, theLocale, theFormat, theArgPtr) vsprintf(theBuffer, theFormat, theArgPtr)
90 #define vfprintf_l(theFile, theLocale, theFormat, theArgPtr) vfprintf(theFile, theFormat, theArgPtr)
91#endif
92
93double Strtod (const char* theStr, char** theNextPtr)
94{
95 return strtod_l (theStr, theNextPtr, Standard_CLocaleSentry::GetCLocale());
96}
97
98double Atof (const char* theStr)
99{
100 return Strtod (theStr, NULL);
101}
102
103int Printf (const Standard_CString theFormat, ...)
104{
105 SAVE_TL();
106 va_list argp;
107 va_start(argp, theFormat);
108 int result = vprintf_l (Standard_CLocaleSentry::GetCLocale(), theFormat, argp);
109 va_end(argp);
110 return result;
111}
7fd59977 112
91322f44 113int Fprintf (FILE* theFile, const char* theFormat, ...)
114{
115 SAVE_TL();
116 va_list argp;
117 va_start(argp, theFormat);
118 int result = vfprintf_l(theFile, Standard_CLocaleSentry::GetCLocale(), theFormat, argp);
119 va_end(argp);
120 return result;
121}
122
123int Sprintf (char* theBuffer, const char* theFormat, ...)
124{
125 SAVE_TL();
126 va_list argp;
127 va_start(argp, theFormat);
128 int result = vsprintf_l(theBuffer, Standard_CLocaleSentry::GetCLocale(), theFormat, argp);
129 va_end(argp);
130 return result;
131}