| 1 | /********** |
| 2 | This library is free software; you can redistribute it and/or modify it under |
| 3 | the terms of the GNU Lesser General Public License as published by the |
| 4 | Free Software Foundation; either version 3 of the License, or (at your |
| 5 | option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.) |
| 6 | |
| 7 | This library is distributed in the hope that it will be useful, but WITHOUT |
| 8 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 9 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
| 10 | more details. |
| 11 | |
| 12 | You should have received a copy of the GNU Lesser General Public License |
| 13 | along with this library; if not, write to the Free Software Foundation, Inc., |
| 14 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 15 | **********/ |
| 16 | // "liveMedia" |
| 17 | // Copyright (c) 1996-2020 Live Networks, Inc. All rights reserved. |
| 18 | // Support for temporarily setting the locale (e.g., to "C" or "POSIX") for (e.g.) parsing or printing |
| 19 | // floating-point numbers in protocol headers, or calling toupper()/tolower() on human-input strings. |
| 20 | // Implementation |
| 21 | |
| 22 | #include "Locale.hh" |
| 23 | #include <strDup.hh> |
| 24 | |
| 25 | Locale::Locale(char const* newLocale, LocaleCategory category) { |
| 26 | #ifndef LOCALE_NOT_USED |
| 27 | #ifndef NEWLOCALE_NOT_USED |
| 28 | int categoryMask; |
| 29 | switch (category) { |
| 30 | case All: { categoryMask = LC_ALL_MASK; break; } |
| 31 | case Numeric: { categoryMask = LC_NUMERIC_MASK; break; } |
| 32 | } |
| 33 | fLocale = newlocale(categoryMask, newLocale, NULL); |
| 34 | fPrevLocale = uselocale(fLocale); |
| 35 | #else |
| 36 | switch (category) { |
| 37 | case All: { fCategoryNum = LC_ALL; break; } |
| 38 | case Numeric: { fCategoryNum = LC_NUMERIC; break; } |
| 39 | } |
| 40 | fPrevLocale = strDup(setlocale(fCategoryNum, NULL)); |
| 41 | setlocale(fCategoryNum, newLocale); |
| 42 | #endif |
| 43 | #endif |
| 44 | } |
| 45 | |
| 46 | Locale::~Locale() { |
| 47 | #ifndef LOCALE_NOT_USED |
| 48 | #ifndef NEWLOCALE_NOT_USED |
| 49 | if (fLocale != (locale_t)0) { |
| 50 | uselocale(fPrevLocale); |
| 51 | freelocale(fLocale); |
| 52 | } |
| 53 | #else |
| 54 | if (fPrevLocale != NULL) { |
| 55 | setlocale(fCategoryNum, fPrevLocale); |
| 56 | delete[] fPrevLocale; |
| 57 | } |
| 58 | #endif |
| 59 | #endif |
| 60 | } |
| 61 | |