/src/wrappers/glib/library/utilities/g_key_file.e

http://github.com/tybor/Liberty · Specman e · 838 lines · 448 code · 100 blank · 290 comment · 8 complexity · 002a7d71c370c0c88dfc4e273515314c MD5 · raw file

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