Fix issues with flexcat file paths
[AROS.git] / tools / flexcat / C_c_V20.sd
blobba53acdaf039d74e60cb6ffeb20b8249f8994f40
1 ##rem $Id$
2 ##stringtype C
3 ##shortstrings
4 /****************************************************************
5    This file was created automatically by `%fv'
6    from "%f0".
8    Do NOT edit by hand!
9 ****************************************************************/
11 #include <string.h>
13 #include <exec/memory.h>
14 #include <libraries/iffparse.h>
17 #if defined(__SASC)  ||  defined(_DCC)
18 #include <proto/exec.h>
19 #include <proto/dos.h>
20 #include <proto/utility.h>
21 #include <proto/iffparse.h>
22 #include <proto/locale.h>
23 #elif defined(__GNUC__)
24 #include <inline/locale.h>
25 #include <inline/iffparse.h>
26 #include <inline/dos.h>
27 #include <inline/exec.h>
28 #include <inline/utility.h>
29 #else
30 #include <clib/iffparse_protos.h>
31 #include <clib/locale_protos.h>
32 #include <clib/dos_protos.h>
33 #include <clib/exec_protos.h>
34 #include <clib/utility_protos.h>
35 #endif
41 static LONG %b_Version = %v;
42 static const STRPTR %b_BuiltInLanguage = (STRPTR) %l;
44 struct FC_Type
45 {   LONG    ID;
46     STRPTR  Str;
49 const struct FC_Type _%i = { %d, %s };
51 static struct Catalog *%b_Catalog = NULL;
52 static struct FC_Type *%b_OwnCatalog = NULL;
53 static LONG %b_OwnStrings;
54 static LONG %b_OwnBytes;
56 void Open%bCatalog(struct Locale *loc, STRPTR language)
57 { LONG tag, tagarg;
58   extern struct Library *LocaleBase;
59   extern struct Library *IFFParseBase;
60   extern void Close%bCatalog(void);
62   Close%bCatalog();  /*  Not needed if the programmer pairs Open-()
63                          and CloseCatalog() right, but does no harm. */
64   if (language == NULL)
65   { tag = TAG_IGNORE;
66   }
67   else
68   { tag = OC_Language;
69     tagarg = (LONG) language;
70   }
71   if (LocaleBase != NULL  &&  %b_Catalog == NULL)
72   { %b_Catalog = OpenCatalog(loc, (STRPTR) "%b.catalog",
73                              OC_BuiltInLanguage, %b_BuiltInLanguage,
74                              tag, tagarg,
75                              OC_Version, %b_Version,
76                              TAG_DONE);
77   }
78   if (LocaleBase == NULL  &&  IFFParseBase != NULL  &&  language != NULL  &&
79       Stricmp(language, %b_BuiltInLanguage) != 0)
80   { struct IFFHandle *iffhandle;
81     char path[128]; /*  Enough to hold 4 path items (dos.library 3.0)  */
83     if ((iffhandle = AllocIFF())  !=  NULL)
84     { /*  Trying to open the catalog  */
85       strcpy(path, "Catalogs");
86       AddPart((STRPTR) path, language, sizeof(path));
87       AddPart((STRPTR) path, (STRPTR) "%b.catalog", sizeof(path));
88       if ((iffhandle->iff_Stream = Open((STRPTR) path, MODE_OLDFILE))
89                                  ==  NULL)
90       { strcpy(path, "Locale:Catalogs");
91         AddPart((STRPTR) path, language, sizeof(path));
92         AddPart((STRPTR) path, (STRPTR) "%b.catalog", sizeof(path));
93         iffhandle->iff_Stream = Open((STRPTR) path, MODE_OLDFILE);
94       }
96       if (iffhandle->iff_Stream)
97       { InitIFFasDOS(iffhandle);
98         if (!OpenIFF(iffhandle, IFFF_READ))
99         { if (!PropChunk(iffhandle, MAKE_ID('C','T','L','G'),
100                          MAKE_ID('S','T','R','S')))
101           { struct StoredProperty *sp;
102             int error;
104             for (;;)
105             { if ((error = ParseIFF(iffhandle, IFFPARSE_STEP))
106                          ==  IFFERR_EOC)
107               { continue;
108               }
109               if (error != 0)
110               { break;
111               }
113               if (sp = FindProp(iffhandle, MAKE_ID('C','T','L','G'),
114                                 MAKE_ID('S','T','R','S')))
115               { LONG *ptr;
116                 LONG BytesToScan, StrLength;
118                 /*  First scan: Check the number of strings             */
119                 /*  Note that this assumes that the strings are padded  */
120                 /*  to a longword boundary!                             */
121                 %b_OwnBytes = 0;
122                 %b_OwnStrings = 0;
123                 BytesToScan = sp->sp_Size;
124                 ptr = sp->sp_Data;
125                 while (BytesToScan > 0)
126                 { ++%b_OwnStrings;
127                   ++ptr;                      /*  Skip ID               */
128                   StrLength = *ptr+1;         /*  NUL-Byte!             */
129                   %b_OwnBytes += StrLength;
130                   ptr += 1+(StrLength+3)/4;   /*  Skip Length and String*/
131                   BytesToScan -= 8+((StrLength+3)/4)*4;
132                 }
134                 /*  Marginal check: BytesToScan has to be 0!            */
135                 if (BytesToScan == 0)
136                 { char *cptr;
137                   LONG i;
139                   if (%b_OwnCatalog = (struct FC_Type *)
140                       AllocMem(%b_OwnStrings*sizeof(struct FC_Type)+%b_OwnBytes,
141                                MEMF_ANY))
142                   { /*  Second scan: Copy the strings and their ID's    */
143                     cptr = (char *) &%b_OwnCatalog[%b_OwnStrings];
144                     BytesToScan = sp->sp_Size;
145                     ptr = sp->sp_Data;
146                     i = 0;
147                     while (BytesToScan > 0)
148                     { %b_OwnCatalog[i].ID = *(ptr++);
149                       %b_OwnCatalog[i].Str = (STRPTR) cptr;
150                       StrLength = *ptr+1;     /*  NUL-Byte!             */
151                       ptr++;
152                       strncpy(cptr, (char *) ptr, StrLength);
153                                         /*  Not more, not less bytes!   */
154                       cptr+=StrLength;
155                       ptr += (StrLength+3)/4;
156                       BytesToScan -= 8+((StrLength+3)/4)*4;
157                       ++i;
158                     }
159                     break;
160                   }
161                 }
162               }
163             }
164           }
165           CloseIFF(iffhandle);
166         }
167         Close(iffhandle->iff_Stream);
168       }
170       FreeIFF(iffhandle);
171     }
172   }
176 void Close%bCatalog(void)
177 { if (LocaleBase != NULL)
178   { CloseCatalog(%b_Catalog);
179   }
180   %b_Catalog = NULL;
181   if (%b_OwnCatalog != NULL)
182   { FreeMem(%b_OwnCatalog,
183             %b_OwnStrings*sizeof(struct FC_Type)+%b_OwnBytes);
184     %b_OwnCatalog = NULL;
185   }
189 STRPTR Get%bString(APTR fcstr)
190 { STRPTR defaultstr = NULL;
191   LONG strnum, i;
193   strnum = ((struct FC_Type *) fcstr)->ID;
194   defaultstr = ((struct FC_Type *) fcstr)->Str;
196   if (%b_Catalog == NULL)
197   { if (%b_OwnCatalog != NULL)
198     { for (i = 0;  i < %b_OwnStrings;  i++)
199        { if (%b_OwnCatalog[i].ID == strnum)
200          { return(%b_OwnCatalog[i].Str);
201          }
202        }
203     }
204     return(defaultstr);
205   }
206   return(GetCatalogStr(%b_Catalog, strnum, defaultstr));