c语言math函数里的could,【C语言】使用glib库函数操作配置档案

最近有个案子需要检测系统是否为正常关机,使用读写文件的方式实现,主要使用到了

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=0ap_ck_reboot_count=0system_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 structfile : the path of a filename to load, in the GLib filename encodingflags : flags from GKeyFileFlagserror : return location for a GError, or NULLReturns :  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 GKeyFilegroup_name : a group namekey : a keyerror : return location for a GErrorReturns : 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 GKeyFilegroup_name : a group namekey : a keyvalue : 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 GKeyFilelength : return location for the length of the returned string, or NULLerror : return location for a GError, or NULLReturns : 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 encodingcontents : string to write to the filelength : length of contents, or -1 if contents is a nul-terminated stringerror : return location for a GError, or NULLReturns : TRUE on success, FALSE if an error occurred

此外还要注意keyfile的flags设定,其实现如下:

enum GKeyFileFlagstypedef 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_NONENo flags, default behaviourG_KEY_FILE_KEEP_COMMENTSUse 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_TRANSLATIONSUse 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值