公 告

欢迎各位网友添加友情链接,在您添加本博客:http://linux0818.blogspot.com/ 做为链接后, E-mail:linux0818@gmail.com给我,我将将您的网址添加到本博客。

2009年3月23日星期一

OpenSource之Linux文件格式的读,写,查找等一系列函数

源代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define STRING_SIZE 1024
#define FILENAME "/root/fxiong/info"

struct keyvalue {
char key[STRING_SIZE];
char value[STRING_SIZE];
struct keyvalue *next;
};


/* Sets up the list. First entry is a dummy one to avoid having to special
* * case empty lists. */
struct keyvalue *initkeyvalues(void)
{
struct keyvalue *head = malloc(sizeof(struct keyvalue));

strcpy(head->key, "KEY");
strcpy(head->value, "VALUE");
head->next = NULL;

return head;
}

/* Splats all the entries in a list. */
void freekeyvalues(struct keyvalue *head)
{
struct keyvalue *cur = head->next;
struct keyvalue *next;

while (cur) {
next = cur->next;
free(cur);
cur = next;
}
}


/*
* Appends a entry. Not very efficent because it rescans the list looking
* for the end. Maybe fix this later.
*/
void appendkeyvalue(struct keyvalue *head, char *key, char *value)
{
struct keyvalue *new = malloc(sizeof(struct keyvalue));
struct keyvalue *cur = head->next;
struct keyvalue *tail = head;

strncpy(new->key, key, STRING_SIZE);
strncpy(new->value, value, STRING_SIZE);
new->next = NULL;

while (cur) {
tail = cur;
cur = cur->next;
}
tail->next = new;
}

/* Reads from a file into a new list. Uses appendkeyvalue to add entries.
* * Will bomb out on a error (eg bad format line). */
int readkeyvalues(struct keyvalue *head, char *filename)
{
FILE *file;
char buffer[STRING_SIZE];
char *temp;
char *key, *value;

if (!(file = fopen(filename, "r")))
return 0;

while (fgets(buffer, STRING_SIZE, file)) {
temp = buffer;
while (*temp) {
if (*temp == '\n')
*temp = '\0';
temp++;
}
if (!strlen(buffer))
continue;
if (!(temp = strchr(buffer, '='))) {
fclose(file);
return 0;
}
*temp = '\0';
key = buffer;
value = temp + 1;
/* See if string is quoted. If so, skkip first quote, and
* * nuke the one at the end. */
if (value[0] == '\'') {
value++;
if ((temp = strrchr(value, '\'')))
*temp = '\0';
else {
fclose(file);
return 0;
}
}
if (strlen(key))
appendkeyvalue(head, key, value);
}

fclose(file);

return 1;
}

/* Writes out a list to a file. Easy. */
int writekeyvalues(struct keyvalue *head, char *filename)
{
FILE *file;
struct keyvalue *cur = head->next;

if (!(file = fopen(filename, "w")))
return 0;

while (cur) {
/* No space in value? If there is, we need to quote the value
* * * so the shell can read it. */
if (!strchr(cur->value, ' '))
fprintf(file, "%s=%s\n", cur->key, cur->value);
else
fprintf(file, "%s=\'%s\'\n", cur->key, cur->value);
cur = cur->next;
}

fclose(file);

return 1;
}


/* Finds a key and copies the value back. Would be nice to have a func that
* * just returns a pointer to the value? */
int findkey(struct keyvalue *head, char *key, char *value)
{
struct keyvalue *cur = head->next;

while (cur) {
if (strcmp(key, cur->key) == 0) {
strcpy(value, cur->value);
return 1;
}
cur = cur->next;
}

return 0;
}



/* Otherwrites a key with a new value, or if it dosn't exist, appends it
* * on the end. */
void replacekeyvalue(struct keyvalue *head, char *key, char *value)
{
struct keyvalue *cur = head->next;

while (cur) {
if (strcmp(cur->key, key) == 0) {
strncpy(cur->value, value, STRING_SIZE);
return;
}
cur = cur->next;
}

appendkeyvalue(head, key, value);
}

int main(int argc, char **argv)
{
char value[64] = "";
int ret = 0;
struct keyvalue *kv = initkeyvalues();
struct keyvalue *rkv = initkeyvalues();

replacekeyvalue(kv, "Name", "XiongFeng");
replacekeyvalue(kv, "Sex", "Man");
replacekeyvalue(kv, "Age", "25");
replacekeyvalue(kv, "Phone", "123456789");

writekeyvalues(kv, FILENAME);

readkeyvalues(rkv, FILENAME);

if (findkey(rkv, "Sex", value))
printf("Found the value:%s\n", value);
else
printf("Not found the value!\n");

freekeyvalues(kv);
freekeyvalues(rkv);


return 0;
}

运行结果:
Found the value:Man

该程序运行会在/root/fxiong/下创建一个info文件,将数据按照一定格式写入该文件。
程序简单,实用,不错吧 : )
--
/**************************************/
Name: Xiong Feng
E-mail:linux0818@gmail.com
MSN:linux0818@hotmail.com
QQ:23562033
Address: GuangZhou.China
/**************************************/

没有评论:

发表评论