forked from christian-vigh-phpclasses/IniFile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIniFile.class.php
More file actions
executable file
·2294 lines (1738 loc) · 73.3 KB
/
IniFile.class.php
File metadata and controls
executable file
·2294 lines (1738 loc) · 73.3 KB
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/***************************************************************************************************
NAME
IniFile.phpclass
DESCRIPTION
Ini-File management system.
AUTHOR
Christian Vigh, 01/2010.
HISTORY
[Version : 1.0] [Date : 2010/08/22] [Author : CV]
Initial release.
[Version : 1.0.1] [Date : 2014/11/06] [Author : CV]
. Fixed some bugs on property names in Save().
. Added the $keys_by_reference boolean parameter to GetKeys(). When true (the default, and
also the original behavior), key values are returned by reference. When false, a copy is
returned.
[Version : 1.0.2] [Date : 2014/12/06] [Author : CV]
. Swapped the $file and $forced parameters of the Save() method.
[Version : 1.0.3] [Date : 2014/12/06] [Author : CV]
. Changed the GetKeys() method to return an empty array if the section does not exist.
[Version : 1.0.4] [Date : 2015/03/28] [Author : CV]
. Added a regex parameter to the GetSections() method.
. Added a regex parameter to the GetKeys() method.
[Version : 1.0.5] [Date : 2015/04/07] [Author : CV]
. Changed the __Load() method to allow spaces after the ending keyword of a
multiline entry.
[Version : 1.0.6] [Date : 2015/04/08] [Author : CV]
. Added the GetBooleanKey() function
[Version : 1.0.7] [Date : 2015/04/10] [Author : CV]
. Corrected a bug in the __Load() method that made current line number tracking erroneous.
Note that displayed error line may differ from the actual one by an index of 1.
. Changed the __SetPosition() method not to modify the current line number, which is now
completely tracked by the __Load() method.
[Version : 1.0.8] [Date : 2015/04/11] [Author : CV]
. Added the SetVariableStore() and __SetVariableStore() methods.
. Changed the GetKey() and GetKeys() methods to use optional variable store for variable
expansion.
. Changed the GetKey() method so that several key aliases can be specified as an array for
the $key parameter.
[Version : 1.0.9] [Date : 2015/06/12] [Author : CV]
. Changed the __Load() method so that the equal sign is not mandatory for a key with an
empty value. Thus, the key :
[SomeSection]
somekey
becomes equivalent to :
[SomeSection]
somekey =
[Version : 1.0.10] [Date : 2015/06/12] [Author : CV]
. Changed the Load*() and Append*() methods to include an additional parameter,
$separator (default is '='), to specify the separator string between a key and its value.
. Authorize spaces in key names
[Version : 1.0.11] [Date : 2015/06/12] [Author : CV]
. $this -> EOL and $this -> CRLF were handled inconsistently.
. Spaces before the equal sign for single-line entries where not preserved
[Version : 1.0.12] [Date : 2015/10/07] [Author : CV]
. Corrected the __Load() method, which did not recognize key definitions preceded by
spaces or tabs.
[Version : 1.0.13] [Date : 2015/10/25] [Author : CV]
. Since version 1.0.9, spaces to the left of the equal sign were not preserved when reading
.ini files.
[Version : 1.0.14] [Date : 2015/11/17] [Author : CV]
. Added the $options parameter to the class constructor and the Loadxxx/Appendxxx() functions.
This allows for specifying compqtibility options with parse_ini_file(), for example for
quoted string values.
. Fixed some inconsistencies with $separator parameter handling.
[Version : 1.0.15] [Date : 2015/11/26] [Author : CV]
. Added the GetIntegerKey() method.
[Version : 1.0.16] [Date : 2017/04/07] [Author : CV]
. Added the GetListKey() method.
***************************************************************************************************/
require_once ( 'Variables.phpclass' ) ;
// Determine if we run under Windows or Unix
if ( ! defined ( 'IS_WINDOWS' ) )
{
if ( ! strncasecmp ( php_uname ( 's' ), 'windows', 7 ) )
{
define ( 'IS_WINDOWS' , 1 ) ;
define ( 'IS_UNIX' , 0 ) ;
}
else
{
define ( 'IS_WINDOWS' , 0 ) ;
define ( 'IS_UNIX' , 1 ) ;
}
}
/*===========================================================================================
IniFile class -
.INI file management. This class tries as much as possible to preserve the original
text formatting including comments when data is loaded from an existing .INI file.
This means that if you use the Save() method to save .INI file contents, the generated
file will have exactly the same contents as the original file (except for the
modifications you made between loading and saving), and comments will be preserved.
The differences with a real .INI file are the following :
- Comments :
. A mono-line comment can be introduced with the '#', ';' and '//' construct
. A multi-line comment is enclosed between '/*' and '* /'.
- You cannot put comments at the end of a section key definitions. Comments are
considered to be part of the key value.
- Section key definitions can be multiline. In that case, you have to use the '<<'
construct as in the following example :
mykey =<<
some text for my key value
(continued)
END
By default, a multiline key definition ends with a line containing only the word 'END'.
You can specify however a different termination, after the '<<' construct :
mykey =<<KEYEND
some text for my key value
(continued)
KEYEND
In both examples, the value of the 'mykey' key will be :
"some text for my key value{EOL}(continued)"
The "{EOL}" string will be either "\n" on Unix systems or "\r\n" on Windows systems.
However, if the IniFile object has been created with an existing .INI file
contents, the EOL string will be that of the initial file.
Key definitions can be stored outside any section definition (ie, at the very beginning
of the .INI file). In that case, they are placed in an section whose name is the
empty string ("").
===========================================================================================*/
class IniFile // extends Object
{
// Ini entry types
const INI_COMMENT = "any" ; // Comment or spaces
const INI_SECTION = "section" ; // Section name
const INI_ENTRY = "entry" ; // Section entry
// Load disposition option
const LOAD_ANY = 0 ; // The .ini file is loaded if it exists, or will be created
const LOAD_EXISTING = 1 ; // The .ini file is loaded. If it does not exist, an error will occur
const LOAD_NEW = 2 ; // The .ini file is recreated, whether existing or not
// Key definitions alignment options
const ALIGN_NONE = 0 ; // No alignment
const ALIGN_SECTION = 1 ; // Aligns the equal signs within a section only
const ALIGN_FILE = 2 ; // Aligns the equal signs within the whole file
// Compatibility options with parse_ini_file()
const OPTION_NONE = 0x0000 ; // No particular options - default behavior
const OPTION_COMPATIBILITY_QUOTED_VALUES = 0x0001 ; // When set, quotes are removed from values
// Ini file path
public $File = null ;
// Reformat on save (align definitions)
private $AlignmentOption = self::ALIGN_NONE ;
// Ini file items
private $Items = array ( ) ;
// EOL type
private $CRLF ;
private $EOL ;
// Dirty flag
private $Dirty = false ;
// Optional variable store
public $Variables = false ;
// Separator between a key and a value
public $Separator = '=' ;
// INI file options
public $Options ;
/*********************************************************************************************/
/*********************************************************************************************/
/*********************************************************************************************/
/****** ******/
/****** CONSTRUCTOR & MAGIC METHODS ******/
/****** ******/
/*********************************************************************************************/
/*********************************************************************************************/
/*********************************************************************************************/
// Constructor : does nothing. A IniFile object must be created by using the LoadFromxxx methods
public function __construct ( $separator = '=', $options = self::OPTIONS_NONE )
{
// Determine the current EOL string, depending on OS type.
// This setting may be overriden if .INI file contents coming from a different OS are loaded.
if ( IS_WINDOWS )
{
$this -> CRLF = true ;
$this -> EOL = "\r\n" ;
}
else
{
$this -> CRLF = false ;
$this -> EOL = "\n" ;
}
$this -> Separator = $separator ;
$this -> Options = $options ;
}
// Conversion to string : returns the contents of the .INI file
public function __tostring ( )
{ return ( $this -> AsString ( ) ) ; }
/*********************************************************************************************/
/*********************************************************************************************/
/*********************************************************************************************/
/****** ******/
/****** PRIVATE FUNCTIONS ******/
/****** ******/
/*********************************************************************************************/
/*********************************************************************************************/
/*********************************************************************************************/
//
// __AddTextBlock -
// Adds a text block in the chain of .INI file contents. This can include comments but also,
// more simply, newline separators between a section name and the next line
//
private function __AddTextBlock ( $value )
{
if ( strlen ( $value ) )
$this -> Items [] = array ( 'type' => self::INI_COMMENT, 'value' => $value ) ;
return ( "" ) ;
}
//
// __Compact -
// Called when elements from the $this -> Items array are unset, to remove empty slots
//
private function __Compact ( )
{
$this -> Items = array_values ( $this -> Items ) ;
}
//
// __EOLReplace -
// Internally, end of line inidicators are stored as "\n", whatever the original .INI file format
// (Windows or Unix). This function restores the original EOL indicator.
//
private function __EOLReplace ( $value )
{
if ( $this -> CRLF )
$value = str_replace ( "\n", "\r\n", $value ) ;
return ( $value ) ;
}
//
// __FindKey -
// Searches for the specified key in the specified section. Returns the key index in the
// $this -> Items array, or false if key not found.
//
private function __FindKey ( $section, $key )
{
$section = $this -> __NormalizeName ( $section ) ;
$key = $this -> __NormalizeName ( $key ) ;
$index = $this -> __FindSection ( $section ) ;
if ( $index === false )
return ( false ) ;
for ( $i = $index + 1 ; $i < count ( $this -> Items ) ; $i ++ )
{
$item = $this -> Items [$i] ;
if ( $item [ 'type' ] == self::INI_ENTRY &&
! strcasecmp ( $item [ 'name'], $key ) )
return ( $i ) ;
else if ( $item [ 'type' ] == self::INI_SECTION )
break ;
}
return ( false ) ;
}
//
// __FindSection -
// Searches for the specified section. Returns the section index in the $this -> Items array,
// or false if section not found.
//
private function __FindSection ( $name )
{
$name = $this -> __NormalizeName ( $name ) ;
$index = 0 ;
foreach ( $this -> Items as $item )
{
if ( $item [ 'type' ] == self::INI_SECTION &&
! strcasecmp ( $item [ 'value'], $name ) )
return ( $index ) ;
$index ++ ;
}
return ( false ) ;
}
//
// __Load -
// Does the real job of parsing the input file or string contents.
// The '$file' parameter is used only when displaying error messages.
//
// Notes :
// . If the input string contains duplicate section names, their contents will be merged
// . If the input string contains duplicate key names, subsequent key definitions will
// override the original one.
//
private function __Load ( $contents, $file = null, $separator = '=', $options = self::OPTION_NONE )
{
if ( ! $file )
$file = '(string)' ;
$key_value_separator = ( $separator ) ? $separator : '=' ;
$single_re = '/^
(?P<name> [^' . $key_value_separator . ']+)
(
(?P<sep> \s*' . $key_value_separator . '\s*)
(?P<value> .*)
)?
$/isx' ;
$multi_re = '/^' .
'(?P<name> [^' . $key_value_separator . ']+)' .
'(?P<sep> \s*' . $key_value_separator . '\s* \<\<\<? \s* (?P<word> [^\s]*) .* )' .
'$/isx' ;
// Check if we have a Unix or Windows file
$crlf = ( strpos ( $contents, "\r\n" ) !== false ) ;
if ( $crlf ) // Windows file
{
// The input string contains "\r\n" end-of-line characters ; replace them with a single "\n"
// This is used to simplify input string parsing
$this -> CRLF = true ;
$this -> EOL = "\r\n" ;
$contents = str_replace ( "\r", '', $contents ) ;
}
else // Unix file
{
$this -> CRLF = false ;
$this -> EOL = "\n" ;
}
// Some initializations
$contents_length = strlen ( $contents ) ; // Length of the input string
$text_value = "" ; // Contains parsed comments and newlines, anything that is not part of a
// section or key definitions
$line = 0 ; // Current line and char in line during parsing
$char = 1 ; // (used only when displaying error messages)
$section = null ; // Current section name
$errhead = "" ; // Prefix string used when displaying error messages
// Parse input string
$i_start = 0 ;
for ( $i = 0 ; $i < $contents_length ; $i ++ )
{
// Get current and next chars
$ch = $contents [$i] ;
$chnext = ( $i + 1 < $contents_length ) ? $contents [$i+1] : null ;
// Complain if we have a construct other than '//' or '/*'
if ( $ch == '/' && $chnext != '*' && $chnext != '/' )
throw ( new Exception ( "$errhead Comment character '/' not followed by single-line comment character ('/') or multiline comment character (*)." ) ) ;
// Single-line comment
if ( $ch == ';' || $ch == '#' || ! strncmp ( substr ( $contents, $i, 2 ), '//', 2 ) )
{
// Locate the end of line
$end = strpos ( $contents, "\n", $i ) ;
// If no end of line, this means that we are on the last line of the file
if ( $end === false )
{
$text_value .= substr ( $contents, $i ) ;
$i = $contents_length ;
}
// Otherwise extract the comment portion and update current char and current index accordingly
else
{
$text_value .= substr ( $contents, $i, $end - $i ) ;
$i = $end ;
$ch = substr ( $contents, $i, 1 ) ;
}
}
// Multiline comment (note : nested multiline comments are not allowed)
else if ( ! strncmp ( substr ( $contents, $i, 2 ), "/*", 2 ) )
{
$end = strpos ( $contents, "*/", $i ) ;
// no comment end found : complain
if ( $end === false )
throw ( new Exception ( "$errhead Unterminated multiline comment." ) ) ;
// Otherwise extract the comment part. Consecutive comments are catenated
else
{
$str = substr ( $contents, $i, $end - $i + 1 ) ;
for ( $j = 1 ; $j < strlen ( $str ) ; $j ++ )
{
$sch = $str [$j] ;
$this -> __SetPosition ( $file, $sch, $line, $char, $errhead ) ;
}
$text_value .= $str ;
$i = $end + 1 ;
$ch = substr ( $contents, $i, 1 ) ;
}
}
// Opening bracket : this is a section name
else if ( $ch == '[' )
{
// Add any comments that have been found so far
$text_value = $this -> __AddTextBlock ( $text_value ) ;
// Locate the closing bracket
$end = strpos ( $contents, ']', $i ) ;
// Complain if not found
if ( $end === false )
throw ( new Exception ( "$errhead Unfinished section start." ) ) ;
// Extract the section name
$section = trim ( substr ( $contents, $i + 1, $end - $i - 1 ) ) ;
$section = $this -> __NormalizeName ( $section ) ;
// If section does not already exist, append it to the Items array
if ( $this -> __FindSection ( $section ) === false )
$this -> Items [] = array ( 'type' => self::INI_SECTION, 'value' => $section ) ;
$i = $end + 1 ;
// This code is for the case where "[section_name]" represents the last characters of the input string,
// without a terminating newline
if ( $i < $contents_length )
$ch = substr ( $contents, $i, 1 ) ;
}
// if we fall here, we are arriving on a key=value definition (if the line starts with spaces, they will be
// included in the preceding comment block)
else if ( $ch > ' ' )
{
// If $section is null, this means that we have not encountered a section name so far.
// In that case, we create the unnamed section to store the key definitions that are at the beginning
// of the input string
if ( $section === null )
{
$section = "" ;
$this -> Items [] = array ( 'type' => self::INI_SECTION, 'value' => "" ) ;
}
// Regular expressions to match a single-line key definition, or the start of multiline one
// Add any comments encountered so far
$text_value = $this -> __AddTextBlock ( $text_value ) ;
// Find the end of the key definition line
// If no newline found, this means that the definition is at the last line of the file and
// that it is not terminated with a newline character
$nlpos = strpos ( $contents, "\n", $i ) ;
if ( $nlpos === false )
$nlpos = $contents_length ;
// Extract the key definition and update the pointer accordingly
$entry = substr ( $contents, $i, $nlpos - $i ) ;
$i = $nlpos ;
// Multiline definition
if ( preg_match ( $multi_re, $entry, $matches ) )
{
$name = trim ( $matches [ 'name' ] ) ;
preg_match ( '/(?P<spaces> \s*)$/x', $matches [ 'name' ], $spmatch ) ;
$separator = $spmatch [ 'spaces' ] . $matches [ 'sep' ] ;
$word = trim ( $matches [ 'word' ] ) ;
$multiline = true ;
$quoted = false ;
$closing = strpos ( $contents, "\n$word", $i ) ;
if ( $closing == false ) // Closing delimiter is on end of file, no trailing newline
throw ( new Exception ( "$errhead Unterminated multiline entry '$name'." ) ) ;
// Extract definition and update pointer to current char
$value = substr ( $contents, $i + 1, $closing - $i - 1 ) ;
$i += strlen ( $value ) + strlen ( $word ) + 1 ;
// Allow for spaces after the ending keyword
while ( $i < $contents_length && $contents [$i] != "\n" && ctype_space ( $contents [$i] ) )
$i ++ ;
if ( $i < $contents_length ) // Skip ending newline
$i ++ ;
}
// Single-line definition
else if ( preg_match ( $single_re, $entry, $matches ) )
{
$name = trim ( $matches [ 'name' ] ) ;
preg_match ( '/(?P<spaces> \s*)$/x', $matches [ 'name' ], $spmatch ) ;
$separator = ( isset ( $matches [ 'sep' ] ) ) ? $spmatch [ 'spaces' ] . $matches [ 'sep' ] : $key_value_separator ;
$value = ( isset ( $matches [ 'value' ] ) ) ? $matches [ 'value' ] : '' ;
$word = "" ;
$multiline = false ;
$quoted = ( $this -> Options & self::OPTION_COMPATIBILITY_QUOTED_VALUES ) ?
String::RemoveDelimiters ( $value, '"' ) : false ;
}
// Neither multiline, nor single-line : complain
else
{
$this -> __SetPosition ( $file, '', $line+1, $char, $errhead ) ;
throw ( new Exception ( "$errhead Invalid entry :\n\t$entry." ) ) ;
}
// Build the new item entry
$vch = substr ( $value, 0, 1 ) ;
if ( $vch == ' ' || $vch == "\t" )
$value = substr ( $value, 1 ) ;
$value = rtrim ( $value ) ;
$item = array
(
'type' => self::INI_ENTRY,
'section' => $section,
'name' => $this -> __NormalizeName ( $name ),
'separator' => $separator,
'value' => $value,
'multiline' => $multiline,
'word' => $word,
'quoted' => $quoted
) ;
// Check if the key already exists
$key_index = $this -> __FindKey ( $section, $name ) ;
// If yes, replace the original definition with the current one
if ( $key_index !== false )
$this -> Items [ $key_index ] = $item ;
// Otherwise, append it to our list of .INI file items
else
$this -> Items [] = $item ;
// This code is for the case where the key definition represents the last characters of the input string,
// without a terminating newline
if ( $i < $contents_length )
$ch = $contents [$i] ;
}
// Update current line
$delta = $i - $i_start ;
if ( $delta )
{
$line_count = substr_count ( $contents, "\n", $i_start, $delta ) ;
$line += $line_count ;
}
$i_start = $i ;
// All possible .INI file element types have been processed ; update the error message prefix
if ( $i < $contents_length )
{
$text_value .= $ch ;
$this -> __SetPosition ( $file, $ch, $line, $char, $errhead ) ;
}
}
// Create a new comment block if remaining comments or end of lines have been found
$text_value = $this -> __AddTextBlock ( $text_value ) ;
}
//
// __NormalizeName -
// Normalizes a section or key name : removes enclosing spaces and duplicate spaces within the name.
//
private function __NormalizeName ( $name )
{
$name = trim( $name ) ;
$name = preg_replace ( '/\s+/', ' ', $name ) ;
return ( $name ) ;
}
//
// __SetPosition -
// Sets the prefix of the error message to be displayed upon error.
//
private function __SetPosition ( $file, $ch, $line, &$char, &$errhead )
{
$file = basename ( $file ) ;
if ( $ch == "\n" || $ch == '' )
{
$line ++ ;
$char = 1 ;
}
else
$char ++ ;
$errhead = "$file: line $line, char $char :" ;
}
/*********************************************************************************************/
/*********************************************************************************************/
/*********************************************************************************************/
/****** ******/
/****** PUBLIC FUNCTIONS ******/
/****** ******/
/*********************************************************************************************/
/*********************************************************************************************/
/*********************************************************************************************/
/*-------------------------------------------------------------------------------------------
NAME
AlignDefinitions - Aligns the key definitions.
PROTOTYPE
$inifile -> AlignDefinition ( $align_option = null ) ;
DESCRIPTION
Aligns the key definitions within .INI file sections, so that the equal signs before
the key values remain aligned.
PARAMETERS
$align_option -
Can be anyone of the following values :
ALIGN_NONE -
No alignment is performed, the initial key definitions are left as is.
ALIGN_SECTION -
Equal signs in key definitions are aligned on the same column, but
only at the section level.
ALIGN_FILE -
Equal signs in key definitions are align on the same column at a file-level.
null (default value) -
The alignment option is taken from the value specified with the
SetAlignment() method. The default value is ALIGN_NONE.
--------------------------------------------------------------------------------------------*/
public function AlignDefinitions ( $option = null )
{
// Get the actual alignment option
if ( $option === null )
$option = $this -> AlignmentOption ;
if ( $option == self::ALIGN_NONE )
return ;
// Get the sections list
$sections = $this -> GetSections ( ) ;
// If alignment at the file level, compute the maximum length of a key name
if ( $option == self::ALIGN_FILE )
{
$maxlength = - 1 ;
$items = $this -> GetAllKeys ( ) ;
foreach ( $items as $section => $keys )
{
foreach ( $keys as $name => $value )
{
$length = strlen ( $name ) ;
if ( $maxlength < $length )
$maxlength = $length ;
}
}
}
// Loop through each section of the .INI file
foreach ( $sections as $section )
{
$keys = $this -> GetKeys ( $section ) ;
// If alignment at the section level, compute the maximum length of a key name for that section
if ( $option == self::ALIGN_SECTION )
{
$maxlength = -1 ;
foreach ( $keys as $key => $value )
{
$length = strlen ( $key ) ;
if ( $length > $maxlength )
$maxlength = $length ;
}
}
// Now, for each key in the current section, adjust the value of the 'separator' entry
// to make sure that all equal signs will be aligned
foreach ( $keys as $key => $value )
{
$index = $this -> __FindKey ( $section, $key ) ;
$item = &$this -> Items [ $index ] ;
$sep = preg_replace ( '/\s*=\s*/', ' = ', $item [ 'separator' ] ) ;
$length = $maxlength - strlen ( $item [ 'name' ] ) ;
if ( $length > 0 )
$sep = str_repeat ( ' ', $length ) . $sep ;
$item [ 'separator' ] = $sep ;
}
}
}
/*-------------------------------------------------------------------------------------------
NAME
AppendFromArray,
AppendFromFile,
AppendFromString - Appends .INI definitions
PROTOTYPE
$status = $inifile -> AppendFromArray ( $array ) ;
$status = $inifile -> AppendFromFile ( $file ) ;
$status = $inifile -> AppendFromString ( $string ) ;
DESCRIPTION
Appends definitions from the specified .INI file contents.
PARAMETERS
$array (array of strings) -
.INI file definitions. The EOL string is determined by the OS version.
$file (string) -
File to be read.
$string (string) -
String to be read.
RETURNS
True if the operation was successful, false otherwise.
--------------------------------------------------------------------------------------------*/
public function AppendFromArray ( $array, $options = self::OPTION_NONE )
{
$this -> Dirty = true ;
return ( $this -> __Load ( implode ( $this -> EOL, $array ), null, $this -> Separator, $options ) ) ;
}
public function AppendFromFile ( $file, $options = self::OPTION_NONE )
{
global $Application ;
$inifile = $Application -> GetAbsolutePath ( $file ) ;
if ( file_exists ( $inifile ) )
{
$this -> Dirty = true ;
return ( $this -> __Load ( file_get_contents ( $file ), $file, $this -> Separator, $options ) ) ;
}
else
return ( false ) ;
}
public function AppendFromString ( $string, $options = self::OPTION_NONE )
{
$this -> Dirty = true ;
return ( $this -> __Load ( $string, null, $this -> Separator, $options ) ) ;
}
/*-------------------------------------------------------------------------------------------
NAME
AppendSection - Appends a section to the current .INI file.
PROTOTYPE
$inifile -> AppendSection ( $section, $comment_before = null, $comment_after = null ) ;
DESCRIPTION
Appends a section to the .INI file.
PARAMETERS
$section (string) -
Section name. If the section already exists, nothing happens.
$comment_before, $comment_after (string) -
Comments to be appended before and after the section name (optional).
--------------------------------------------------------------------------------------------*/
public function AppendSection ( $section, $comment_before = null, $comment_after = null )
{
$section = $this -> __NormalizeName ( $name ) ;
$index = $this -> __FindSection ( $section ) ;
if ( $index !== false )
return ;
if ( $comment_before !== null )
$this -> __AddTextBlock ( $comment_before . "\n", false ) ;
$this -> Items [] = array ( 'type' => self::INI_SECTION, 'value' => $section ) ;
$this -> __AddTextBlock ( "\n", false ) ;
if ( $comment_after !== null )
$this -> __AddTextBlock ( $comment_after . "\n", false ) ;
$this -> Dirty = true ;
return ( true ) ;
}
/*-------------------------------------------------------------------------------------------
NAME
AsString - Returns the .INI file as a string
PROTOTYPE
$text = $inifile -> AsString ( $full = true ) ;
DESCRIPTION
Returns the contents of the .INI file as a string.
PARAMETERS
$full (boolean) -
If true (the default), the initial .INI file comments will be included in the
result.
--------------------------------------------------------------------------------------------*/
public function AsString ( $full = true )
{
$result = "" ;
$this -> AlignDefinitions ( ) ;
// Loop through items
foreach ( $this -> Items as $item )
{
switch ( $item [ 'type'] )
{
// Comment block or newline separator
case self::INI_COMMENT :
if ( ! $full )
break ;
$value = $item [ 'value' ] ;
if ( $this -> CRLF )
$value = $this -> __EOLReplace ( $value ) ;
$result .= $value ;
break ;
// Section name
case self::INI_SECTION :
if ( $item [ 'value' ] )
$result .= '[' . $item [ 'value' ] . ']' ;
break ;
// Section entry. Handle multiline and single-line keys
case self::INI_ENTRY :
if ( $item [ 'multiline'] )
{
$value = $this -> __EOLReplace ( $item [ 'value' ] ) ;
$result .= $item [ 'name' ] . $item [ 'separator' ] . $this -> EOL .
$value . $this -> EOL .
$item [ 'word' ] ;
}
else
{
if ( ( $this -> Options & self::OPTION_COMPATIBILITY_QUOTED_VALUES ) && $item [ 'quoted' ] )
$value = String::QuotedString ( $item [ 'value' ], '"', true ) ;
else
$value = $item [ 'value' ] ;
$result .= $item [ 'name' ] . $item [ 'separator' ] . $value ;
}
break ;
default :
throw ( new Exception ( "Unknow entry type '" . $item [ 'type'] . "'." ) ) ;
}
}
// All done, return
return ( $result ) ;
}
/*-------------------------------------------------------------------------------------------
NAME
ClearKey - Clears a key value.
PROTOTYPE
$inifile -> ClearKey ( $section, $key ) ;
DESCRIPTION
Clears a key value. This is the equivalent of calling :
$inifile -> SetKey ( $section, $key, "" ) ;
PARAMETERS
$section (string) -
Section name.
$key (string) -
Key name.
RETURN VALUE
True if the section/key pair exists, false otherwise.
--------------------------------------------------------------------------------------------*/
public function ClearKey ( $section, $key )
{
$index = $this -> __FindKey ( $section, $key ) ;
if ( $index !== false )
{
$item = &$this -> Items [ $index ] ;
$item [ 'value' ] = "" ;
$item [ 'multiline' ] = false ;
$this -> Dirty = true ;
return ( true ) ;
}
else
return ( false ) ;
}
/*-------------------------------------------------------------------------------------------
NAME
ClearSection - Clears a section contents.
PROTOTYPE
$status = $inifile -> ClearSection ( $section ) ;
DESCRIPTION
Clears a section contents, without removing the section name from the .INI file.
PARAMETERS
$section (string) -
Section name. Specify an empty string ("") for the unnamed global section.
RETURN VALUE -
true if the section exists, false otherwise.
--------------------------------------------------------------------------------------------*/
public function ClearSection ( $section )
{
// Find the section
$index = $this -> __FindSection ( $section ) + 1 ;
$count = count ( $this -> Items ) ;
if ( $index === false )
return ( false ) ;
// Isolate section contents in the Items array