• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 2008 The Android Open Source Project
2 **
3 ** This software is licensed under the terms of the GNU General Public
4 ** License version 2, as published by the Free Software Foundation, and
5 ** may be copied, distributed, and modified under those terms.
6 **
7 ** This program is distributed in the hope that it will be useful,
8 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
9 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 ** GNU General Public License for more details.
11 */
12 #ifndef _ANDROID_UTILS_INI_H
13 #define _ANDROID_UTILS_INI_H
14 
15 #include <stdint.h>
16 
17 /* the emulator supports a simple .ini file format for its configuration
18  * files. See docs/ANDROID-CONFIG-FILES.TXT for details.
19  */
20 
21 /* an opaque structure used to model an .ini configuration file */
22 typedef struct IniFile   IniFile;
23 
24 /* creates a new IniFile object from a config file loaded in memory.
25  *  'fileName' is only used when writing a warning to stderr in case
26  * of badly formed output
27  */
28 IniFile*  iniFile_newFromMemory( const char*  text, const char*  fileName  );
29 
30 /* creates a new IniFile object from a file path,
31  * returns NULL if the file cannot be opened.
32  */
33 IniFile*  iniFile_newFromFile( const char*  filePath);
34 
35 /* try to write an IniFile into a given file.
36  * returns 0 on success, -1 on error (see errno for error code)
37  */
38 int       iniFile_saveToFile( IniFile*  f, const char*  filePath );
39 
40 /* try to write an IniFile into a given file, ignorig pairs with empty values.
41  * returns 0 on success, -1 on error (see errno for error code)
42  */
43 int       iniFile_saveToFileClean( IniFile*  f, const char*  filepath );
44 
45 /* free an IniFile object */
46 void      iniFile_free( IniFile*  f );
47 
48 /* returns the number of (key.value) pairs in an IniFile */
49 int       iniFile_getPairCount( IniFile*  f );
50 
51 /* returns the value of a given key from an IniFile.
52  * NULL if the key is not assigned in the corresponding configuration file
53  */
54 const char*  iniFile_getValue( IniFile*  f, const char*  key );
55 
56 /* Copies a 'key, value' pair for an entry in the file.
57  * Param:
58  *  f - Initialized IniFile instance.
59  *  index - Index of the entry to copy. Must be less than value returned from the
60  *      iniFile_getPairCount routine.
61  *  key, value - Receives key, and value strings for the entry. If this routine
62  *      succeeds, the caller must free the buffers allocated for the strings.
63  * Return:
64  *  0 on success, -1 if the index exceeds the capacity of the file
65  */
66 int     iniFile_getEntry(IniFile* f, int index, char** key, char** value);
67 
68 /* returns a copy of the value of a given key, or NULL if defaultValue is NULL.
69  * caller must free() it.
70  */
71 char*   iniFile_getString( IniFile*  f, const char*  key, const char* defaultValue );
72 
73 /* returns an integer value, or a default in case the value string is
74  * missing or badly formatted
75  */
76 int     iniFile_getInteger( IniFile*  f, const char*  key, int  defaultValue );
77 
78 /* returns a 64-bit integer value, or a default in case the value string is
79  * missing or badly formatted
80  */
81 int64_t iniFile_getInt64( IniFile*  f, const char*  key, int64_t  defaultValue );
82 
83 /* returns a double value, or a default in case the value string is
84  * missing or badly formatted
85  */
86 double  iniFile_getDouble( IniFile*  f, const char*  key, double  defaultValue );
87 
88 /* parses a key value as a boolean. Accepted values are "1", "0", "yes", "YES",
89  * "no" and "NO". Returns either 1 or 0.
90  * note that the default value must be provided as a string too
91  */
92 int     iniFile_getBoolean( IniFile*  f, const char*  key, const char*  defaultValue );
93 
94 /* parses a key value as a disk size. this means it can be an integer followed
95  * by a suffix that can be one of "mMkKgG" which correspond to KiB, MiB and GiB
96  * multipliers.
97  *
98  * NOTE: we consider that 1K = 1024, not 1000.
99  */
100 int64_t  iniFile_getDiskSize( IniFile*  f, const char*  key, const char*  defaultValue );
101 
102 /* These functions are used to set values in an IniFile */
103 void iniFile_setValue( IniFile* f, const char* key, const char* value );
104 void iniFile_setInteger( IniFile* f, const char* key, int value );
105 void iniFile_setInt64( IniFile* f, const char* key, int64_t value );
106 void iniFile_setDouble( IniFile* f, const char* key, double value );
107 void iniFile_setBoolean( IniFile* f, const char* key, int value );
108 void iniFile_setDiskSize( IniFile* f, const char* key, int64_t size );
109 
110 /* */
111 
112 #endif /* _ANDROID_UTILS_INI_H */
113