Added memory-stored debug output to safe mode.
[cake.git] / workbench / libs / locale / openlocale.c
blobe5e9e32a36aafe65e0ab53114f51070e2c31cc1f
1 /*
2 Copyright © 1995-2008, 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,
83 LocaleBase));
85 /* Have we been asked for a disk-based locale? */
86 if(name != NULL)
88 struct IFFHandle *iff;
89 ULONG error;
90 struct LocalePrefs *lp;
91 struct ContextNode *cn;
93 /* Clear error condition before we start. */
95 SetIoErr(0);
97 lp = AllocMem(sizeof(struct LocalePrefs), MEMF_CLEAR);
99 DEBUG_OPENLOCALE(dprintf("OpenLocale: lp 0x%lx\n",lp));
101 if( lp == NULL )
103 SetIoErr(ERROR_NO_FREE_STORE);
105 return NULL;
108 iff = AllocIFF();
110 DEBUG_OPENLOCALE(dprintf("OpenLocale: iff 0x%lx\n",iff));
112 if(iff == NULL)
114 FreeMem(lp, sizeof(struct LocalePrefs));
115 SetIoErr(ERROR_NO_FREE_STORE);
116 return NULL;
119 iff->iff_Stream = (ULONG)Open(name, MODE_OLDFILE);
121 DEBUG_OPENLOCALE(dprintf("OpenLocale: stream 0x%lx\n",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) && (cn->cn_Type == ID_PREF))
144 if(ReadChunkBytes(iff, lp, sizeof(struct LocalePrefs)) == sizeof(struct LocalePrefs))
146 locale = AllocMem(sizeof(struct IntLocale), MEMF_CLEAR|MEMF_PUBLIC);
147 DEBUG_OPENLOCALE(dprintf("OpenLocale: locale 0x%lx\n",locale));
148 if(locale)
150 InitLocale(name, locale, lp, LocaleBase);
151 break;
153 else
154 SetIoErr(ERROR_NO_FREE_STORE);
157 } /* from a stop chunk */
158 else if(error != IFFERR_EOC)
159 break;
161 } /* while(1) */
162 } /* StopChunk() */
164 CloseIFF(iff);
166 Close((BPTR)iff->iff_Stream);
167 FreeIFF(iff);
168 FreeMem(lp, sizeof(struct LocalePrefs));
170 else
172 /* Return the current default */
174 DEBUG_OPENLOCALE(dprintf("OpenLocale: LocaleLock 0x%lx\n",&IntLB(LocaleBase)->lb_LocaleLock));
176 ObtainSemaphore(&IntLB(LocaleBase)->lb_LocaleLock);
177 locale = IntLB(LocaleBase)->lb_CurrentLocale;
178 locale->il_Count++;
179 ReleaseSemaphore(&IntLB(LocaleBase)->lb_LocaleLock);
182 DEBUG_OPENLOCALE(dprintf("OpenLocale: Locale 0x%lx\n",locale));
183 /* We let the optimiser do some CSE above */
184 return (struct Locale *)locale;
186 AROS_LIBFUNC_EXIT
187 } /* OpenLocale */