-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFl_Preferences.cxx
2007 lines (1787 loc) · 61.4 KB
/
Fl_Preferences.cxx
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
//
// Preferences methods for the Fast Light Tool Kit (FLTK).
//
// Copyright 2002-2010 by Matthias Melcher.
// Copyright 2011-2024 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
// https://www.fltk.org/COPYING.php
//
// Please see the following page on how to report bugs and issues:
//
// https://www.fltk.org/bugs.php
//
#include <FL/Fl.H>
#include "Fl_System_Driver.H"
#include <FL/Fl_Preferences.H>
#include <FL/Fl_Plugin.H>
#include <FL/filename.H>
#include <FL/fl_utf8.h>
#include <FL/fl_string_functions.h>
#include "flstring.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#if (FLTK_USE_STD)
#include <string>
#endif
char Fl_Preferences::nameBuffer[128];
char Fl_Preferences::uuidBuffer[40];
Fl_Preferences *Fl_Preferences::runtimePrefs = 0;
unsigned int Fl_Preferences::fileAccess_ = Fl_Preferences::ALL;
static int clocale_snprintf(char *buffer, size_t buffer_size, const char *format, ...)
{
va_list args;
va_start(args, format);
int retval = Fl::system_driver()->clocale_vsnprintf(buffer, buffer_size, format, args);
va_end(args);
return retval;
}
static int clocale_sscanf(const char *input, const char *format, ...)
{
va_list args;
va_start(args, format);
int retval = Fl::system_driver()->clocale_vsscanf(input, format, args);
va_end(args);
return retval;
}
/**
Returns a UUID as generated by the system.
A UUID is a "universally unique identifier" which is commonly used in
configuration files to create identities. A UUID in ASCII looks like this:
<tt>937C4900-51AA-4C11-8DD3-7AB59944F03E</tt>. It has always 36 bytes plus
a trailing zero.
\return a pointer to a static buffer containing the new UUID in ASCII format.
The buffer is overwritten during every call to this function!
*/
const char *Fl_Preferences::new_UUID() {
Fl::system_driver()->newUUID(uuidBuffer);
return uuidBuffer;
}
/**
Tell the FLTK preferences system which files in the file system it may read, create, or write.
The FLTK core library will try to read or even create or write preference files
when calling Fl::option(), Fl_File_Chooser, the printing panel, and possibly
some other internal functions. If your application wants to keep FLTK from
touching the file system, call this function before making any other FLTK calls:
\code
// neither FLTK nor the app may read, create, or write preference files
Fl_Preferences::file_access( Fl_Preferences::NONE );
\endcode
or
\code
// FLTK may not read, create, or write preference files, but the application may
Fl_Preferences::file_access( Fl_Preferences::APP_OK );
\endcode
All flags can be combined using an OR operator. If flags are not set, that
specific access to the file system will not be allowed. By default, all access
is granted. To clear one or more flags from the default setting, use:
\code
Fl_Preferences::file_access( Fl_Preferences::file_access()
&~ Fl_Preferences::SYSTEM_WRITE );
\endcode
If preferences are created using a filename (instead of Fl_Preferences::USER or
Fl_Preferences::SYSTEM), file access is handled as if the Fl_Preferences::USER
flag was set.
\see Fl_Preferences::NONE and others for a list of flags.
\see Fl_Preferences::file_access()
*/
void Fl_Preferences::file_access(unsigned int flags)
{
fileAccess_ = flags;
}
/**
Return the current file access permissions for the FLTK preferences system.
\see Fl_Preferences::file_access(unsigned int)
*/
unsigned int Fl_Preferences::file_access()
{
return fileAccess_;
}
/**
Determine the file name and path to preferences that would be openend with
these parameters.
Find the possible location of a preference file on disk without touching any
of the pathname components. This can be used to check if a preference file
already exists.
\param[out] buffer write the resulting path into this buffer
\param[in] buffer_size size of the `buffer` in bytes
\param[in] root can be \c USER_L or \c SYSTEM_L for user specific or system
wide preferences
\param[in] vendor unique text describing the company or author of this file,
must be a valid filepath segment
\param[in] application unique text describing the application, must be a
valid filepath segment
\return the input root value, or Fl_Preferences::UNKNOWN_ROOT_TYPE if the path
could not be determined.
\see Fl_Preferences( Root root, const char *vendor, const char *application )
*/
Fl_Preferences::Root Fl_Preferences::filename( char *buffer, size_t buffer_size, Root root, const char *vendor, const char *application )
{
Root ret = UNKNOWN_ROOT_TYPE;
if (buffer && buffer_size>0) {
char *fn = Fl::system_driver()->preference_rootnode(NULL, root, vendor, application);
if (fn) {
fl_strlcpy(buffer, fn, buffer_size);
// FLTK always returns forward slashes in paths
{ char *s; for ( s = buffer; *s; s++ ) if ( *s == '\\' ) *s = '/'; }
ret = root;
} else {
buffer[0] = 0;
}
}
return ret;
}
/**
The constructor creates a group that manages key/value pairs and
child groups.
Preferences can be stored per user using the root type
`Fl_Preferences::USER_L`, or stored system-wide using
`Fl_Preferences::SYSTEM_L`.
Groups and key/value pairs can be read and written randomly. Reading undefined
values will return the default value. Writing undefined values will create
all required groups and key/vlaue pairs.
This constructor creates the <i>base</i> instance for all following entries
and reads the database from disk into memory if it exists.
The vendor argument is a unique text string identifying the development team
or vendor of an application. A domain name or an EMail address (replacing
the '@' with a '.') are great unique names, e.g. "research.matthiasm.com" or
"fluid.fltk.org".
The application argument can be the working title or final name of your
application.
Both vendor and application must be valid UNIX path segments as they become
parts of the preference file path and may contain forward slashes to create
deeper file structures.
\note On \b Windows, the directory is constructed by querying the
<i>Common AppData</i> or <i>AppData</i> key of the
<tt>Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders</tt>
registry entry.
The filename and path is then constructed as
<tt>\$(query)/\$(vendor)/\$(application).prefs</tt> .
If the query call fails, data will be stored in RAM only.
It will be lost when the app exits.
\par In FLTK versions before 1.4.0, if querying the registry failed,
preferences would be written to
<tt>C:\\FLTK\\\$(vendor)\\\$(application).prefs</tt> .
\note On \b Linux, the \c USER directory is constructed by reading \c $HOME .
If \c $HOME is not set or not pointing to an existing directory, FLTK will
check the path member of the passwd struct returned by \c getpwuid(getuid()) .
If all attempts fail, data will be stored in RAM only and be lost when the
app exits.
The \c SYSTEM preference filename is hardcoded as
<tt>/etc/fltk/\$(vendor)/\$(application).prefs</tt> .
For backward compatibility, the old \c USER `.prefs` file naming scheme
<tt>\$(directory)/.fltk/\$(vendor)/\$(application).prefs</tt> is checked first.
If that file does not exist, the environment variable `$XDG_CONFIG_HOME` is
read as a base directory. If `$XDG_CONFIG_HOME` not set, the base directory
defaults to `$HOME/.config/`.
The user preferences will be stored in
<tt>\$(directory)/\$(vendor)/\$(application).prefs</tt>. The user data path
will be
<tt>\$(directory)/\$(vendor)/\$(application)/</tt>.
In FLTK versions before 1.4.0, if \c $HOME was not set, the \c USER path
would be empty, generating <tt>\$(vendor)/\$(application).prefs</tt>, which
was used relative to the current working directory.
\note On \b macOS, the \c USER directory is constructed by reading \c $HOME .
If \c $HOME is not set or not pointing to an existing directory, we check the
path returned by \c NSHomeDirectory() , and finally checking the path member
of the passwd struct returned by \c getpwuid(getuid()) .
If all attempts fail, data will be stored in RAM only and be lost when the app exits.
The filename and path is then constructed as
<tt>\$(directory)/Library/Preferences/\$(vendor)/\$(application).prefs</tt> .
The \c SYSTEM directory is hardcoded as
<tt>/Library/Preferences/\$(vendor)/\$(application).prefs</tt> .
\par In FLTK versions before 1.4.0, if \c $HOME was not set, the \c USER path
would be \c NULL , generating
<tt>\<null\>/Library/Preferences/\$(vendor)/\$(application).prefs</tt>,
which would silently fail to create a preference file.
\param[in] root can be \c USER_L or \c SYSTEM_L for user specific or system
wide preferences, add the \c CLEAR flag to start with a clean set of
preferences instead of reading them from a preexisting database
\param[in] vendor unique text describing the company or author of this file, must be a valid filepath segment
\param[in] application unique text describing the application, must be a valid filepath segment
\see Fl_Preferences(Fl_Preferences *parent, const char *group) with parent set to NULL
*/
Fl_Preferences::Fl_Preferences( Root root, const char *vendor, const char *application ) {
node = new Node( "." );
rootNode = new RootNode( this, root, vendor, application );
node->setRoot(rootNode);
if (root & CLEAR)
clear();
}
/**
\brief Deprecated: Use this constructor to create or read a preference file at an
arbitrary position in the file system.
This constructor should no longer be used because the generated database uses
the current locale, making it impossible to exchange floating point settings
between machines with different language settings.
Use `Fl_Preferences(path, vendor, application, Fl_Preferences::C_LOCALE)` in
new projects and `Fl_Preferences(path, vendor, application, 0)` if you must
keep backward compatibility.
\see Fl_Preferences( const char *path, const char *vendor, const char *application, Root flags )
*/
Fl_Preferences::Fl_Preferences( const char *path, const char *vendor, const char *application ) {
node = new Node( "." );
rootNode = new RootNode( this, path, vendor, application, (Root)0 );
node->setRoot(rootNode);
}
/**
\brief Use this constructor to create or read a preference file at an
arbitrary position in the file system.
The file name is generated in the form <tt>\$(path)/\$(application).prefs</tt>.
If \p application is \c NULL, \p path is taken literally as the file
path and name.
```
// Example: read from an existing database and write modifications when flushed
// or destructor is called
Fl_Preferences database("/user/matt/test.prefs", "org.fltk.test", NULL,
Fl_Preferences::C_LOCALE);
// Example: create a new preferences file with an empty data set
Fl_Preferences database("/user/matt/test.prefs", "org.fltk.test", NULL,
(Fl_Preferences::Root)(Fl_Preferences::C_LOCALE|Fl_Preferences::CLEAR));
```
\note the C_LOCALE flag is is not set by default for backward compatibility,
but it is highly recommended to set it when opening a database.
\param[in] path path to the directory that contains the preference file
\param[in] vendor unique text describing the company or author of this file,
must be a valid file path segment
\param[in] application unique text describing the application, must be a valid
filename or NULL
\param[in] flags C_LOCALE to make the preferences file independent of the
current locale, add the CLEAR flag to start with a clean set of preferences
instead of reading from the database
*/
Fl_Preferences::Fl_Preferences( const char *path, const char *vendor, const char *application, Root flags ) {
node = new Node( "." );
rootNode = new RootNode( this, path, vendor, application, flags );
node->setRoot(rootNode);
if (flags & CLEAR)
clear();
}
/**
\brief Generate or read a new group of entries within another group.
Use the \p group argument to name the group that you would like to access.
\p Group can also contain a path to a group further down the hierarchy by
separating group names with a forward slash '/'.
\param[in] parent reference object for the new group
\param[in] group name of the group to access (may contain '/'s)
*/
Fl_Preferences::Fl_Preferences( Fl_Preferences &parent, const char *group ) {
rootNode = parent.rootNode;
node = parent.node->addChild( group );
}
/**
\brief Create or access a group of preferences using a name.
Parent should point to a previously created parent preferences group to
create a preferences hierarchy.
If `parent` is set to `NULL`, an unnamed database will be accessed that exists
only in local memory and is not associated with a file on disk. The root type
of this database is set to `Fl_Preferences::MEMORY`.
- the memory database is \em not shared among multiple instances of the same app
- memory databases are \em not thread safe
- all data will be lost when the app quits
```
void some_function() {
Fl_Preferences guide( NULL, "Guide" );
guide.set("answer", 42);
}
void other_function() {
int x;
Fl_Preferences guide( NULL, "Guide" );
guide.get("answer", x, -1);
}
```
FLTK uses the memory database to manage plugins. See `Fl_Plugin`.
\param[in] parent the parameter parent is a pointer to the parent group.
If \p parent is \p NULL, the new preferences item refers to an
application internal database ("runtime prefs") which exists only
once, and remains in RAM only until the application quits.
This database is used to manage plugins and other data indexes
by strings. Runtime prefs are \em not thread-safe.
\param[in] group a group name that is used as a key into the database
\see Fl_Preferences( Fl_Preferences&, const char *group )
*/
Fl_Preferences::Fl_Preferences( Fl_Preferences *parent, const char *group ) {
if (parent==NULL) {
if (!runtimePrefs) {
runtimePrefs = new Fl_Preferences();
runtimePrefs->node = new Node( "." );
runtimePrefs->rootNode = new RootNode( runtimePrefs );
runtimePrefs->node->setRoot(runtimePrefs->rootNode);
}
parent = runtimePrefs;
}
rootNode = parent->rootNode;
node = parent->node->addChild( group );
}
/**
\brief Open a child group using a given index.
Use the \p groupIndex argument to find the group that you would like to access.
If the given index is invalid (negative or too high), a new group is created
with a UUID as a name.
The index needs to be fixed. It is currently backward. Index 0 points
to the last member in the 'list' of preferences.
\param[in] parent reference object for the new group
\param[in] groupIndex zero based index into child groups
*/
Fl_Preferences::Fl_Preferences( Fl_Preferences &parent, int groupIndex ) {
rootNode = parent.rootNode;
if (groupIndex<0 || groupIndex>=parent.groups()) {
node = parent.node->addChild( newUUID() );
} else {
node = parent.node->childNode( groupIndex );
}
}
/**
\see Fl_Preferences( Fl_Preferences&, int groupIndex )
*/
Fl_Preferences::Fl_Preferences( Fl_Preferences *parent, int groupIndex ) {
rootNode = parent->rootNode;
if (groupIndex<0 || groupIndex>=parent->groups()) {
node = parent->node->addChild( newUUID() );
} else {
node = parent->node->childNode( groupIndex );
}
}
/**
Create a new dataset access point using a dataset ID.
ID's are a great way to remember shortcuts to database entries that are deeply
nested in a preferences database, as long as the database root is not deleted.
An ID can be retrieved from any Fl_Preferences dataset, and can then be used
to create multiple new references to the same dataset.
ID's can be very helpful when put into the <tt>user_data()</tt> field of
widget callbacks.
*/
Fl_Preferences::Fl_Preferences( Fl_Preferences::ID id ) {
node = (Node*)id;
rootNode = node->findRoot();
}
/**
Create another reference to a Preferences group.
*/
Fl_Preferences::Fl_Preferences(const Fl_Preferences &rhs)
: node(rhs.node),
rootNode(rhs.rootNode)
{ }
/**
Assign another reference to a preference group.
*/
Fl_Preferences &Fl_Preferences::operator=(const Fl_Preferences &rhs) {
if (&rhs != this) {
node = rhs.node;
rootNode = rhs.rootNode;
}
return *this;
}
/**
The destructor removes allocated resources. When used on the
\em base preferences group, the destructor flushes all changes
to the preference file and deletes all internal databases.
The destructor does not remove any data from the database. It merely
deletes your reference to the database.
*/
Fl_Preferences::~Fl_Preferences() {
if (node && !node->parent()) delete rootNode;
// DO NOT delete nodes! The root node will do that after writing the preferences.
// Zero all pointer to avoid memory errors, even though
// Valgrind does not complain (Cygwin does though)
node = 0L;
rootNode = 0L;
}
/**
Return the file name and path to the preference file.
If the preferences have not changed or have not been flushed, the file
or directory may not have been created yet.
\param[out] buffer write the resulting path into this buffer
\param[in] buffer_size size of the `buffer` in bytes
\return the root type at creation type, or MEMORY for runtime prefs, it does
not return CORE or LOCALE flags.
*/
Fl_Preferences::Root Fl_Preferences::filename( char *buffer, size_t buffer_size)
{
if (!buffer || buffer_size==0)
return UNKNOWN_ROOT_TYPE;
RootNode *rn = rootNode;
if (!rn)
return UNKNOWN_ROOT_TYPE;
if (rn->root()==MEMORY)
return MEMORY;
char *fn = rn->filename();
if (!fn)
return UNKNOWN_ROOT_TYPE;
fl_strlcpy(buffer, fn, buffer_size);
if (buffer[0]==0)
return UNKNOWN_ROOT_TYPE;
return (Root)(rn->root() & ROOT_MASK);
}
/**
Returns the number of groups that are contained within a group.
\return 0 for no groups at all
*/
int Fl_Preferences::groups() {
return node->nChildren();
}
/**
Returns the name of the Nth (\p num_group) group.
There is no guaranteed order of group names. The index must
be within the range given by groups().
\param[in] num_group number indexing the requested group
\return 'C' string pointer to the group name
*/
const char *Fl_Preferences::group( int num_group ) {
return node->child( num_group );
}
/**
Returns non-zero if a group with this name exists.
Group names are relative to the Fl_Preferences node and can contain a path.
"." describes the current node, "./" describes the topmost node.
By preceding a groupname with a "./" its path becomes relative to the topmost node.
\param[in] key name of group that is searched for
\return 0 if no group by that name was found
*/
char Fl_Preferences::group_exists( const char *key ) {
return node->search( key ) ? 1 : 0 ;
}
/**
Deletes a group.
Removes a group and all keys and groups within that group
from the database.
\param[in] group name of the group to delete
\return 0 if call failed
*/
char Fl_Preferences::delete_group( const char *group ) {
Node *nd = node->search( group );
if ( nd ) return nd->remove();
return 0;
}
/**
Delete all groups.
*/
char Fl_Preferences::delete_all_groups() {
node->deleteAllChildren();
return 1;
}
/**
Returns the number of entries (name/value pairs) in a group.
\return number of entries
*/
int Fl_Preferences::entries() {
return node->nEntry();
}
/**
Returns the name of an entry. There is no guaranteed order of entry
names. The index must be within the range given by entries().
\param[in] index number indexing the requested entry
\return pointer to value cstring
*/
const char *Fl_Preferences::entry( int index ) {
return node->entry(index).name;
}
/**
Returns non-zero if an entry with this name exists.
\param[in] key name of entry that is searched for
\return 0 if entry was not found
*/
char Fl_Preferences::entry_exists( const char *key ) {
return node->getEntry( key )>=0 ? 1 : 0 ;
}
/**
Deletes a single name/value pair.
This function removes the entry \p key from the database.
\param[in] key name of entry to delete
\return 0 if deleting the entry failed
*/
char Fl_Preferences::delete_entry( const char *key ) {
return node->deleteEntry( key );
}
/**
Delete all entries.
*/
char Fl_Preferences::delete_all_entries() {
node->deleteAllEntries();
return 1;
}
/**
Delete all groups and all entries.
*/
char Fl_Preferences::clear() {
char ret1 = deleteAllGroups();
char ret2 = deleteAllEntries();
return ret1 & ret2;
}
/**
Reads an entry from the group. A default value must be
supplied. The return value indicates if the value was available
(non-zero) or the default was used (0).
\param[in] key name of entry
\param[out] value returned from preferences or default value if none was set
\param[in] defaultValue default value to be used if no preference was set
\return 0 if the default value was used
*/
char Fl_Preferences::get( const char *key, int &value, int defaultValue ) {
const char *v = node->get( key );
value = v ? atoi( v ) : defaultValue;
return ( v != 0 );
}
/**
Sets an entry (name/value pair). The return value indicates if there
was a problem storing the data in memory. However it does not reflect
if the value was actually stored in the preference file.
\param[in] key name of entry
\param[in] value set this entry to \p value
\return 0 if setting the value failed
*/
char Fl_Preferences::set( const char *key, int value ) {
snprintf( nameBuffer, sizeof(nameBuffer), "%d", value );
node->set( key, nameBuffer );
return 1;
}
/**
Reads an entry from the group. A default value must be
supplied. The return value indicates if the value was available
(non-zero) or the default was used (0).
\param[in] key name of entry
\param[out] value returned from preferences or default value if none was set
\param[in] defaultValue default value to be used if no preference was set
\return 0 if the default value was used
*/
char Fl_Preferences::get( const char *key, float &value, float defaultValue ) {
const char *v = node->get( key );
if (v) {
if (rootNode->root() & C_LOCALE) {
clocale_sscanf(v, "%g", &value);
} else {
value = (float)atof(v);
}
} else {
value = defaultValue;
}
return ( v != NULL );
}
/**
Sets an entry (name/value pair). The return value indicates if there
was a problem storing the data in memory. However it does not reflect
if the value was actually stored in the preference file.
\param[in] key name of entry
\param[in] value set this entry to \p value
\return 0 if setting the value failed
*/
char Fl_Preferences::set( const char *key, float value ) {
if (rootNode->root() & C_LOCALE) {
clocale_snprintf( nameBuffer, sizeof(nameBuffer), "%g", value );
} else {
snprintf( nameBuffer, sizeof(nameBuffer), "%g", value );
}
node->set( key, nameBuffer );
return 1;
}
/**
Sets an entry (name/value pair). The return value indicates if there
was a problem storing the data in memory. However it does not reflect
if the value was actually stored in the preference file.
\param[in] key name of entry
\param[in] value set this entry to \p value
\param[in] precision number of decimal digits to represent value
\return 0 if setting the value failed
*/
char Fl_Preferences::set( const char *key, float value, int precision ) {
if (rootNode->root() & C_LOCALE) {
clocale_snprintf( nameBuffer, sizeof(nameBuffer), "%.*g", precision, value );
} else {
snprintf( nameBuffer, sizeof(nameBuffer), "%.*g", precision, value );
}
node->set( key, nameBuffer );
return 1;
}
/**
Reads an entry from the group. A default value must be
supplied. The return value indicates if the value was available
(non-zero) or the default was used (0).
\param[in] key name of entry
\param[out] value returned from preferences or default value if none was set
\param[in] defaultValue default value to be used if no preference was set
\return 0 if the default value was used
*/
char Fl_Preferences::get( const char *key, double &value, double defaultValue ) {
const char *v = node->get( key );
if (v) {
if (rootNode->root() & C_LOCALE) {
clocale_sscanf(v, "%lg", &value);
} else {
value = atof(v);
}
} else {
value = defaultValue;
}
return ( v != NULL );
}
/**
Sets an entry (name/value pair). The return value indicates if there
was a problem storing the data in memory. However it does not reflect
if the value was actually stored in the preference file.
\param[in] key name of entry
\param[in] value set this entry to \p value
\return 0 if setting the value failed
*/
char Fl_Preferences::set( const char *key, double value ) {
if (rootNode->root() & C_LOCALE) {
clocale_snprintf( nameBuffer, sizeof(nameBuffer), "%lg", value );
} else {
snprintf( nameBuffer, sizeof(nameBuffer), "%lg", value );
}
node->set( key, nameBuffer );
return 1;
}
/**
Sets an entry (name/value pair). The return value indicates if there
was a problem storing the data in memory. However it does not reflect
if the value was actually stored in the preference file.
\param[in] key name of entry
\param[in] value set this entry to \p value
\param[in] precision number of decimal digits to represent value
\return 0 if setting the value failed
*/
char Fl_Preferences::set( const char *key, double value, int precision ) {
if (rootNode->root() & C_LOCALE) {
clocale_snprintf( nameBuffer, sizeof(nameBuffer), "%.*lg", precision, value );
} else {
snprintf( nameBuffer, sizeof(nameBuffer), "%.*lg", precision, value );
}
node->set( key, nameBuffer );
return 1;
}
// remove control sequences from a string
static char *decodeText( const char *src ) {
int len = 0;
const char *s = src;
for ( ; *s; s++, len++ ) {
if ( *s == '\\' ) {
if ( isdigit( s[1] ) ) {
s+=3;
} else {
s+=1;
}
}
}
char *dst = (char*)malloc( len+1 ), *d = dst;
for ( s = src; *s; s++ ) {
char c = *s;
if ( c == '\\' ) {
if ( s[1] == '\\' ) { *d++ = c; s++; }
else if ( s[1] == 'n' ) { *d++ = '\n'; s++; }
else if ( s[1] == 'r' ) { *d++ = '\r'; s++; }
else if ( isdigit( s[1] ) ) { *d++ = ((s[1]-'0')<<6) + ((s[2]-'0')<<3) + (s[3]-'0'); s+=3; }
else s++; // error
}
else
*d++ = c;
}
*d = 0;
return dst;
}
/**
Reads an entry from the group. A default value must be
supplied. The return value indicates if the value was available
(non-zero) or the default was used (0).
'maxSize' is the maximum length of text that will be read.
The text buffer must allow for one additional byte for a trailing zero.
\param[in] key name of entry
\param[out] text returned from preferences or default value if none was set
\param[in] defaultValue default value to be used if no preference was set
\param[in] maxSize maximum length of value plus one byte for a trailing zero
\return 0 if the default value was used
*/
char Fl_Preferences::get( const char *key, char *text, const char *defaultValue, int maxSize ) {
const char *v = node->get( key );
if ( v && strchr( v, '\\' ) ) {
char *w = decodeText( v );
strlcpy(text, w, maxSize);
free( w );
return 1;
}
if ( !v ) v = defaultValue;
if ( v ) strlcpy(text, v, maxSize);
else *text = 0;
return ( v != defaultValue );
}
/**
Reads an entry from the group. A default value must be
supplied. The return value indicates if the value was available
(non-zero) or the default was used (0). get() allocates memory of
sufficient size to hold the value. The buffer must be free'd by
the developer using 'free(value)'.
\param[in] key name of entry
\param[out] text returned from preferences or default value if none was set
\param[in] defaultValue default value to be used if no preference was set
\return 0 if the default value was used
*/
char Fl_Preferences::get( const char *key, char *&text, const char *defaultValue ) {
const char *v = node->get( key );
if ( v && strchr( v, '\\' ) ) {
text = decodeText( v );
return 1;
}
if ( !v ) v = defaultValue;
if ( v )
text = fl_strdup( v );
else
text = 0;
return ( v != defaultValue );
}
#if (FLTK_USE_STD)
/**
Reads an entry from the group. A default value must be
supplied. The return value indicates if the value was available
(non-zero) or the default was used (0).
\param[in] key name of entry
\param[out] value returned from preferences or default value if none was set
\param[in] defaultValue default value to be used if no preference was set
\return 0 if the default value was used
*/
char Fl_Preferences::get( const char *key, std::string &value, const std::string &defaultValue ) {
const char *v = node->get( key );
if (v) {
if ( strchr( v, '\\' ) ) {
char *text = decodeText( v );
value = text;
::free(text);
} else {
value = v;
}
return 1;
} else {
value = defaultValue;
return 0;
}
}
#endif
/**
Sets an entry (name/value pair). The return value indicates if there
was a problem storing the data in memory. However it does not
reflect if the value was actually stored in the preference file.
\param[in] key name of entry
\param[in] text set this entry to \p value
\return 0 if setting the value failed
*/
char Fl_Preferences::set( const char *key, const char *text ) {
const char *s = text ? text : "";
int n=0, ns=0;
for ( ; *s; s++ ) { n++; if ( *s<32 || *s=='\\' || *s==0x7f ) ns+=4; }
if ( ns ) {
char *buffer = (char*)malloc( n+ns+1 ), *d = buffer;
for ( s=text; *s; ) {
char c = *s;
if ( c=='\\' ) { *d++ = '\\'; *d++ = '\\'; s++; }
else if ( c=='\n' ) { *d++ = '\\'; *d++ = 'n'; s++; }
else if ( c=='\r' ) { *d++ = '\\'; *d++ = 'r'; s++; }
else if ( c<32 || c==0x7f )
{ *d++ = '\\'; *d++ = '0'+((c>>6)&3); *d++ = '0'+((c>>3)&7); *d++ = '0'+(c&7); s++; }
else *d++ = *s++;
}
*d = 0;
node->set( key, buffer );
free( buffer );
}
else
node->set( key, text );
return 1;
}
// convert a hex string to binary data
static void *decodeHex( const char *src, int &size ) {
size = (int) strlen( src )/2;
unsigned char *data = (unsigned char*)malloc( size ), *d = data;
const char *s = src;
for ( int i=size; i>0; i-- ) {
int v;
char x = tolower(*s++);
if ( x >= 'a' ) v = x-'a'+10; else v = x-'0';
v = v<<4;
x = tolower(*s++);
if ( x >= 'a' ) v += x-'a'+10; else v += x-'0';
*d++ = (uchar)v;
}
return (void*)data;
}
/**
Reads a binary entry from the group, encoded in hexadecimal blocks.
\param[in] key name of entry
\param[out] data value returned from preferences or default value if none was set
\param[in] defaultValue default value
\param[in] defaultSize size of default value array
\param[in] maxSize maximum length of value, to receive the number of bytes
read, use the function below instead.
\return 0 if the default value was used
\see Fl_Preferences::get( const char *key, void *data, const void *defaultValue, int defaultSize, int *maxSize )
*/
char Fl_Preferences::get( const char *key, void *data, const void *defaultValue, int defaultSize, int maxSize ) {
const char *v = node->get( key );
if ( v ) {
int dsize;
void *w = decodeHex( v, dsize );
memmove( data, w, dsize>maxSize?maxSize:dsize );
free( w );
return 1;
}
if ( defaultValue )
memmove( data, defaultValue, defaultSize>maxSize?maxSize:defaultSize );
return 0;
}
/**
Reads a binary entry from the group, encoded in hexadecimal blocks.
A binary (not hex) default value can be supplied.
The return value indicates if the value was available (non-zero) or the
default was used (0).
`maxSize` is the maximum length of text that will be read and returns the
actual number of bytes read.
\param[in] key name of entry
\param[out] data value returned from preferences or default value if none was set
\param[in] defaultValue default value to be used if no preference was set
\param[in] defaultSize size of default value array
\param[inout] maxSize maximum length of value and actual number of bytes set
\return 0 if the default value was used
*/
char Fl_Preferences::get( const char *key, void *data, const void *defaultValue, int defaultSize, int *maxSize ) {
if (!maxSize || !data)
return -1;
int capacity = *maxSize;
const char *v = node->get( key );
if ( v ) {
int nFound;
void *w = decodeHex( v, nFound );
int nWrite = (nFound>capacity) ? capacity : nFound;
memmove( data, w, nWrite);
free( w );
*maxSize = nWrite;
return 1;
}
if ( defaultValue ) {
int nWrite = (defaultSize>capacity) ? capacity : defaultSize;
memmove( data, defaultValue, nWrite );
*maxSize = nWrite;
} else {
*maxSize = 0;
}
return 0;
}
/**
Reads an entry from the group. A default value must be
supplied. The return value indicates if the value was available
(non-zero) or the default was used (0). get() allocates memory of