2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
5 OpenLocale() - Give access to a new locale.
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
,
27 /*****************************************************************************
30 #include <proto/locale.h>
32 AROS_LH1(struct Locale
*, OpenLocale
,
35 AROS_LHA(CONST_STRPTR
, name
, A0
),
38 struct LocaleBase
*, LocaleBase
, 26, Locale
)
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
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.
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
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.
75 *****************************************************************************/
79 struct IntLocale
*locale
= NULL
;
81 DEBUG_OPENLOCALE(dprintf("OpenLocale: name <%s> localebase 0x%lx\n",
84 /* Have we been asked for a disk-based locale? */
87 struct IFFHandle
*iff
;
89 struct LocalePrefs
*lp
;
90 struct ContextNode
*cn
;
92 /* Clear error condition before we start. */
96 lp
= AllocMem(sizeof(struct LocalePrefs
), MEMF_CLEAR
);
98 DEBUG_OPENLOCALE(dprintf("OpenLocale: lp 0x%lx\n", lp
));
102 SetIoErr(ERROR_NO_FREE_STORE
);
109 DEBUG_OPENLOCALE(dprintf("OpenLocale: iff 0x%lx\n", iff
));
113 FreeMem(lp
, sizeof(struct LocalePrefs
));
114 SetIoErr(ERROR_NO_FREE_STORE
);
118 iff
->iff_Stream
= (IPTR
) Open(name
, MODE_OLDFILE
);
120 DEBUG_OPENLOCALE(dprintf("OpenLocale: stream 0x%lx\n",
123 if (iff
->iff_Stream
== 0)
125 FreeMem(lp
, sizeof(struct LocalePrefs
));
132 if (!OpenIFF(iff
, IFFF_READ
))
134 if (!StopChunk(iff
, ID_PREF
, ID_LCLE
))
138 error
= ParseIFF(iff
, IFFPARSE_SCAN
);
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
))
150 AllocMem(sizeof(struct IntLocale
),
151 MEMF_CLEAR
| MEMF_PUBLIC
);
152 DEBUG_OPENLOCALE(dprintf
153 ("OpenLocale: locale 0x%lx\n", locale
));
156 InitLocale(name
, locale
, lp
,
161 SetIoErr(ERROR_NO_FREE_STORE
);
164 } /* from a stop chunk */
165 else if (error
!= IFFERR_EOC
)
173 Close((BPTR
) iff
->iff_Stream
);
175 FreeMem(lp
, sizeof(struct LocalePrefs
));
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
;
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
;