1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2013 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
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.
21 #define _Standard_CString_SourceFile
23 #include <Standard_CLocaleSentry.hxx>
24 #include <Standard_CString.hxx>
25 #include <Standard_Type.hxx>
26 #include <Standard_OStream.hxx>
30 //============================================================================
31 //==== HashCode of a CString
32 //============================================================================
33 Standard_Integer HashCode (const Standard_CString Value,
34 const Standard_Integer Upper )
36 Standard_Integer aLen = (Standard_Integer)strlen(Value);
37 return HashCode (HashCodes (Value, aLen), Upper);
40 //============================================================================
41 //==== HashCode of a CString
42 //============================================================================
43 Standard_Integer HashCodes (const Standard_CString Value,
44 const Standard_Integer Len)
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++)
51 /* hash = hash * 33 ^ c */
52 hash = ((hash << 5) + hash) ^ (*c);
58 //======================================================================
59 // Locale-independent equivalents of C functions dealing with conversion
60 // of string to real and vice-versa
61 //======================================================================
64 // There are a lot of *_l functions availalbe on Mac OS X - we use them
67 // MSVCRT has equivalents with slightly different syntax
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)
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
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.
84 #error System does not support xlocale. Import/export could be broken if C locale did not specified by application.
86 #define strtod_l(thePtr, theNextPtr, theLocale) strtod(thePtr, theNextPtr)
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)
93 double Strtod (const char* theStr, char** theNextPtr)
95 return strtod_l (theStr, theNextPtr, Standard_CLocaleSentry::GetCLocale());
98 double Atof (const char* theStr)
100 return Strtod (theStr, NULL);
103 int Printf (const Standard_CString theFormat, ...)
107 va_start(argp, theFormat);
108 int result = vprintf_l (Standard_CLocaleSentry::GetCLocale(), theFormat, argp);
113 int Fprintf (FILE* theFile, const char* theFormat, ...)
117 va_start(argp, theFormat);
118 int result = vfprintf_l(theFile, Standard_CLocaleSentry::GetCLocale(), theFormat, argp);
123 int Sprintf (char* theBuffer, const char* theFormat, ...)
127 va_start(argp, theFormat);
128 int result = vsprintf_l(theBuffer, Standard_CLocaleSentry::GetCLocale(), theFormat, argp);