最近有个案子需要检测系统是否为正常关机,使用读写文件的方式实现,主要使用到了
glib库函数,下面是code片段,其中AP_CONFIG_FILE_PATH定义如下:
#define AP_CONFIG_FILE_PATH "/usr/share/archer/ap_checker.conf"
配置文档“ap_checker.conf”的内容为:
[ap_checker]
power_off_normal=0
ap_ck_reboot_count=0
system_reboot_count=0
具体code:
static int archer_check_power_off_normal(void)
{
GKeyFile *keyfile;
GKeyFileFlags flags;
GError *error = NULL;
gchar *file_buf = NULL;
gsize length;
keyfile = g_key_file_new();
flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;
if(!g_key_file_load_from_file(keyfile, AP_CONFIG_FILE_PATH, flags, &error))
{
printf("ERR:Load AP_CONFIG_FILE_PATH error!\n");
g_key_file_free(keyfile);
}
else
{
if (g_key_file_get_integer(keyfile, "ap_checker", "power_off_normal", NULL) == POWER_OFF_NORMAL_NO)
{
g_key_file_set_integer(keyfile, "ap_checker", "ap_ck_reboot_count", 0);
g_key_file_set_integer(keyfile, "ap_checker", "system_reboot_count", 0);
}
else
{
g_key_file_set_integer(keyfile, "ap_checker", "power_off_normal", 0);
}
file_buf = g_key_file_to_data(keyfile, &length, &error);
g_file_set_contents(AP_CONFIG_FILE_PATH, file_buf, -1, &error);
}
g_key_file_free(keyfile);
return 0;
}
库函数说明:
g_key_file_new ()
GKeyFile * g_key_file_new(void);
Creates a new empty GKeyFile object. Use g_key_file_load_from_file(), g_key_file_load_from_data(), g_key_file_load_from_dirs() or g_key_file_load_from_data_dirs() to read an existing key file.
Returns : an empty GKeyFile.
g_key_file_free ()
void g_key_file_free(GKeyFile *key_file);
Frees a GKeyFile.
key_file : a GKeyFile
g_key_file_load_from_file ()
gboolean g_key_file_load_from_file(GKeyFile *key_file,
const gchar *file,
GKeyFileFlags flags,
GError **error);
Loads a key file into an empty GKeyFile structure. If the file could not be loaded then error is set to either a GFileError or GKeyFileError.
key_file : an empty GKeyFile struct
file : the path of a filename to load, in the GLib filename encoding
flags : flags from GKeyFileFlags
error : return location for a GError, or NULL
Returns : TRUE if a key file could be loaded, FALSE otherwise
g_key_file_get_integer ()
gint g_key_file_get_integer(GKeyFile *key_file,
const gchar *group_name,
const gchar *key,
GError **error);
Returns the value associated with key under group_name as an integer.
If key cannot be found then 0 is returned and error is set to G_KEY_FILE_ERROR_KEY_NOT_FOUND. Likewise, if the value associated with key cannot be interpreted as an integer then 0 is returned and error is set to G_KEY_FILE_ERROR_INVALID_VALUE.
key_file : a GKeyFile
group_name : a group name
key : a key
error : return location for a GError
Returns : the value associated with the key as an integer, or 0 if the key was not found or could not be parsed.
g_key_file_set_integer ()
void g_key_file_set_integer(GKeyFile *key_file,
const gchar *group_name,
const gchar *key,
gint value);
Associates a new integer value with key under group_name. If key cannot be found then it is created.
key_file : a GKeyFile
group_name : a group name
key : a key
value : an integer value
g_key_file_to_data ()
gchar * g_key_file_to_data(GKeyFile *key_file,
gsize *length,
GError **error);
This function outputs key_file as a string.
Note that this function never reports an error, so it is safe to pass NULL as error.
key_file : a GKeyFile
length : return location for the length of the returned string, or NULL
error : return location for a GError, or NULL
Returns : a newly allocated string holding the contents of the GKeyFile
g_file_set_contents ()
gboolean g_file_set_contents(const gchar *filename,
const gchar *contents,
gssize length,
GError **error);
Writes all of contents to a file named filename, with good error checking. If a file called filename already exists it will be overwritten.
This write is atomic in the sense that it is first written to a temporary file which is then renamed to the final name. Notes:
* On Unix, if filename already exists hard links to filename will break. Also since the file is recreated, existing permissions, access control lists, metadata etc. may be lost. If filename is a symbolic link, the link itself will be replaced, not the linked file.
* On Windows renaming a file will not remove an existing file with the new name, so on Windows there is a race condition between the existing file being removed and the temporary file being renamed.
* On Windows there is no way to remove a file that is open to some process, or mapped into memory. Thus, this function will fail if filename already exists and is open.
If the call was sucessful, it returns TRUE. If the call was not successful, it returns FALSE and sets error. The error domain is G_FILE_ERROR. Possible error codes are those in the GFileError enumeration.
filename : name of a file to write contents to, in the GLib file name encoding
contents : string to write to the file
length : length of contents, or -1 if contents is a nul-terminated string
error : return location for a GError, or NULL
Returns : TRUE on success, FALSE if an error occurred
此外还要注意keyfile的flags设定,其实现如下:
enum GKeyFileFlags
typedef enum
{
G_KEY_FILE_NONE = 0,
G_KEY_FILE_KEEP_COMMENTS = 1 << 0,
G_KEY_FILE_KEEP_TRANSLATIONS = 1 << 1
} GKeyFileFlags;
描述如下:
Flags which influence the parsing.
G_KEY_FILE_NONE
No flags, default behaviour
G_KEY_FILE_KEEP_COMMENTS
Use this flag if you plan to write the (possibly modified) contents of the key file back to a file; otherwise all comments will be lost when the key file is written back.
G_KEY_FILE_KEEP_TRANSLATIONS
Use this flag if you plan to write the (possibly modified) contents of the key file back to a file; otherwise only the translations for the current language will be written back.
For more information please refer to these website:
http://library.gnome.org/devel/glib/unstable/glib-Key-value-file-parser.html
http://library.gnome.org/devel/glib/unstable/glib-File-Utilities.html
阅读(3933) | 评论(0) | 转发(1) |