/src/wrappers/glib/library/utilities/g_key_file.e
Specman e | 838 lines | 448 code | 100 blank | 290 comment | 8 complexity | 002a7d71c370c0c88dfc4e273515314c MD5 | raw file
1indexing 2 description: "Key-value file parser -- parses .ini-like config files." 3 copyright: "[ 4 Copyright (C) 2007 Paolo Redaelli, Glib developers 5 6 This library is free software; you can redistribute it and/or 7 modify it under the terms of the GNU Lesser General Public License 8 as published by the Free Software Foundation; either version 2.1 of 9 the License, or (at your option) any later version. 10 11 This library is distributed in the hopeOA that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 You should have received a copy of the GNU Lesser General Public 17 License along with this library; if not, write to the Free Software 18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19 02110-1301 USA 20 ]" 21 22class G_KEY_FILE 23 -- G_KEY_FILE lets you parse, edit or create files containing 24 -- groups of key-value pairs, which we call key files for lack of a 25 -- better name. Several freedesktop.org specifications use key 26 -- files now, e.g the Desktop Entry Specification and the Icon 27 -- Theme Specification. 28 29 -- The syntax of key files is described in detail in the Desktop 30 -- Entry Specification, here is a quick summary: Key files consists 31 -- of groups of key-value pairs, interspersed with comments. 32 33 -- # this is just an example 34 -- # there can be comments before the first group 35 36 -- [First Group] 37 38 -- Name=Key File Example\tthis value shows\nescaping 39 40 -- # localized strings are stored in multiple key-value pairs 41 -- Welcome=Hello 42 -- Welcome[de]=Hallo 43 -- Welcome[fr]=Bonjour 44 -- Welcome[it]=Ciao 45 46 -- [Another Group] 47 48 -- Numbers=2;20;-200;0 49 50 -- Booleans=true;false;true;true 51 52 -- Lines beginning with a '#' and blank lines are considered comments. 53 54 -- Groups are started by a header line containing the group name 55 -- enclosed in '[' and ']', and ended implicitly by the start of 56 -- the next group or the end of the file. Each key-value pair must 57 -- be contained in a group. 58 59 -- Key-value pairs generally have the form key=value, with the 60 -- exception of localized strings, which have the form 61 -- key[locale]=value. Space before and after the '=' character are 62 -- ignored. Newline, tab, carriage return and backslash characters 63 -- are escaped as \n, \t, \r, and \\, respectively. To preserve 64 -- initial and final spaces, these can also be escaped as \s. 65 66 -- Key files can store strings (possibly with localized variants), 67 -- integers, booleans and lists of these. Lists are separated by a 68 -- separator character, typically ';' or ','. To use the list 69 -- separator character in a value in a list, it has to be escaped 70 -- by prefixing it with a backslash. 71 72 -- This syntax is obviously inspired by the .ini files commonly met 73 -- on Windows, but there are some important differences: 74 75 -- o .ini files use the ';' character to begin comments, key files 76 -- use the '#' character. 77 78 -- o Key files allow only comments before the first group. 79 80 -- o Key files are always encoded in UTF-8. 81 82 -- o Key and Group names are case-sensitive, for example a group 83 -- called [GROUP] is a different group from [group]. 84 85inherit 86 C_STRUCT redefine free end 87 EIFFEL_OWNED redefine free end 88 89insert 90 SHARED_G_ERROR 91 GKEYFILE_EXTERNALS 92 GKEY_FILE_STRUCT 93 94creation make, load_from_file, from_external_pointer 95 96feature {} -- Creation 97 make is 98 -- Creates a new empty G_KEY_FILE object. Use 99 -- `load_from_file', `load_from_data' or 100 -- `load_from_data_dirs' to read an existing key file. 101 do 102 from_external_pointer(g_key_file_new) 103 end 104 105 106 load_from_file (a_file: STRING; some_flags: GKEY_FILE_FLAGS_ENUM) is 107 -- Loads a key file into an empty GKeyFile structure. If 108 -- `a_file' has been completely and correctly read 109 -- `is_successful' is set to True, otherwise, i.e. if 110 -- `a_file' could not be loaded, `error' is set to either a 111 -- GFileError or GKeyFileError. 112 113 -- `a_file' : the path of a filename to load, in the GLib file name encoding 114 require 115 file_not_void: a_file /= Void 116 do 117 make 118 is_successful:=(g_key_file_load_from_file 119 (handle, a_file.to_external, some_flags.value, 120 error.reference)).to_boolean 121 ensure meaningful_error: -- TODO 122 end 123 124 -- TODO: load_from_data 125 126 -- gboolean g_key_file_load_from_data (GKeyFile *key_file, const 127 -- gchar *data, gsize length, GKeyFileFlags flags, GError **error); 128 129 -- Loads a key file from memory into an empty GKeyFile 130 -- structure. If the object cannot be created then error is set to 131 -- a GKeyFileError. 132 133 -- key_file : an empty GKeyFile struct 134 -- data : key file loaded in memory. 135 -- length : the length of data in bytes 136 -- flags : flags from GKeyFileFlags 137 -- error : return location for a GError, or NULL 138 -- Returns : TRUE if a key file could be loaded, FALSE othewise 139 140 load_from_data_dirs (a_file: STRING; some_flags: GKEY_FILE_FLAGS_ENUM) is 141 -- Looks for `a_key_file' in the paths returned from 142 -- `g_get_user_data_dir' and `g_get_system_data_dirs' (TODO: 143 -- these two are the names of the C functions; provide the 144 -- Eiffel equivalents), loads the file into key_file and 145 -- returns the file's full path in full_path. If the file 146 -- could not be loaded then an error is set to either a 147 -- GFileError or GKeyFileError. 148 149 -- `a_keyfile' is a relative path to a filename to open and 150 -- parse. 151 152 -- `full_path' will be set to the full path of the file. 153 154 -- `is_successful' and `error' will be updated. 155 require file_not_void: a_file /= Void 156 local full_path_ptr: POINTER 157 do 158 make 159 -- key_file must be an empty GKeyFile struct 160 is_successful:=(g_key_file_load_from_data_dirs 161 (handle, a_file.to_external, 162 $full_path_ptr, -- gchar **full_path, 163 some_flags.value, 164 error.reference)).to_boolean 165 create full_path.from_external(full_path_ptr) 166 end 167 168feature 169 full_path: STRING 170 -- The full path of the key file. Set by `load_from_data_dirs' 171 172 free (a_pointer: POINTER) is 173 -- Frees a GKeyFile. 174 do 175 g_key_file_free(a_pointer) 176 end 177 178 set_list_separator (a_separator: CHARACTER) is 179 -- Sets the character which is used to separate values in 180 -- lists. Typically ';' or ',' are used as separators. The 181 -- default list separator is ';'. 182 do 183 g_key_file_set_list_separator (handle, a_separator) 184 end 185 186 to_string: STRING is 187 -- The key file as a string. 188 do 189 create Result.from_external 190 (g_key_file_to_data (handle, 191 default_pointer, -- i.e.: gsize *length, 192 error.reference)) 193 end 194 195 196 start_group: STRING is 197 -- the name of the start group of the file. A new string is 198 -- created every time. 199 do 200 create Result.from_external(g_key_file_get_start_group(handle)) 201 ensure not_void: Result/=Void 202 end 203 204 groups: COLLECTION[STRING] is 205 -- all the groups in the key file. Currently it is a NULL_TERMINATED_STRING_ARRAY 206 local groups_ptr: POINTER; a_length: INTEGER 207 do 208 groups_ptr:=(g_key_file_get_groups 209 (handle,$a_length)) 210 create {NULL_TERMINATED_STRING_ARRAY} 211 Result.from_external(groups_ptr) 212 213 -- g_key_file_get_groups returns all groups in the key file loaded with 214 -- key_file. The array of returned groups will be NULL-terminated, so 215 -- length may optionally be NULL. 216 217 -- length : return location for the number of returned 218 -- groups, or NULL 219 220 -- Note: C documentation says to use g_strfreev to free groups. 221 -- g_strfreev frees the array *and* the contained strings; it isn't 222 -- necessary since memory handling is done throught Eiffel's GC 223 end 224 225 keys_of (a_group: STRING): COLLECTION[STRING] is 226 -- the all keys for `a_group'. If `a_group' cannot be found, Void is 227 -- returned and `error' is set to 228 -- `g_key_file_error_group_not_found'. Currently it is a 229 -- NULL_TERMINATED_STRING_ARRAY 230 require 231 group_not_void: a_group/=Void 232 group_exists: has_group(a_group) 233 local keys_ptr: POINTER 234 do 235 -- the all keys for `a_group'. The array of returned keys will be 236 -- NULL-terminated, so length may optionally be NULL. In the event that 237 -- the group_name cannot be found, NULL is returned and error is set to 238 -- G_KEY_FILE_ERROR_GROUP_NOT_FOUND. 239 keys_ptr:=(g_key_file_get_keys 240 (handle, a_group.to_external, 241 default_pointer, -- gsize *length, 242 error.reference)) 243 create {NULL_TERMINATED_STRING_ARRAY} 244 Result.from_external(keys_ptr) 245 end 246 247feature -- Queries 248 -- Note: there are not preconditions like "require 249 -- is_integer(a_group,a_key)" in a `integer_value' query because 250 -- the underlying GKeyFile API does not allow to implement it 251 -- efficiently: there is no way to know the type of a key except 252 -- trying to get it and then check if there were no error; i.e. 253 254 -- width := key_file.integer(my_group,width_label) 255 -- if key_file.is_valid then .... 256 257 -- Note: preconditions 258 has_group (a_group: STRING): BOOLEAN is 259 -- Does the key file have the group named `a_group'? 260 require group_not_void: a_group/=Void 261 do 262 Result:=(g_key_file_has_group(handle,a_group.to_external)).to_boolean 263 end 264 265 has_key (a_group, a_key: STRING): BOOLEAN is 266 -- Does the key file have `a_key' in `a_group'? If `a_group' 267 -- is Void `start_group' is used. 268 require 269 key_not_void: a_key/=Void 270 do 271 Result:=(g_key_file_has_key 272 (handle, a_group.to_external, 273 a_key.to_external, error.reference)).to_boolean 274 end 275 276 is_valid: BOOLEAN is 277 -- Did the last query returned a meaningful value? 278 279 -- Note: further feature calls can change this value, 280 -- becuase it is computed from the value of the common, 281 -- shared error object. 282 local f: GKEY_FILE_ERROR_ENUM 283 do 284 Result := error.code /= f.invalid_value_low_level 285 end 286 287 value (a_group, a_key: STRING): STRING is 288 -- the value associated with `a_key' under `a_group'. 289 require 290 group_not_void: a_group/=Void 291 key_not_void: a_key/=Void 292 has_key: has_key(a_group, a_key) 293 local value_ptr: POINTER 294 do 295 value_ptr:=(g_key_file_get_value 296 (handle, a_group.to_external, 297 a_key.to_external, error.reference)) 298 check value_ptr.is_not_null end 299 create Result.from_external(value_ptr) 300 -- g_key_file_get_value returns a newly allocated string or NULL if the 301 -- specified key cannot be found. 302 303 -- In the event the key cannot be found, NULL is returned and error is 304 -- set to G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the event that the 305 -- group_name cannot be found, NULL is returned and error is set to 306 -- G_KEY_FILE_ERROR_GROUP_NOT_FOUND. 307 end 308 309 string (a_group, a_key: STRING): STRING is 310 -- the string associated with `a_key' under `a_group'. 311 require 312 group_not_void: a_group/=Void 313 key_not_void: a_key/=Void 314 has_key: has_key(a_group, a_key) 315 local value_ptr: POINTER 316 do 317 -- If `a_key' cannot be found, Result is Void and 318 -- error is set to `g_key_file_error_key_not_found'. 319 320 -- If `a_group' cannot be found, Result is Void and error is 321 -- set to `g_key_file_error_group_not_found'. 322 value_ptr:=(g_key_file_get_string 323 (handle, a_group.to_external, 324 a_key.to_external, error.reference)) 325 -- g_key_file_get_string returns a newly allocated string or 326 -- NULL if the specified key cannot be found. 327 check value_ptr.is_not_null end 328 create Result.from_external(value_ptr) 329 end 330 331 localized_string (a_group, a_key, a_locale: STRING): STRING is 332 -- the string value associated with `a_key' under `a_group', 333 -- translated in the given locale, if available. If no 334 -- suitable translation can be found then the untranslated 335 -- value is returned. 336 require 337 group_not_void: a_group/=Void 338 key_not_void: a_key/=Void 339 has_key: has_key(a_group, a_key) 340 local value_ptr: POINTER 341 do 342 value_ptr:=(g_key_file_get_locale_string 343 (handle, a_group.to_external, 344 a_key.to_external, null_or_string(a_locale), 345 error.reference)) 346 -- Returns the value associated with key under group_name 347 -- translated in the given locale if available. If locale is 348 -- NULL then the current locale is assumed. 349 350 -- If key cannot be found then NULL is returned and error is 351 -- set to G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the value 352 -- associated with key cannot be interpreted or no suitable 353 -- translation can be found then the untranslated value is 354 -- returned. 355 check value_ptr.is_not_null end 356 create Result.from_external(value_ptr) 357 end 358 359 boolean (a_group, a_key: STRING): BOOLEAN is 360 -- the boolean value associated with `a_key' under 361 -- `a_group'. If the value associated with key cannot be 362 -- interpreted as a boolean then `is_valid' will be False. 363 require 364 key_not_void: a_key/=Void 365 has_key: has_key(a_group, a_key) 366 do 367 Result:=(g_key_file_get_boolean 368 (handle, a_group.to_external, a_key.to_external, 369 error.reference)).to_boolean 370 -- g_key_file_get_boolean returns the value associated with 371 -- key under group_name as a boolean. If key cannot be found 372 -- then the return value is undefined and error is set to 373 -- G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value 374 -- associated with key cannot be interpreted as a boolean 375 -- then the return value is also undefined and error is set 376 -- to G_KEY_FILE_ERROR_INVALID_VALUE. 377 end 378 379 integer (a_group, a_key: STRING): INTEGER is 380 -- the integer value associated with `a_key' under 381 -- `a_group'. If `a_group' is Void, the start group is used. 382 383 -- If the value associated with `a_key' cannot be interpreted 384 -- as an integer then `is_valid' will be False. 385 require 386 key_not_void: a_key/=Void 387 has_key: has_key(a_group, a_key) 388 do 389 Result:=(g_key_file_get_integer 390 (handle, null_or_string(a_group), a_key.to_external, 391 error.reference)) 392 -- Returns the value associated with key under group_name as 393 -- an integer. 394 395 -- If key cannot be found then the return value is undefined 396 -- and error is set to 397 -- G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value 398 -- associated with key cannot be interpreted as an integer 399 -- then the return value is also undefined and error is set 400 -- to G_KEY_FILE_ERROR_INVALID_VALUE. 401 end 402 403 real (a_group, a_key: STRING): REAL is 404 -- the real value associated with `a_key' under `a_group'. If 405 -- `a_group' is Void, the start group is used. 406 407 -- If the value associated with `a_key' cannot be interpreted 408 -- as a real then `is_valid' will be False. 409 require 410 key_not_void: a_key/=Void 411 has_key: has_key(a_group, a_key) 412 do 413 Result:=(g_key_file_get_double(handle, a_group.to_external, a_key.to_external, error.reference)) 414 end 415 416 strings, string_list (a_group, a_key: STRING): COLLECTION[STRING] is 417 -- the values associated with `a_key' under `a_group'. 418 require 419 group_not_void: a_group/=Void 420 key_not_void: a_key/=Void 421 has_key: has_key(a_group, a_key) 422 local value_ptr: POINTER; 423 do 424 not_yet_implemented 425 value_ptr:=(g_key_file_get_string_list 426 (handle, a_group.to_external, a_key.to_external, 427 default_pointer, -- gsize *length, 428 error.reference)) 429 -- if value_ptr.is_not_null then 430 -- create {NULL_TERMINATED_STRING_ARRAY} 431 -- Result.from_external(value_ptr) 432 -- end 433 434 -- g_key_file_get_string_list returns the values associated with key 435 -- under group_name. 436 437 -- In the event the key cannot be found, NULL is returned and error is 438 -- set to G_KEY_FILE_ERROR_KEY_NOT_FOUND. In the event that the 439 -- group_name cannot be found, NULL is returned and error is set to 440 -- G_KEY_FILE_ERROR_GROUP_NOT_FOUND. 441 442 -- key_file : a GKeyFile 443 -- group_name : a group name 444 -- key : a key 445 446 -- length return location for the number of returned strings, or NULL 447 -- error return location for a GError, or NULL 448 -- returns a NULL-terminated string array or NULL if the specified key 449 -- cannot be found. The array should be freed with g_strfreev(). 450 end 451 452 localized_string_list (a_group, a_key, a_locale: STRING): COLLECTION[STRING] is 453 -- the values associated with `a_key' under `a_group' 454 -- translated in `a_locale' if available. If `a_locale' is 455 -- Void then the current locale is assumed. 456 require 457 group_not_void: a_group/=Void 458 key_not_void: a_key/=Void 459 has_key: has_key(a_group, a_key) 460 local value_ptr: POINTER 461 do 462 value_ptr:=(g_key_file_get_locale_string_list 463 (handle, a_group.to_external, a_key.to_external, 464 null_or_string(a_locale), 465 default_pointer, -- gsize *length, a return location for the number of returned strings or NULL 466 error.reference)) 467 -- g_key_file_get_locale_string_list returns the values associated with 468 -- key under group_name translated in the given locale if available. If 469 -- locale is NULL then the current locale is assumed. 470 471 -- If key cannot be found then NULL is returned and error is set to 472 -- G_KEY_FILE_ERROR_KEY_NOT_FOUND. If the values associated with key 473 -- cannot be interpreted or no suitable translations can be found then 474 -- the untranslated values are returned. The returned array is 475 -- NULL-terminated, so length may optionally be NULL. 476 477 -- g_key_file_get_locale_string_list returns a newly allocated 478 -- NULL-terminated string array or NULL if the key isn't found. The 479 -- string array should be freed with g_strfreev(). 480 481 not_yet_implemented 482 -- if value_ptr.is_not_null then 483 -- create {NULL_TERMINATED_STRING_ARRAY} 484 -- Result.from_external(value_ptr) 485 -- end 486 end 487 488 boolean_list (a_group, a_key: STRING): COLLECTION[BOOLEAN] is 489 -- the values associated with `a_key' under `a_group'. If `a_group' is 490 -- Void, the start_group is used. 491 require 492 group_not_void: a_group/=Void 493 key_not_void: a_key/=Void 494 has_key: has_key(a_group, a_key) 495 local value_ptr: POINTER; a_storage: NATIVE_ARRAY[INTEGER]; a_length, i: INTEGER 496 do 497 value_ptr:=(g_key_file_get_boolean_list 498 (handle, a_group.to_external, 499 a_key.to_external, 500 $a_length, error.reference)) 501 -- If key cannot be found then the return value is undefined and error 502 -- is set to G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values 503 -- associated with key cannot be interpreted as booleans then the 504 -- return value is also undefined and error is set to 505 -- G_KEY_FILE_ERROR_INVALID_VALUE. 506 if value_ptr.is_not_null then 507 -- g_key_file_get_boolean_list returns the values associated with 508 -- key under group_name as booleans. 509 a_storage:=a_storage.from_pointer(value_ptr) 510 create {FAST_ARRAY[BOOLEAN]} Result.make(a_length) 511 from i:=a_length-1 until i=0 loop 512 Result.force(a_storage.item(i).to_boolean,i) 513 i:=i-1 514 end 515 end 516 end 517 518 integer_list (a_group, a_key: STRING): COLLECTION[INTEGER_32] is 519 -- the value associated with `a_key' under `a_group'. 520 require 521 group_not_void: a_group/=Void 522 key_not_void: a_key/=Void 523 has_key: has_key(a_group, a_key) 524 local 525 value_ptr: POINTER; a_storage: NATIVE_ARRAY[INTEGER_32]; 526 a_length, i: INTEGER_32 527 do 528 value_ptr:=(g_key_file_get_integer_list 529 (handle, a_group.to_external, 530 a_key.to_external, $a_length, error.reference)) 531 -- g_key_file_get_integer_list returns the values associated with key 532 -- under group_name as integers. 533 534 -- If key cannot be found then the return value is undefined and error 535 -- is set to G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the values 536 -- associated with key cannot be interpreted as integers then the 537 -- return value is also undefined and error is set to 538 -- G_KEY_FILE_ERROR_INVALID_VALUE. 539 if value_ptr.is_not_null then 540 create {FAST_ARRAY[INTEGER_32]} 541 Result.make(a_length) 542 from i:=a_length-1 until i=0 loop 543 Result.force(a_storage.item(i),i) 544 i:=i-1 545 end 546 end 547 end 548 549 real_list (a_group, a_key: STRING): COLLECTION[REAL] is 550 -- the real values associated with `a_key' under `a_group'. `is_valid' 551 -- will be False if the values associated with key cannot be 552 -- interpreted as real. 553 require 554 group_not_void: a_group/=Void 555 key_not_void: a_key/=Void 556 has_key: has_key(a_group, a_key) 557 local 558 value_ptr: POINTER; a_storage: NATIVE_ARRAY[REAL]; 559 a_length, i: INTEGER 560 do 561 value_ptr:=(g_key_file_get_double_list 562 (handle, a_group.to_external, 563 a_key.to_external, $a_length, 564 error.reference)) 565 -- g_key_file_get_double_list returns the values associated with key 566 -- under group_name as doubles. If group_name is NULL, the start group 567 -- is used. 568 if value_ptr.is_not_null then 569 a_storage:=a_storage.from_pointer(value_ptr) 570 create {FAST_ARRAY[REAL]} 571 Result.make(a_length) 572 from i:=a_length-1 until i=0 loop 573 Result.force(a_storage.item(i),i) 574 i:=i-1 575 end 576 end 577 end 578 579 comment (a_group, a_key: STRING): STRING is 580 -- The comment above `a_key' from `a_group'. If `a_key' is Void then 581 -- comment will be read from above `a_group'. If both `a_key' and 582 -- `a_group' are Void, then comment will be read from above the first 583 -- group in the file. 584 require 585 group_not_void: a_group/=Void 586 key_not_void: a_key/=Void 587 has_key: has_key(a_group, a_key) 588 local ptr: POINTER 589 do 590 ptr:=(g_key_file_get_comment 591 (handle, null_or_string(a_group), 592 null_or_string(a_key), error.reference)) 593 if ptr.is_not_null then create Result.from_external(ptr) end 594 end 595 596feature -- Setting commands 597 598 -- Note: g_key_file_set_value seems to be a perfect duplicate of 599 -- g_key_file_set_string. Therefore I haven't wrapped it into an 600 -- eventual `set_value'. 601 602 set_string (a_group, a_key, a_string: STRING) is 603 -- Associates `a_string' with `a_key' under `a_group'. If `a_key' cannot 604 -- be found then it is created. If `a_group' cannot be found then it is 605 -- created. 606 require 607 group_not_void: a_group/=Void 608 key_not_void: a_key/=Void 609 string_not_void: a_string/=Void 610 do 611 g_key_file_set_string(handle, a_group.to_external, 612 a_key.to_external, a_string.to_external) 613 ensure set: string(a_group, a_key).is_equal(a_string) 614 end 615 616 set_localized_string (a_group, a_key, a_string, a_locale: STRING) is 617 -- Associates `a_string' with `a_key' under `a_group' for 618 -- `a_locale'. If translation for `a_key' cannot be found then it is 619 -- created. 620 require 621 group_not_void: a_group/=Void 622 key_not_void: a_key/=Void 623 string_not_void: a_string/=Void 624 locale_not_void: a_locale/=Void 625 do 626 g_key_file_set_locale_string(handle, a_group.to_external, 627 a_key.to_external, a_locale.to_external, 628 a_string.to_external) 629 ensure set: localized_string(a_group, a_key, a_locale).is_equal(a_string) 630 end 631 632 set_boolean (a_group, a_key: STRING; a_value: BOOLEAN) is 633 -- Associates the boolean `a_value' with `a_key' under `a_group'. If 634 -- `a_key' cannot be found then it is created. 635 require 636 group_not_void: a_group/=Void 637 key_not_void: a_key/=Void 638 do 639 g_key_file_set_boolean(handle,a_group.to_external, 640 a_key.to_external, a_value.to_integer) 641 ensure set: boolean(a_group,a_key)=a_value 642 end 643 644 set_integer (a_group, a_key: STRING; a_value: INTEGER_32) is 645 -- Associates the integer `a_value' with `a_key' under `a_group'. If 646 -- `a_key' cannot be found then it is created. 647 require 648 group_not_void: a_group/=Void 649 key_not_void: a_key/=Void 650 do 651 g_key_file_set_integer (handle, a_group.to_external, 652 a_key.to_external, a_value) 653 end 654 655 set_real (a_group, a_key: STRING; a_value: REAL) is 656 -- Associates the real `a_value' with `a_key' under `a_group'. If 657 -- `a_key' cannot be found then it is created. 658 -- If `a_group' is Void, the start group is used. 659 require key_not_void: a_key/=Void 660 do 661 g_key_file_set_double (handle, null_or_string(a_group), 662 a_key.to_external, a_value) 663 end 664 665 -- Note: the original C API names were referring to lists. Eiffel 666 -- wrapper allow to use generic collections, hence the missing 667 -- "list". 668 669 set_strings (a_group, a_key: STRING; some_strings: COLLECTION[STRING]) is 670 -- Associates `some_strings' to `a_key' under `a_group'. If `a_key' 671 -- cannot be found then it is created. If `a_group' cannot be found 672 -- then it is created. 673 require 674 group_not_void: a_group/=Void 675 key_not_void: a_key/=Void 676 strings_not_void: some_strings/=Void 677 local native: NATIVE_ARRAY[POINTER]; i: INTEGER; si: ITERATOR[STRING] 678 do 679 native:=native.calloc(some_strings.count) 680 from 681 i:=0; si:=some_strings.new_iterator; si.start 682 until si.is_off loop 683 native.put(null_or_string(si.item),i) 684 i:=i+1 685 si.next 686 end 687 g_key_file_set_string_list 688 (handle, a_group.to_external, a_key.to_external, 689 native.to_external, some_strings.count.to_natural_32) 690 end 691 692 set_localized_strings (a_group, a_key, a_locale: STRING; some_strings: COLLECTION[STRING]) is 693 -- Associates `some_strings' to `a_key' under `a_group' for 694 -- `a_locale' language. If `a_key' cannot be found then it is 695 -- created. If `a_group' cannot be found then it is created. 696 require 697 group_not_void: a_group/=Void 698 key_not_void: a_key/=Void 699 strings_not_void: some_strings/=Void 700 locale_not_void: a_locale/=Void 701 local native: NATIVE_ARRAY[POINTER]; i: INTEGER; si: ITERATOR[STRING] 702 do 703 native:=native.calloc(some_strings.count) 704 from 705 i:=0; si:=some_strings.new_iterator; si.start 706 until si.is_off loop 707 native.put(null_or_string(si.item),i) 708 i:=i+1 709 si.next 710 end 711 g_key_file_set_locale_string_list 712 (handle, a_group.to_external, a_key.to_external, 713 a_locale.to_external, native.to_external, some_strings.count.to_natural_32 714 ) 715 end 716 717 set_booleans (a_group, a_key: STRING; some_booleans: COLLECTION[BOOLEAN]) is 718 -- Associates `some_booleans' to `a_key' under `a_group' If 719 -- `a_key' cannot be found then it is created. If `a_group' 720 -- cannot be found then it is created. If `a_group' is Void, 721 -- `start_group' is used. 722 require 723 key_not_void: a_key/=Void 724 valid_booleans: (some_booleans/=Void and then 725 not some_booleans.is_empty) 726 local native: NATIVE_ARRAY[INTEGER]; i: INTEGER; si: ITERATOR[BOOLEAN] 727 do 728 native:=native.calloc(some_booleans.count) 729 from 730 i:=0; si:=some_booleans.new_iterator; si.start 731 until si.is_off loop 732 native.put(si.item.to_integer,i) 733 i:=i+1 734 si.next 735 end 736 g_key_file_set_boolean_list (handle, null_or_string(a_group), 737 a_key.to_external, 738 native.to_external, some_booleans.count.to_natural_32) 739 end 740 741 set_integers (a_group, a_key: STRING; some_integers: COLLECTION[INTEGER]) is 742 -- Associates `some_integers' to `a_key' under `a_group' If 743 -- `a_key' cannot be found then it is created. If `a_group' 744 -- cannot be found then it is created. If `a_group' is Void, 745 -- `start_group' is used. 746 747 -- Performance hint: if `some_integers' is an heir of 748 -- ARRAYED_COLLECTION no temporary copies of given data are made. 749 -- Otherwise there is an O(some_integers.count) overhead both in 750 -- memory and time. 751 require 752 key_not_void: a_key/=Void 753 valid_integers: (some_integers/=Void and then 754 not some_integers.is_empty) 755 local array: ARRAYED_COLLECTION[INTEGER_32] 756 do 757 array ?= some_integers 758 if array=Void then 759 create {FAST_ARRAY[INTEGER_32]} array.from_collection (some_integers) 760 end 761 g_key_file_set_integer_list 762 (handle, null_or_string(a_group), a_key.to_external, 763 array.to_external, array.count.to_natural_32) 764 end 765 766 set_reals (a_group, a_key: STRING; some_reals: COLLECTION[REAL]) is 767 -- Associates `some_reals' to `a_key' under `a_group' If 768 -- `a_key' cannot be found then it is created. If `a_group' 769 -- cannot be found then it is created. If `a_group' is Void, 770 -- `start_group' is used. 771 772 -- Performance hint: if `some_reals' is an heir of 773 -- ARRAYED_COLLECTION no temporary copies of given data are made. 774 -- Otherwise there is an O(some_integers.count) overhead both in 775 -- memory and time. 776 777 require 778 key_not_void: a_key/=Void 779 valid_reals: (some_reals/=Void and then 780 not some_reals.is_empty) 781 local array: ARRAYED_COLLECTION[REAL] 782 do 783 array?=some_reals 784 if array=Void then 785 create {FAST_ARRAY[REAL]} array.from_collection(some_reals) 786 end 787 g_key_file_set_double_list 788 (handle, null_or_string(a_group), a_key.to_external, 789 array.to_external, array.count.to_natural_32) 790 end 791 792 set_comment (a_group, a_key, a_comment: STRING) is 793 -- Places `a_comment' above `a_key' from `a_group'. If `a_key' is 794 -- Void then comment will be written above `a_group'. If both `a_key' and 795 -- `a_group' are Void, then comment will be written above the first 796 -- group in the file. 797 798 -- `is_successful' will be True if `a_comment' has actually been 799 -- set. 800 require comment_not_void: a_comment/=Void 801 do 802 is_successful := g_key_file_set_comment 803 (handle, null_or_string(a_group), 804 null_or_string(a_key), a_comment.to_external, 805 error.reference).to_boolean 806 end 807 808feature -- Removing 809 remove_group (a_group: STRING) is 810 -- Removes `a_group' from the key file. `error' is updated. 811 require group_not_void: a_group/=Void 812 local res: INTEGER 813 do 814 res:=g_key_file_remove_group(handle,a_group.to_external,error.reference) 815 end 816 817 remove_key (a_group, a_key: STRING) is 818 -- Removes `a_key' in `a_group' from the key file. 819 require 820 group_not_void: a_group/=Void 821 key_not_void: a_key/=Void 822 local res: INTEGER 823 do 824 res:=g_key_file_remove_key (handle, a_group.to_external, a_key.to_external, error.reference) 825 end 826 827 remove_comment (a_group, a_key: STRING) is 828 -- Removes a comment above `a_key' from `a_group'. If `a_key' 829 -- is Void then comment will be written above `a_group'. If 830 -- both `a_key' and `a_group' are Void, then comment will be 831 -- written above the first group in the file. `error' is 832 -- updated. 833 local res: INTEGER 834 do 835 res:=g_key_file_remove_comment(handle,null_or_string(a_group), 836 null_or_string(a_key), error.reference) 837 end 838end -- class G_KEY_FILE