1/* -*- Mode: C -*-
2  ======================================================================
3  FILE: icalderivedparameters.{c,h}
4  CREATOR: eric 09 May 1999
5
6  $Id: icalderivedparameter.c.in,v 1.9 2008-01-15 23:17:40 dothebart Exp $
7  $Locker:  $
8
9
10 (C) COPYRIGHT 2000, Eric Busboom, http://www.softwarestudio.org
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of either:
14
15    The LGPL as published by the Free Software Foundation, version
16    2.1, available at: http://www.fsf.org/copyleft/lesser.html
17
18  Or:
19
20    The Mozilla Public License Version 1.0. You may obtain a copy of
21    the License at http://www.mozilla.org/MPL/
22
23  The original code is icalderivedparameters.{c,h}
24
25  Contributions from:
26     Graham Davison (g.m.davison@computer.org)
27
28 ======================================================================*/
29/*#line 29 "icalparameter.c.in"*/
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#endif
33
34
35#include "icalparameter.h"
36#include "icalparameterimpl.h"
37
38#include "icalproperty.h"
39#include "icalerror.h"
40#include "icalmemory.h"
41#include "icaltypes.h"
42
43#include <stdlib.h> /* for malloc() */
44#include <errno.h>
45#include <string.h> /* for memset() */
46
47#ifdef WIN32
48#define strcasecmp      stricmp
49#endif
50
51icalvalue_kind icalparameter_value_to_value_kind(icalparameter_value value);
52
53struct icalparameter_impl* icalparameter_new_impl(icalparameter_kind kind);
54
55/* This map associates each of the parameters with the string
56   representation of the parameter's name */
57struct icalparameter_kind_map {
58    icalparameter_kind kind;
59    const char *name;
60
61};
62
63/* This map associates the enumerations for the VALUE parameter with
64   the kinds of VALUEs. */
65
66struct icalparameter_value_kind_map {
67    icalparameter_value value;
68    icalvalue_kind kind;
69};
70
71/* This map associates the parameter enumerations with a specific parameter and the string representation of the enumeration */
72
73struct icalparameter_map {
74    icalparameter_kind kind;
75    int enumeration;
76    const char* str;
77};
78
79
80
81<insert_code_here>
82
83const char* icalparameter_kind_to_string(icalparameter_kind kind)
84{
85    int i;
86
87    for (i=0; parameter_map[i].kind != ICAL_NO_PARAMETER; i++) {
88	if (parameter_map[i].kind == kind) {
89	    return parameter_map[i].name;
90	}
91    }
92
93    return 0;
94
95}
96
97icalparameter_kind icalparameter_string_to_kind(const char* string)
98{
99    int i;
100
101    if (string ==0 ) {
102        return ICAL_NO_PARAMETER;
103    }
104
105    for (i=0; parameter_map[i].kind  != ICAL_NO_PARAMETER; i++) {
106
107        if (strcasecmp(parameter_map[i].name, string) == 0) {
108            return parameter_map[i].kind;
109        }
110    }
111
112    if(strncmp(string,"X-",2)==0){
113        return ICAL_X_PARAMETER;
114    }
115
116    if (ical_get_unknown_token_handling_setting() == ICAL_TREAT_AS_ERROR) {
117        return ICAL_NO_PARAMETER;
118    } else {
119        return ICAL_IANA_PARAMETER;
120    }
121}
122
123
124icalvalue_kind icalparameter_value_to_value_kind(icalparameter_value value)
125{
126    int i;
127
128    for (i=0; value_kind_map[i].kind  != ICAL_NO_VALUE; i++) {
129
130	if (value_kind_map[i].value == value) {
131	    return value_kind_map[i].kind;
132	}
133    }
134
135    return ICAL_NO_VALUE;
136}
137
138
139const char* icalparameter_enum_to_string(int e)
140{
141    int i;
142
143    icalerror_check_arg_rz(e >= ICALPARAMETER_FIRST_ENUM,"e");
144    icalerror_check_arg_rz(e <= ICALPARAMETER_LAST_ENUM,"e");
145
146    for (i=0; icalparameter_map[i].kind != ICAL_NO_PARAMETER; i++){
147        if(e == icalparameter_map[i].enumeration){
148            return icalparameter_map[i].str;
149        }
150    }
151
152    return 0;
153}
154
155int icalparameter_string_to_enum(const char* str)
156{
157    int i;
158
159    icalerror_check_arg_rz(str != 0,"str");
160
161    for (i=0; icalparameter_map[i].kind != ICAL_NO_PARAMETER; i++){
162        if(strcasecmp(str,icalparameter_map[i].str) == 0) {
163            return icalparameter_map[i].enumeration;
164        }
165    }
166
167    return 0;
168}
169
170icalparameter* icalparameter_new_from_value_string(icalparameter_kind kind,const  char* val)
171{
172
173    struct icalparameter_impl* param=0;
174    int found_kind = 0;
175    int i;
176
177    icalerror_check_arg_rz((val!=0),"val");
178
179    /* Search through the parameter map to find a matching kind */
180
181    param = icalparameter_new_impl(kind);
182    if (!param)
183	return 0;
184
185    for (i=0; icalparameter_map[i].kind != ICAL_NO_PARAMETER; i++){
186        if(kind == icalparameter_map[i].kind) {
187            found_kind = 1;
188            if(strcasecmp(val,icalparameter_map[i].str) == 0) {
189
190                param->data = (int)icalparameter_map[i].enumeration;
191                return param;
192            }
193        }
194    }
195
196    if(found_kind == 1){
197        /* The kind was in the parameter map, but the string did not
198           match, so assume that it is an alternate value, like an
199           X-value.*/
200
201        icalparameter_set_xvalue(param, val);
202
203    } else {
204
205        /* If the kind was not found, then it must be a string type */
206
207        ((struct icalparameter_impl*)param)->string = icalmemory_strdup(val);
208
209    }
210
211   return param;
212}
213
214
215
216
217