source: mainline/uspace/lib/posix/source/locale.c@ 5a6cc679

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5a6cc679 was 0d0b319, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Flip error constants to positive values, and update libposix for the change.

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 * Copyright (c) 2011 Jiri Zarevucky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libposix
30 * @{
31 */
32/** @file Locale-specific definitions.
33 */
34
35#define LIBPOSIX_INTERNAL
36#define __POSIX_DEF__(x) posix_##x
37
38#include "internal/common.h"
39#include "posix/locale.h"
40
41#include <errno.h>
42
43#include "posix/limits.h"
44#include "posix/string.h"
45
46/* Just a very basic dummy implementation.
47 * This should allow code using locales to work properly, but doesn't provide
48 * any localization functionality.
49 * Should be extended/rewritten when or if HelenOS supports locales natively.
50 */
51
52struct __posix_locale {
53 int _dummy;
54};
55
56const struct posix_lconv C_LOCALE = {
57 .currency_symbol = (char *) "",
58 .decimal_point = (char *) ".",
59 .frac_digits = CHAR_MAX,
60 .grouping = (char *) "",
61 .int_curr_symbol = (char *) "",
62 .int_frac_digits = CHAR_MAX,
63 .int_n_cs_precedes = CHAR_MAX,
64 .int_n_sep_by_space = CHAR_MAX,
65 .int_n_sign_posn = CHAR_MAX,
66 .int_p_cs_precedes = CHAR_MAX,
67 .int_p_sep_by_space = CHAR_MAX,
68 .int_p_sign_posn = CHAR_MAX,
69 .mon_decimal_point = (char *) "",
70 .mon_grouping = (char *) "",
71 .mon_thousands_sep = (char *) "",
72 .negative_sign = (char *) "",
73 .n_cs_precedes = CHAR_MAX,
74 .n_sep_by_space = CHAR_MAX,
75 .n_sign_posn = CHAR_MAX,
76 .positive_sign = (char *) "",
77 .p_cs_precedes = CHAR_MAX,
78 .p_sep_by_space = CHAR_MAX,
79 .p_sign_posn = CHAR_MAX,
80 .thousands_sep = (char *) ""
81};
82
83/**
84 * Set program locale.
85 *
86 * @param category What category to set.
87 * @param locale Locale name.
88 * @return Original locale name on success, NULL on failure.
89 */
90char *posix_setlocale(int category, const char *locale)
91{
92 // TODO
93 if (locale == NULL || *locale == '\0' ||
94 posix_strcmp(locale, "C") == 0) {
95 return (char *) "C";
96 }
97 return NULL;
98}
99
100/**
101 * Return locale-specific information.
102 *
103 * @return Information about the current locale.
104 */
105struct posix_lconv *posix_localeconv(void)
106{
107 // TODO
108 return (struct posix_lconv *) &C_LOCALE;
109}
110
111/**
112 * Duplicate locale object.
113 *
114 * @param locobj Object to duplicate.
115 * @return Duplicated object.
116 */
117posix_locale_t posix_duplocale(posix_locale_t locobj)
118{
119 if (locobj == NULL) {
120 errno = EINVAL;
121 return NULL;
122 }
123 posix_locale_t copy = malloc(sizeof(struct __posix_locale));
124 if (copy == NULL) {
125 errno = ENOMEM;
126 return NULL;
127 }
128 memcpy(copy, locobj, sizeof(struct __posix_locale));
129 return copy;
130}
131
132/**
133 * Free locale object.
134 *
135 * @param locobj Object to free.
136 */
137void posix_freelocale(posix_locale_t locobj)
138{
139 if (locobj) {
140 free(locobj);
141 }
142}
143
144/**
145 * Create or modify a locale object.
146 *
147 * @param category_mask Mask of categories to be set or modified.
148 * @param locale Locale to be used.
149 * @param base Object to modify. 0 if new object is to be created.
150 * @return The new/modified locale object.
151 */
152posix_locale_t posix_newlocale(int category_mask, const char *locale,
153 posix_locale_t base)
154{
155 if (locale == NULL ||
156 (category_mask & LC_ALL_MASK) != category_mask) {
157 errno = EINVAL;
158 return NULL;
159 }
160 // TODO
161 posix_locale_t new = malloc(sizeof(struct __posix_locale));
162 if (new == NULL) {
163 errno = ENOMEM;
164 return NULL;
165 }
166 if (base != NULL) {
167 posix_freelocale(base);
168 }
169 return new;
170}
171
172/**
173 * Set locale for the current thread.
174 *
175 * @param newloc Locale to use.
176 * @return The previously set locale or LC_GLOBAL_LOCALE
177 */
178posix_locale_t posix_uselocale(posix_locale_t newloc)
179{
180 // TODO
181 return LC_GLOBAL_LOCALE;
182}
183
184/** @}
185 */
Note: See TracBrowser for help on using the repository browser.