0023948: Wrong intersection between a surface of revolution and a plane.
[occt.git] / src / Standard / Standard_CString.cxx
1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2013 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
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.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
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
21 #define _Standard_CString_SourceFile
22
23 #include <Standard_CLocaleSentry.hxx>
24 #include <Standard_CString.hxx>
25 #include <Standard_Type.hxx>
26 #include <Standard_OStream.hxx>
27 #include <string.h>
28 #include <stdarg.h>
29
30 //============================================================================
31 //==== HashCode of a CString
32 //============================================================================
33 Standard_Integer HashCode (const Standard_CString Value,
34                            const Standard_Integer Upper )
35 {
36   Standard_Integer aLen = (Standard_Integer)strlen(Value);
37   return HashCode (HashCodes (Value, aLen), Upper);
38 }
39
40 //============================================================================
41 //==== HashCode of a CString
42 //============================================================================
43 Standard_Integer HashCodes (const Standard_CString Value,
44                             const Standard_Integer Len)
45 {
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   }
54
55   return hash;
56 }
57
58 //======================================================================
59 // Locale-independent equivalents of C functions dealing with conversion
60 // of string to real and vice-versa
61 //======================================================================
62
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
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.
83     #ifndef __ANDROID__
84       #error System does not support xlocale. Import/export could be broken if C locale did not specified by application.
85     #endif
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
93 double Strtod (const char* theStr, char** theNextPtr)
94 {
95   return strtod_l (theStr, theNextPtr, Standard_CLocaleSentry::GetCLocale());
96 }
97
98 double Atof (const char* theStr)
99 {
100   return Strtod (theStr, NULL);
101 }
102
103 int 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 }
112
113 int 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
123 int 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 }