/*1* Copyright (c) 2001-2002 Michael David Adams.2* All rights reserved.3*/45/* __START_OF_JASPER_LICENSE__6*7* JasPer License Version 2.08*9* Copyright (c) 2001-2006 Michael David Adams10* Copyright (c) 1999-2000 Image Power, Inc.11* Copyright (c) 1999-2000 The University of British Columbia12*13* All rights reserved.14*15* Permission is hereby granted, free of charge, to any person (the16* "User") obtaining a copy of this software and associated documentation17* files (the "Software"), to deal in the Software without restriction,18* including without limitation the rights to use, copy, modify, merge,19* publish, distribute, and/or sell copies of the Software, and to permit20* persons to whom the Software is furnished to do so, subject to the21* following conditions:22*23* 1. The above copyright notices and this permission notice (which24* includes the disclaimer below) shall be included in all copies or25* substantial portions of the Software.26*27* 2. The name of a copyright holder shall not be used to endorse or28* promote products derived from the Software without specific prior29* written permission.30*31* THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS32* LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER33* THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS34* "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING35* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A36* PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO37* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL38* INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING39* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,40* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION41* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE42* PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE43* THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.44* EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS45* BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL46* PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS47* GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE48* ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE49* IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL50* SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,51* AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL52* SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH53* THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,54* PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH55* RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY56* EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.57*58* __END_OF_JASPER_LICENSE__59*/6061/*62* Tag-Value Parser Library63*64* $Id: jas_tvp.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $65*/6667/******************************************************************************\68* Includes.69\******************************************************************************/7071#include <assert.h>72#include <stdio.h>73#include <ctype.h>74#include <stdlib.h>7576#include "jasper/jas_malloc.h"77#include "jasper/jas_string.h"78#include "jasper/jas_tvp.h"7980/******************************************************************************\81* Macros.82\******************************************************************************/8384/* Is the specified character valid for a tag name? */85#define JAS_TVP_ISTAG(x) \86(isalpha(x) || (x) == '_' || isdigit(x))8788/******************************************************************************\89* Code for creating and destroying a tag-value parser.90\******************************************************************************/9192jas_tvparser_t *jas_tvparser_create(const char *s)93{94jas_tvparser_t *tvp;95if (!(tvp = jas_malloc(sizeof(jas_tvparser_t)))) {96return 0;97}98if (!(tvp->buf = jas_strdup(s))) {99jas_tvparser_destroy(tvp);100return 0;101}102tvp->pos = tvp->buf;103tvp->tag = 0;104tvp->val = 0;105return tvp;106}107108void jas_tvparser_destroy(jas_tvparser_t *tvp)109{110if (tvp->buf) {111jas_free(tvp->buf);112}113jas_free(tvp);114}115116/******************************************************************************\117* Main parsing code.118\******************************************************************************/119120/* Get the next tag-value pair. */121int jas_tvparser_next(jas_tvparser_t *tvp)122{123char *p;124char *tag;125char *val;126127/* Skip any leading whitespace. */128p = tvp->pos;129while (*p != '\0' && isspace(*p)) {130++p;131}132133/* Has the end of the input data been reached? */134if (*p == '\0') {135/* No more tags are present. */136tvp->pos = p;137return 1;138}139140/* Does the tag name begin with a valid character? */141if (!JAS_TVP_ISTAG(*p)) {142return -1;143}144145/* Remember where the tag name begins. */146tag = p;147148/* Find the end of the tag name. */149while (*p != '\0' && JAS_TVP_ISTAG(*p)) {150++p;151}152153/* Has the end of the input data been reached? */154if (*p == '\0') {155/* The value field is empty. */156tvp->tag = tag;157tvp->val = "";158tvp->pos = p;159return 0;160}161162/* Is a value field not present? */163if (*p != '=') {164if (*p != '\0' && !isspace(*p)) {165return -1;166}167*p++ = '\0';168tvp->tag = tag;169tvp->val = "";170tvp->pos = p;171return 0;172}173174*p++ = '\0';175176val = p;177while (*p != '\0' && !isspace(*p)) {178++p;179}180181if (*p != '\0') {182*p++ = '\0';183}184185tvp->pos = p;186tvp->tag = tag;187tvp->val = val;188189return 0;190}191192/******************************************************************************\193* Code for querying the current tag/value.194\******************************************************************************/195196/* Get the current tag. */197char *jas_tvparser_gettag(jas_tvparser_t *tvp)198{199return tvp->tag;200}201202/* Get the current value. */203char *jas_tvparser_getval(jas_tvparser_t *tvp)204{205return tvp->val;206}207208/******************************************************************************\209* Miscellaneous code.210\******************************************************************************/211212/* Lookup a tag by name. */213jas_taginfo_t *jas_taginfos_lookup(jas_taginfo_t *taginfos, const char *name)214{215jas_taginfo_t *taginfo;216taginfo = taginfos;217while (taginfo->id >= 0) {218if (!strcmp(taginfo->name, name)) {219return taginfo;220}221++taginfo;222}223return 0;224}225226/* This function is simply for convenience. */227/* One can avoid testing for the special case of a null pointer, by228using this function. This function never returns a null pointer. */229jas_taginfo_t *jas_taginfo_nonull(jas_taginfo_t *taginfo)230{231static jas_taginfo_t invalidtaginfo = {232-1, 0233};234235return taginfo ? taginfo : &invalidtaginfo;236}237238239