1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557 | -------------------------------------------------------------------------------
-- NAME (body) : localize-parser.adb
-- AUTHOR : Pascal Pignard
-- ROLE : Localization files parser unit.
-- NOTES : Ada 2012, GNOGA 2.1 alpha
--
-- COPYRIGHT : (c) Pascal Pignard 2021
-- LICENCE : CeCILL V2 (http://www.cecill.info)
-- CONTACT : http://blady.pagesperso-orange.fr
-------------------------------------------------------------------------------
with Ada.Characters.Wide_Wide_Latin_1;
with Gnoga;
with UXStrings.Formatting;
with UXStrings.Text_IO;
package body Localize.Parser is
use type Content_Maps.Cursor;
procedure Parse_Strings_File
(File_Name : String;
Content : out Property_List)
is
type State_Type is (None, In_Comment, In_Key, In_Value, Equal, Semi_Colon);
Raw_File : UXStrings.Text_IO.File_Type;
Text : String;
C : Unicode_Character;
I : Natural;
State : State_Type := None;
Comment : String;
Key : String;
Value : String;
procedure Append (C : Unicode_Character) is
begin
case State is
when In_Comment =>
Append (Comment, C);
when In_Key =>
Append (Key, C);
when In_Value =>
Append (Value, C);
when others =>
null;
end case;
end Append;
begin
UXStrings.Text_IO.Open (Raw_File, UXStrings.Text_IO.In_File, File_Name, UTF_16LE, UXStrings.Text_IO.LF_Ending);
while not UXStrings.Text_IO.End_Of_File (Raw_File) loop
UXStrings.Text_IO.Get (Raw_File, C);
Text.Append (C);
end loop;
UXStrings.Text_IO.Close (Raw_File);
Content.Clear;
I := Text.First;
while I <= Text.Last loop
if Text (I) = '\' then
Text.Next (I);
case Text (I) is
when 't' =>
Append (Ada.Characters.Wide_Wide_Latin_1.HT);
when 'n' =>
Append (Ada.Characters.Wide_Wide_Latin_1.LF);
when Ada.Characters.Wide_Wide_Latin_1.LF =>
null;
when '"' | '\' =>
Append (Text (I));
when others =>
Append ('\');
Append (Text (I));
end case;
elsif Text (I) = '/' and State = None then
Text.Next (I);
if Text (I) = '*' then
State := In_Comment;
end if;
elsif Text (I) /= '*' and State = In_Comment then
Append (Comment, Text (I));
elsif Text (I) = '*' and State = In_Comment then
Text.Next (I);
if Text (I) = '/' then
State := None;
else
Append (Comment, '*');
Append (Comment, Text (I));
end if;
elsif Text (I) = '"' and State = None then
State := In_Key;
elsif Text (I) /= '"' and State = In_Key then
Append (Key, Text (I));
elsif Text (I) = '"' and State = In_Key then
State := Equal;
elsif Text (I) = '"' and State = Equal then
State := In_Value;
elsif Text (I) /= '"' and State = In_Value then
Append (Value, Text (I));
elsif Text (I) = '"' and State = In_Value then
State := Semi_Colon;
elsif Text (I) = ';' and State = Semi_Colon then
Content.Insert (Key, (Comment, Value, False));
Comment := Null_UXString;
Key := Null_UXString;
Value := Null_UXString;
State := None;
end if;
Text.Next (I);
end loop;
end Parse_Strings_File;
procedure Write_Strings_File
(File_Name : String;
Content : Property_List)
is
Raw_File : UXStrings.Text_IO.File_Type;
procedure Escaped_Put
(Str : String;
Multi_Comment : Boolean := False)
is
begin
for I in 1 .. Length (Str) loop
if Element (Str, I) = Ada.Characters.Wide_Wide_Latin_1.HT then
UXStrings.Text_IO.Put (Raw_File, '\');
UXStrings.Text_IO.Put (Raw_File, 't');
elsif Element (Str, I) = Ada.Characters.Wide_Wide_Latin_1.LF then
if Multi_Comment then
UXStrings.Text_IO.New_Line (Raw_File);
else
UXStrings.Text_IO.Put (Raw_File, '\');
UXStrings.Text_IO.Put (Raw_File, 'n');
end if;
elsif Element (Str, I) in '"' | '\' then
UXStrings.Text_IO.Put (Raw_File, '\');
UXStrings.Text_IO.Put (Raw_File, Element (Str, I));
else
UXStrings.Text_IO.Put (Raw_File, Element (Str, I));
end if;
end loop;
end Escaped_Put;
begin
UXStrings.Text_IO.Create (Raw_File, UXStrings.Text_IO.Out_File, File_Name, UTF_16LE, UXStrings.Text_IO.LF_Ending);
UXStrings.Text_IO.Put_BOM (Raw_File);
for C in Content.Iterate loop
if Content_Maps.Element (C).Comment /= Null_UXString then
UXStrings.Text_IO.Put (Raw_File, '/');
UXStrings.Text_IO.Put (Raw_File, '*');
Escaped_Put (Content_Maps.Element (C).Comment, True);
UXStrings.Text_IO.Put (Raw_File, '*');
UXStrings.Text_IO.Put (Raw_File, '/');
UXStrings.Text_IO.New_Line (Raw_File);
end if;
UXStrings.Text_IO.Put (Raw_File, '"');
Escaped_Put (Content_Maps.Key (C));
UXStrings.Text_IO.Put (Raw_File, '"');
UXStrings.Text_IO.Put (Raw_File, ' ');
UXStrings.Text_IO.Put (Raw_File, '=');
UXStrings.Text_IO.Put (Raw_File, ' ');
UXStrings.Text_IO.Put (Raw_File, '"');
Escaped_Put (Content_Maps.Element (C).Text);
UXStrings.Text_IO.Put (Raw_File, '"');
UXStrings.Text_IO.Put (Raw_File, ';');
UXStrings.Text_IO.New_Line (Raw_File);
end loop;
UXStrings.Text_IO.Close (Raw_File);
end Write_Strings_File;
procedure Parse_Properties_File
(File_Name : String;
Content : out Property_List)
is
type State_Type is (None, In_Comment, In_Key, In_Value, Equal);
Raw_File : UXStrings.Text_IO.File_Type;
Text : String;
C : Unicode_Character;
I : Natural;
State : State_Type := None;
Comment : String;
Key : String;
Value : String;
Hex4 : String := "0000";
procedure Append (C : Wide_Wide_Character) is
begin
case State is
when In_Comment =>
Append (Comment, C);
when In_Key =>
Append (Key, C);
when In_Value =>
Append (Value, C);
when None =>
State := In_Key;
Append (Key, C);
when Equal =>
State := In_Value;
Append (Value, C);
end case;
end Append;
begin
UXStrings.Text_IO.Open (Raw_File, UXStrings.Text_IO.In_File, File_Name, Latin_1, UXStrings.Text_IO.LF_Ending);
while not UXStrings.Text_IO.End_Of_File (Raw_File) loop
UXStrings.Text_IO.Get (Raw_File, C);
Text.Append (C);
end loop;
UXStrings.Text_IO.Close (Raw_File);
Content.Clear;
I := Text.First;
while I <= Text.Last loop
if Text (I) = '\' then
Text.Next (I);
case Text (I) is
when 'u' =>
for J in Hex4 loop
Text.Next (I);
exit when I > Text.Last;
Hex4.Replace_Unicode (J, Text (I));
end loop;
Append (Wide_Wide_Character'Val (Gnoga.Value (Hex4, 16)));
when ' ' | ':' | '=' | '#' | '!' | '\' =>
Append (Text (I));
when 't' =>
Append (Ada.Characters.Wide_Wide_Latin_1.HT);
when 'n' =>
Append (Ada.Characters.Wide_Wide_Latin_1.LF);
when Ada.Characters.Wide_Wide_Latin_1.LF =>
null;
when others =>
Append ('\');
Append (Text (I));
end case;
elsif Text (I) = Ada.Characters.Wide_Wide_Latin_1.LF and State = In_Comment then
State := None;
elsif State = In_Comment then
Append (Comment, Text (I));
elsif Text (I) = Ada.Characters.Wide_Wide_Latin_1.LF and State in In_Value | Equal | In_Key then
Content.Include (Key, (Comment, Value, False));
Comment := Null_UXString;
Key := Null_UXString;
Value := Null_UXString;
State := None;
elsif Text (I) in '#' | '!' and State = None then
State := In_Comment;
if Comment /= Null_UXString then
Append (Ada.Characters.Wide_Wide_Latin_1.LF);
end if;
elsif Text (I) in '=' | ':' and State = In_Key then
State := Equal;
elsif Text (I) not in Ada.Characters.Wide_Wide_Latin_1.HT | Ada.Characters.Wide_Wide_Latin_1.LF |
Ada.Characters.Wide_Wide_Latin_1.FF | ' ' and
State in In_Key
then
Append (Key, (Text (I)));
elsif Text (I) not in Ada.Characters.Wide_Wide_Latin_1.HT | Ada.Characters.Wide_Wide_Latin_1.LF |
Ada.Characters.Wide_Wide_Latin_1.FF | ' ' and
State in None
then
State := In_Key;
Append (Key, Text (I));
elsif Text (I) not in Ada.Characters.Wide_Wide_Latin_1.HT | Ada.Characters.Wide_Wide_Latin_1.FF and
State in In_Value
then
Append (Value, Text (I));
elsif Text (I) not in Ada.Characters.Wide_Wide_Latin_1.HT | Ada.Characters.Wide_Wide_Latin_1.FF | ' ' and
State in Equal
then
State := In_Value;
Append (Value, Text (I));
end if;
Text.Next (I);
end loop;
end Parse_Properties_File;
procedure Write_Properties_File
(File_Name : String;
Content : Property_List)
is
Raw_File : UXStrings.Text_IO.File_Type;
type Escape_Space_Type is (No, Start, Full);
procedure Escaped_Write
(Str : String;
Escape_Space : Escape_Space_Type;
Multi_Comment : Boolean := False)
is
Space : Boolean := Escape_Space = Start;
use UXStrings.Formatting;
use all type UXStrings.Formatting.Alignment;
function Format is new Integer_Format (Natural);
function Format_U16
(Item : Natural;
Base : in Number_Base := 16;
PutPlus : in Boolean := False;
Field : in Natural := 4;
Justify : in Alignment := Right;
Fill : in Character := '0')
return UXString renames Format;
begin
for I in 1 .. Length (Str) loop
if Element (Str, I) = Ada.Characters.Wide_Wide_Latin_1.HT then
UXStrings.Text_IO.Put (Raw_File, '\');
UXStrings.Text_IO.Put (Raw_File, 't');
Space := False;
elsif Element (Str, I) = Ada.Characters.Wide_Wide_Latin_1.LF then
if Multi_Comment then
UXStrings.Text_IO.New_Line (Raw_File);
UXStrings.Text_IO.Put (Raw_File, '#');
else
UXStrings.Text_IO.Put (Raw_File, '\');
UXStrings.Text_IO.Put (Raw_File, 'n');
end if;
Space := False;
elsif Element (Str, I) in '=' | ':' | '#' | '!' | '\' and Escape_Space /= No then
UXStrings.Text_IO.Put (Raw_File, '\');
UXStrings.Text_IO.Put (Raw_File, Element (Str, I));
Space := False;
elsif Element (Str, I) = ' ' and (Space or Escape_Space = Full) then
UXStrings.Text_IO.Put (Raw_File, '\');
UXStrings.Text_IO.Put (Raw_File, ' ');
Space := False;
elsif Element (Str, I) < Ada.Characters.Wide_Wide_Latin_1.DEL then
UXStrings.Text_IO.Put (Raw_File, Element (Str, I));
Space := False;
else
UXStrings.Text_IO.Put (Raw_File, '\');
UXStrings.Text_IO.Put (Raw_File, 'u');
declare
Hex4 : constant String := Format_U16 (BMP_Character'Pos (Get_BMP (Str, I, '?')));
begin
for C of Hex4 loop
UXStrings.Text_IO.Put (Raw_File, C);
end loop;
end;
Space := False;
end if;
end loop;
end Escaped_Write;
begin
UXStrings.Text_IO.Create (Raw_File, UXStrings.Text_IO.Out_File, File_Name, Latin_1, UXStrings.Text_IO.LF_Ending);
for C in Content.Iterate loop
if Content_Maps.Element (C).Comment /= Null_UXString then
UXStrings.Text_IO.Put (Raw_File, '#');
Escaped_Write (Content_Maps.Element (C).Comment, No, True);
UXStrings.Text_IO.New_Line (Raw_File);
end if;
Escaped_Write (Content_Maps.Key (C), Full);
UXStrings.Text_IO.Put (Raw_File, '=');
Escaped_Write (Content_Maps.Element (C).Text, Start);
UXStrings.Text_IO.New_Line (Raw_File);
end loop;
UXStrings.Text_IO.Close (Raw_File);
end Write_Properties_File;
procedure Read
(Properties : out Property_List;
File_Name : String)
is
begin
if Tail (File_Name, 8, ' ') = ".strings" then
Parse_Strings_File (File_Name, Properties);
end if;
if Tail (File_Name, 11, ' ') = ".properties" then
Parse_Properties_File (File_Name, Properties);
end if;
end Read;
procedure Write
(Properties : Property_List;
File_Name : String)
is
begin
if Tail (File_Name, 8, ' ') = ".strings" then
Write_Strings_File (File_Name, Properties);
end if;
if Tail (File_Name, 11, ' ') = ".properties" then
Write_Properties_File (File_Name, Properties);
end if;
end Write;
function Keys
(Properties : Property_List)
return Key_List
is
begin
return Result_Key_List : Key_List do
for Index in Properties.Iterate loop
Result_Key_List.Append (Content_Maps.Key (Index));
end loop;
end return;
end Keys;
function Contains
(Properties : Property_List;
Key : String)
return Boolean is (Content_Maps.Contains (Content_Maps.Map (Properties), Key));
procedure Insert
(Properties : in out Property_List;
Key : String)
is
begin
Properties.Insert (Key, (Null_UXString, Null_UXString, False));
end Insert;
procedure Delete
(Properties : in out Property_List;
Key : String)
is
begin
if Properties.Find (Key) /= Content_Maps.No_Element then
Properties.Delete (Key);
end if;
end Delete;
procedure Rename
(Properties : in out Property_List;
From, To : String)
is
begin
Properties.Insert (To, Properties.Element (From));
Properties.Delete (From);
end Rename;
function Text
(Properties : Property_List;
Key : String)
return String
is
begin
if Properties.Find (Key) = Content_Maps.No_Element then
return "";
else
return Properties.Element (Key).Text;
end if;
end Text;
procedure Text
(Properties : in out Property_List;
Key : String;
Value : String)
is
Cursor : constant Content_Maps.Cursor := Properties.Find (Key);
Element : Property_Type;
begin
if Cursor /= Content_Maps.No_Element then
Element := Content_Maps.Element (Cursor);
Element.Text := Value;
if Element.Text /= Content_Maps.Element (Cursor).Text then
Element.Modified := True;
Properties.Replace_Element (Cursor, Element);
end if;
end if;
end Text;
function Comment
(Properties : Property_List;
Key : String)
return String
is
begin
if Properties.Find (Key) = Content_Maps.No_Element then
return Null_UXString;
else
return Properties.Element (Key).Comment;
end if;
end Comment;
procedure Comment
(Properties : in out Property_List;
Key : String;
Value : String)
is
Cursor : constant Content_Maps.Cursor := Properties.Find (Key);
Element : Property_Type;
begin
if Cursor /= Content_Maps.No_Element then
Element := Content_Maps.Element (Cursor);
Element.Comment := Value;
if Element.Comment /= Content_Maps.Element (Cursor).Comment then
Element.Modified := True;
Properties.Replace_Element (Cursor, Element);
end if;
end if;
end Comment;
function Modified
(Properties : Property_List;
Key : String)
return Boolean
is
begin
if Properties.Find (Key) = Content_Maps.No_Element then
return False;
else
return Properties.Element (Key).Modified;
end if;
end Modified;
procedure Reset_Modified_Indicators (Properties : in out Property_List) is
begin
for Element of Properties loop
Element.Modified := False;
end loop;
end Reset_Modified_Indicators;
function Selected_Keys
(Master, Locale : Property_List;
Pattern : String)
return Key_List
is
package Keys_Sorting is new Lists.Generic_Sorting;
begin
if Pattern /= "" then
return Result_Key_List : Key_List do
for Cursor in Master.Iterate loop
if
(Index (Content_Maps.Key (Cursor), Pattern) > 0 or
Index (Content_Maps.Element (Cursor).Text, Pattern) > 0 or
Index (Content_Maps.Element (Cursor).Comment, Pattern) > 0) and
not Result_Key_List.Contains (Content_Maps.Key (Cursor))
then
Result_Key_List.Append (Content_Maps.Key (Cursor));
end if;
end loop;
for Cursor in Locale.Iterate loop
if
(Index (Content_Maps.Key (Cursor), Pattern) > 0 or
Index (Content_Maps.Element (Cursor).Text, Pattern) > 0 or
Index (Content_Maps.Element (Cursor).Comment, Pattern) > 0) and
not Result_Key_List.Contains (Content_Maps.Key (Cursor))
then
Result_Key_List.Append (Content_Maps.Key (Cursor));
end if;
end loop;
end return;
else
return Result_Key_List : Key_List := Keys (Master) do
for Cursor in Locale.Iterate loop
if not Result_Key_List.Contains (Content_Maps.Key (Cursor)) then
Result_Key_List.Append (Content_Maps.Key (Cursor));
end if;
end loop;
Keys_Sorting.Sort (Result_Key_List);
end return;
end if;
end Selected_Keys;
end Localize.Parser;
|