revert between 56095 -> 55830 in arch
[AROS.git] / workbench / libs / locale / openlocale.c
blob73d13fb758f36f89e001acbe0553a458c7d10e28
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 OpenLocale() - Give access to a new locale.
6 */
8 #include <exec/types.h>
9 #include <exec/memory.h>
10 #include <libraries/locale.h>
11 #include <libraries/iffparse.h>
12 #include <prefs/prefhdr.h>
13 #include <proto/dos.h>
14 #include <prefs/locale.h>
15 #include <proto/exec.h>
16 #include <proto/iffparse.h>
17 #include "locale_intern.h"
19 #define DEBUG_OPENLOCALE(x) ;
21 extern void InitLocale(
22 CONST_STRPTR filename,
23 struct IntLocale *,
24 struct LocalePrefs *,
25 struct LocaleBase *);
27 /*****************************************************************************
29 NAME */
30 #include <proto/locale.h>
32 AROS_LH1(struct Locale *, OpenLocale,
34 /* SYNOPSIS */
35 AROS_LHA(CONST_STRPTR, name, A0),
37 /* LOCATION */
38 struct LocaleBase *, LocaleBase, 26, Locale)
40 /* FUNCTION
41 This function will open for use a named locale. A locale is a
42 data structure that contains many different parameters that
43 an application needs in order to localise itself. Using this
44 information, an application can dynamically adapt to the user's
45 environment.
47 Locales are created using the Locale Preferences Editor. If
48 you pass NULL instead of a name, then you will receive the
49 current default Locale. This is the normal procedure.
51 INPUTS
52 name - The name of the locale you wish opened, or NULL
53 to open the current default locale. This will
54 be an IFF PREF file which contains both LCLE
55 and CTRY chunks.
57 RESULT
58 A pointer to an initialised Locale structure, or NULL if none
59 could be opened. If NULL is returned you can use IoErr()
60 to find out what caused this error.
62 If you pass NULL, you will always succeed.
64 NOTES
66 EXAMPLE
68 BUGS
70 SEE ALSO
71 CloseLocale()
73 INTERNALS
75 *****************************************************************************/
77 AROS_LIBFUNC_INIT
79 struct IntLocale *locale = NULL;
81 DEBUG_OPENLOCALE(dprintf("OpenLocale: name <%s> localebase 0x%lx\n",
82 name, LocaleBase));
84 /* Have we been asked for a disk-based locale? */
85 if (name != NULL)
87 struct IFFHandle *iff;
88 ULONG error;
89 struct LocalePrefs *lp;
90 struct ContextNode *cn;
92 /* Clear error condition before we start. */
94 SetIoErr(0);
96 lp = AllocMem(sizeof(struct LocalePrefs), MEMF_CLEAR);
98 DEBUG_OPENLOCALE(dprintf("OpenLocale: lp 0x%lx\n", lp));
100 if (lp == NULL)
102 SetIoErr(ERROR_NO_FREE_STORE);
104 return NULL;
107 iff = AllocIFF();
109 DEBUG_OPENLOCALE(dprintf("OpenLocale: iff 0x%lx\n", iff));
111 if (iff == NULL)
113 FreeMem(lp, sizeof(struct LocalePrefs));
114 SetIoErr(ERROR_NO_FREE_STORE);
115 return NULL;
118 iff->iff_Stream = (IPTR) Open(name, MODE_OLDFILE);
120 DEBUG_OPENLOCALE(dprintf("OpenLocale: stream 0x%lx\n",
121 iff->iff_Stream));
123 if (iff->iff_Stream == 0)
125 FreeMem(lp, sizeof(struct LocalePrefs));
126 FreeIFF(iff);
127 return NULL;
130 InitIFFasDOS(iff);
132 if (!OpenIFF(iff, IFFF_READ))
134 if (!StopChunk(iff, ID_PREF, ID_LCLE))
136 while (1)
138 error = ParseIFF(iff, IFFPARSE_SCAN);
139 if (error == 0)
141 cn = CurrentChunk(iff);
142 if ((cn->cn_ID == ID_LCLE)
143 && (cn->cn_Type == ID_PREF))
145 if (ReadChunkBytes(iff, lp,
146 sizeof(struct LocalePrefs)) ==
147 sizeof(struct LocalePrefs))
149 locale =
150 AllocMem(sizeof(struct IntLocale),
151 MEMF_CLEAR | MEMF_PUBLIC);
152 DEBUG_OPENLOCALE(dprintf
153 ("OpenLocale: locale 0x%lx\n", locale));
154 if (locale)
156 InitLocale(name, locale, lp,
157 LocaleBase);
158 break;
160 else
161 SetIoErr(ERROR_NO_FREE_STORE);
164 } /* from a stop chunk */
165 else if (error != IFFERR_EOC)
166 break;
168 } /* while(1) */
169 } /* StopChunk() */
171 CloseIFF(iff);
173 Close((BPTR) iff->iff_Stream);
174 FreeIFF(iff);
175 FreeMem(lp, sizeof(struct LocalePrefs));
177 else
179 /* Return the current default */
181 DEBUG_OPENLOCALE(dprintf("OpenLocale: LocaleLock 0x%lx\n",
182 &IntLB(LocaleBase)->lb_LocaleLock));
184 ObtainSemaphore(&IntLB(LocaleBase)->lb_LocaleLock);
185 locale = IntLB(LocaleBase)->lb_CurrentLocale;
186 locale->il_Count++;
187 ReleaseSemaphore(&IntLB(LocaleBase)->lb_LocaleLock);
190 DEBUG_OPENLOCALE(dprintf("OpenLocale: Locale 0x%lx\n", locale));
191 /* We let the optimiser do some CSE above */
192 return (struct Locale *)locale;
194 AROS_LIBFUNC_EXIT