?? read_ini.c
字號:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/lib/read_ini.c,v 1.3 2003/01/15 14:04:34 josh Exp $ *//* * Copyright (C) 1999-2005 Wind River Systems, Inc. * All rights reserved. Provided under license only. * Distribution or other use of this software is only * permitted pursuant to the terms of a license agreement * from Wind River Systems (and is otherwise prohibited). * Refer to that license agreement for terms of use. *//**************************************************************************** * Copyright 1993-1997 Epilogue Technology Corporation. * Copyright 1998 Integrated Systems, Inc. * All rights reserved. ****************************************************************************//* * $Log: read_ini.c,v $ * Revision 1.3 2003/01/15 14:04:34 josh * directory structure shifting * * Revision 1.2 2001/11/08 15:56:25 tneale * Updated for newest file layout * * Revision 1.1.1.1 2001/11/05 17:48:42 tneale * Tornado shuffle * * Revision 2.15 2001/01/19 22:23:50 paul * Update copyright. * * Revision 2.14 2000/03/17 00:12:44 meister * Update copyright message * * Revision 2.13 1999/04/26 19:05:01 sra * Fix a few minor warnings, fix a few Solaris-specific problems in the * demo code. * * Revision 2.12 1998/07/15 17:49:28 meister * added nidrive.c and nidrv.h. * * Revision 2.11 1998/07/02 21:23:49 sra * Rearrange #include files to fix stdio dependency. * * Revision 2.10 1998/06/28 20:09:38 sra * Add support for SNARK_READ_INI_FROM_MEMORY. * * Revision 2.9 1998/05/13 21:16:13 meister * only compile this if SNARK_PSOS_NO_PHILE is not installed * * Revision 2.8 1998/02/25 04:57:36 sra * Update copyrights. * * Revision 2.7 1997/03/20 06:53:09 sra * DFARS-safe copyright text. Zap! * * Revision 2.6 1997/03/19 20:20:40 sra * Remove gratuitous Attache dependencies. * * Revision 2.5 1997/03/19 04:47:35 sra * Get rid of some gratuitous historical dependencies on Attache. * * Revision 2.4 1997/02/25 10:58:16 sra * Update copyright notice, dust under the bed. * * Revision 2.3 1996/10/25 16:01:00 sar * Added some include files to get the configuration stuff correct. * * Revision 2.2 1996/03/22 10:05:39 sra * Update copyrights prior to Attache 3.2 release. * * Revision 2.1 1995/07/31 13:54:55 sra * OSPF now working well enough to let somebody else test it. * * Revision 2.0 1995/05/10 22:38:15 sra * Attache release 3.0. * * Revision 1.3 1995/01/06 00:52:48 sra * Update copyright notice for 2.1 release. * * Revision 1.2 1993/08/14 05:04:29 sra * Don't invalidate handle->iter_variable in ini_scan_for_section(), * Shawn's SNMPv2 configuration code wants it to stay valid. * * Revision 1.1 1993/07/05 21:53:30 sra * Initial revision * *//* [clearcase]modification history-------------------01a,19apr05,job update copyright notices*/#include <wrn/wm/common/config.h>#include <wrn/wm/common/glue.h>#if !INSTALL_SNARK_READ_INI_FROM_MEMORY#include <stdio.h>#include <ctype.h>#endif#include <stdlib.h>#include <wrn/wm/demo/snarklib.h>#include <wrn/wm/demo/read_ini.h>#if !INSTALL_SNARK_READ_INI_FROM_MEMORY#define DOWNCASE(c) (isupper((int) c) ? tolower((int) c) : (int) c)#define LINE_SKIP() \ do { \ while (c != '\n') \ if ((c = fgetc(handle->file)) == EOF) \ return 0; \ } while (0)#define SKIP_WHITE() \ do { \ while ((c == ' ') || (c == '\t')) \ if ((c = fgetc(handle->file)) == EOF) \ return 0; \ } while (0)#define CHECK_TOKEN() \ do { \ while (*cp && DOWNCASE(c) == DOWNCASE(*cp)) { \ if ((c = fgetc(handle->file)) == EOF) \ return 0; \ cp++; \ } \ } while (0)#endif /* INSTALL_SNARK_READ_INI_FROM_MEMORY *//* * If the enviroment variable is set then use it to find the .ini file. * If no environment variable or file not found look in the current directory. * If no local file then look for the global default file. */struct ini_handle *ini_open(size_t buflen){ struct ini_handle *handle = GLUE_ALLOC(sizeof(*handle) + buflen); if (!handle) { fprintf(stderr, "Can't allocate handle for ini file\n"); return 0; } MEMSET(handle, 0, sizeof(*handle)); if ((handle->buflen = buflen) != 0) handle->buffer = (char *) (handle + 1);#if !INSTALL_SNARK_READ_INI_FROM_MEMORY { static char env_ini[] = "ETC_INI"; static char local_file[] = "etc.ini"; static char global_file[] = "/etc/etc.ini"; char *env = getenv(env_ini); if ((!env || (handle->file = fopen(env, "r")) == 0) && (handle->file = fopen(local_file, "r")) == 0 && (handle->file = fopen(global_file, "r")) == 0) { fprintf(stderr, "Can't find init file %s\n", (env ? env : local_file)); GLUE_FREE(handle); return 0; } }#endif /* INSTALL_SNARK_READ_INI_FROM_MEMORY */ return handle;}void ini_close(struct ini_handle *handle){ if (!handle) return;#if !INSTALL_SNARK_READ_INI_FROM_MEMORY if (handle->file) fclose(handle->file);#endif /* INSTALL_SNARK_READ_INI_FROM_MEMORY */ GLUE_FREE(handle);}#if INSTALL_SNARK_READ_INI_FROM_MEMORYstatic unsigned long ini_get_pos(struct ini_handle *handle){ return handle->current_pos;}static void ini_set_pos(struct ini_handle *handle, unsigned long new_pos){ handle->current_pos = new_pos;}#else /* INSTALL_SNARK_READ_INI_FROM_MEMORY */static unsigned long ini_get_pos(struct ini_handle *handle){ return (unsigned long) ftell(handle->file);}static void ini_set_pos(struct ini_handle *handle, unsigned long new_pos){ fseek(handle->file, (long) new_pos, 0);}#endif /* INSTALL_SNARK_READ_INI_FROM_MEMORY *//* * Scans for a section with the right name. */static boolean_t ini_scan_for_section (struct ini_handle *handle, char *section){ if (!handle || !section) return 0; /* bad arguments, lose */ if (section == handle->section_name) { ini_set_pos(handle, handle->section_start); return 1; /* section was cached, win */ } if (handle->section_name) /* has file been used? */ ini_set_pos(handle, 0); /* yeah, rewind it */#if INSTALL_SNARK_READ_INI_FROM_MEMORY { for (;;) { if (!etc_ini_table[handle->current_pos].tag) return 0; /* "EOF", lose */ if (!etc_ini_table[handle->current_pos].value && !STRICMP(section, etc_ini_table[handle->current_pos].tag)) break; /* matching section tag, won */ handle->current_pos++; /* otherwise, keep looking */ } handle->current_pos++; /* won, skip to first variable */ }#else /* INSTALL_SNARK_READ_INI_FROM_MEMORY */ { char *cp; int c; if (!handle->file) return 0; /* bad arguments, lose */ for (;;) { if ((c = fgetc(handle->file)) == EOF) return 0; if (c != '[') { /* not at a section name */ LINE_SKIP(); /* so skip the line and try again */ continue; } if ((c = getc(handle->file)) == EOF) return 0; cp = section; /* check the section name */ CHECK_TOKEN(); if (*cp == '\0' && c == ']') /* matched exactly? */ break; /* win */ LINE_SKIP(); } LINE_SKIP(); /* won, skip to first variable */ }#endif /* INSTALL_SNARK_READ_INI_FROM_MEMORY */ /* * Found the desired section. Cache it and return win. */ handle->section_name = section; handle->section_start = ini_get_pos(handle); return 1;}/* * Scan the current section for the right variable. */static char *ini_scan_for_variable (struct ini_handle *handle, char *variable){ if (!handle || !variable || !handle->buffer || handle->buflen <= 0) return 0;#if INSTALL_SNARK_READ_INI_FROM_MEMORY { for (;;) { if (!etc_ini_table[handle->current_pos].tag || !etc_ini_table[handle->current_pos].value) return 0; /* end of section, lose */ if (!STRICMP(variable, etc_ini_table[handle->current_pos].tag)) break; /* matching variable tag, won */ handle->current_pos++; /* otherwise, keep looking */ } if (STRLEN(etc_ini_table[handle->current_pos].value) >= handle->buflen) return 0; /* buffer too small, lose */ STRCPY(handle->buffer, etc_ini_table[handle->current_pos].value); handle->current_pos++; /* won, skip over line we just read */ }#else /* INSTALL_SNARK_READ_INI_FROM_MEMORY */ { char *cp; int c; if (!handle->file) return 0; for (;;) { if ((c = fgetc(handle->file)) == EOF || c == '[') return 0; cp = variable; /* check the variable name */ CHECK_TOKEN(); if (*cp == '\0') { SKIP_WHITE(); if (c == '=') /* and found right delimiter? */ break; /* win */ } LINE_SKIP(); /* didn't match, keep looking */ } if ((c = fgetc(handle->file)) == EOF) return 0; SKIP_WHITE(); cp = handle->buffer; /* now copy the data */ while (c != '\n') { *cp++ = c; if (cp >= handle->buffer + handle->buflen || (c = fgetc(handle->file)) == EOF) return 0; } *cp = '\0'; }#endif /* INSTALL_SNARK_READ_INI_FROM_MEMORY */ return handle->buffer;}char *ini_lookup(struct ini_handle *handle, char *section, char *variable){ if (!handle || !ini_scan_for_section(handle, section)) return 0; return ini_scan_for_variable(handle, variable);}char *ini_iter_start(struct ini_handle *handle, char *section, char *variable){ if (!handle || !ini_scan_for_section(handle, section)) return 0; return ini_scan_for_variable(handle, (handle->iter_variable = variable));}char *ini_iter_next(struct ini_handle *handle){ if (!handle) return 0; return ini_scan_for_variable(handle, handle->iter_variable);}void ini_save (struct ini_handle *handle, struct ini_handle_saved *save){ save->section_name = handle->section_name; save->section_start = handle->section_start; save->iter_variable = handle->iter_variable; save->current_pos = ini_get_pos(handle);}void ini_restore (struct ini_handle *handle, struct ini_handle_saved *save){ handle->section_name = save->section_name; handle->section_start = save->section_start; handle->iter_variable = save->iter_variable; ini_set_pos(handle, save->current_pos);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -