dhcp-varfile.c

来自「this is sample about DHCP-agent」· C语言 代码 · 共 174 行

C
174
字号
/* $Header: /cvsroot/dhcp-agent/dhcp-agent/src/dhcp-varfile.c,v 1.6 2002/12/23 00:51:25 actmodern Exp $ * Copyright 2002 Thamer Alharbash <tmh@whitefang.com>  *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *  * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. The names of the authors may not be used to endorse or promote * products derived from this software without specific prior * written permission. *  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. *  * varfiles -- variable files. Files with varname or varname =  * varvalue for statements. * * This files are meant to be human readable and be cross platform by writing  * the serialized form of all the values. */#define MODULE_NAME "dhcp-varfile"#include "dhcp-local.h"#include "dhcp-libutil.h"#include "dhcp-tokenizer.h"const char *varfile_get_name(varfile_t *varfile){    return stringbuffer_getstring(varfile->var_name);}const char *varfile_get_val(varfile_t *varfile){    return stringbuffer_getstring(varfile->var_val);}int varfile_get_lineno(varfile_t *varfile){    return tokenizer_get_line_no(varfile->tokenizer);}varfile_t *create_varfile(const char *filename, uint8_t mode){    varfile_t *varfile;    varfile = xmalloc(sizeof(varfile_t));    varfile->filename = xstrdup(filename);    varfile->var_name = stringbuffer_create();    varfile->var_val = stringbuffer_create();    varfile->tokenizer = tokenizer_create(filename);    varfile->mode = mode;    return varfile;}void destroy_varfile(varfile_t *varfile){    tokenizer_destroy(varfile->tokenizer);    stringbuffer_destroy(varfile->var_name);    stringbuffer_destroy(varfile->var_val);    xfree(varfile->filename);    xfree(varfile);    return;}const char *varfile_get_filename(varfile_t *varfile){    return varfile->filename;}int varfile_get_next_varnames(varfile_t *varfile){    token_t token;    while(1) {        token = tokenizer_get_next_token(varfile->tokenizer);        if(token == TOKEN_NEWLINE) /* skip empty lines/comments. */            continue;        if(token == TOKEN_EOF)            return VARFILE_EOF;        if(token != TOKEN_STRING)            return VARFILE_PARSE_ERROR;        /* copy string out into varname */        stringbuffer_set(varfile->var_name, tokenizer_get_data(varfile->tokenizer));        token = tokenizer_get_next_token(varfile->tokenizer);        /* if we don't get an end of statement or eof here it's a parse error. */        if(token != TOKEN_NEWLINE && token != TOKEN_EOF)            return VARFILE_PARSE_ERROR;        return VARFILE_SUCCESS;    }}int varfile_get_next_varval(varfile_t *varfile){    token_t token;    /* get the name first. */    while(1) {        token = tokenizer_get_next_token(varfile->tokenizer);        if(token == TOKEN_NEWLINE) /* skip empty lines/comments. */            continue;        if(token == TOKEN_EOF)            return VARFILE_EOF;        if(token != TOKEN_STRING)            return VARFILE_PARSE_ERROR;        /* copy string out into varname */        stringbuffer_set(varfile->var_name, tokenizer_get_data(varfile->tokenizer));        break;    }    token = tokenizer_get_next_token(varfile->tokenizer);    if(token != TOKEN_ASSIGNMENT)        return VARFILE_PARSE_ERROR;    token = tokenizer_get_next_token(varfile->tokenizer);    if(token != TOKEN_STRING)        return VARFILE_PARSE_ERROR;    /* copy string out into var val */    stringbuffer_set(varfile->var_val, tokenizer_get_data(varfile->tokenizer));    /* now we need a clean end of statement. */    token = tokenizer_get_next_token(varfile->tokenizer);    if(token != TOKEN_NEWLINE && token != TOKEN_EOF)        return VARFILE_PARSE_ERROR;    return VARFILE_SUCCESS;}/* Get the next var statement from file. */int varfile_get_next_var(varfile_t *varfile){    switch (varfile->mode) {    case VARFILE_VARNAMES_MODE:        return (varfile_get_next_varnames(varfile));    case VARFILE_VARVAL_MODE:        return (varfile_get_next_varval(varfile));    default:        FATAL_MESSAGE("varfile: file_get_next_var: encountered a fatal bug -- please report this.");    }    /* should never get here. */    return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?