1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
/* $Id: pilalias.c,v 1.1.1.1 2008-10-21 09:10:13 cizzo Exp $
*
* This file is part of the VIMOS pipeline library
* Copyright (C) 2000-2004 European Southern Observatory
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* $Author: cizzo $
* $Date: 2008-10-21 09:10:13 $
* $Revision: 1.1.1.1 $
* $Name: not supported by cvs2svn $
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "pilmemory.h"
#include "pilstrutils.h"
#include "pilkeyword.h"
#include "pilalias.h"
/*
* The alias object type
*/
struct _PIL_ALIAS_ {
PilKeyword *keyword;
char *format;
};
void deletePilAlias(PilAlias *);
/**
* @defgroup pilAlias pilAlias
*
* The module @b pilAlias implements an alias class.
*
* An alias consists of a name alias string, i.e. its name and the
* translation of the alias string, i.e. its value. In addition to this
* an alias must have format string used for printing and an optional
* comment.
*/
/**@{*/
/**
* @brief
* Create a new alias.
*
* @return The function returns a pointer to the allocated alias object if
* no error occurred, otherwise a @c NULL pointer is returned.
*
* @param name Name alias.
* @param value Translated alias.
* @param format Format string.
* @param comment Comment string.
*
* The function allocates the memory for a new alias object. The name alias
* string @em name, its associated mapping @em value and the format string
* @em format are used to initialize the structure and have to be given.
* The alias description @em comment is optional. If @em comment is @c NULL
* the alias description is left empty, i.e. set to @c NULL.
*
* The strings passed for @em name, @em value, @em format and @em comment
* have to be 0 terminated.
*
* The keyword fields are filled by copying the passed input strings.
*/
PilAlias *newPilAlias(const char *name, const char *value,
const char *format, const char *comment)
{
PilAlias *self;
assert(name != 0 && value !=0 && format != 0);
if ((self = (PilAlias *)pil_malloc(sizeof *self))) {
if (!(self->keyword = newPilKeyword(name, value, comment))) {
deletePilAlias(self);
return 0L;
}
if (!(self->format = pil_strdup(format))) {
deletePilAlias(self);
return 0L;
}
}
return self;
}
/**
* @brief
* Destroy an alias object.
*
* @return Nothing.
*
* @param alias Alias object.
*
* The function first deallocates all previously allocated alias object
* members and finally the alias object @em alias itself.
*
* If @em alias is @c NULL, no operation is performed.
*/
void deletePilAlias(PilAlias *alias)
{
if (!alias)
return;
if (alias->keyword)
deletePilKeyword(alias->keyword);
if (alias->format)
pil_free(alias->format);
pil_free(alias);
return;
}
/**
* @brief
* Retrieve the name alias from an alias object.
*
* @return The function returns the name alias string on success and @c NULL
* if an error occurred.
*
* @param alias Alias object.
*
* The function queries the alias object @em alias for the name alias
* string. The alias object must exist.
*/
const char *pilAliasGetName(PilAlias *alias)
{
assert(alias != 0L);
return pilKeyGetName(alias->keyword);
}
/**
* @brief
* Set the name alias string of an alias object.
*
* @return The function returns @c EXIT_SUCCESS if no error occurred,
* otherwise the return value is @c EXIT_FAILURE.
*
* @param alias Alias object.
* @param name Name string.
*
* The function sets the name alias string @em name of the alias object
* @em alias. The alias object must exist.
*/
int pilAliasSetName(PilAlias *alias, const char *name)
{
assert(alias != 0L && name != 0L);
return pilKeySetName(alias->keyword, name);
}
/**
* @brief
* Retrieve the name value from an alias object.
*
* @return The function returns the name value string on success and @c NULL
* if an error occurred.
*
* @param alias Alias object.
*
* The function queries the alias object @em alias for the alias translation,
* i.e. the name value string. The alias object must exist.
*/
const char *pilAliasGetValue(PilAlias *alias)
{
assert(alias != 0L);
return pilKeyGetValue(alias->keyword);
}
/**
* @brief
* Set the value string of an alias object.
*
* @return The function returns @c EXIT_SUCCESS if no error occurred,
* otherwise the return value is @c EXIT_FAILURE.
*
* @param alias Alias object.
* @param value Value string.
*
* The function sets the value string @em value of the alias object
* @em alias. The alias object must exist.
*/
int pilAliasSetValue(PilAlias *alias, const char *value)
{
assert(alias != 0L && value != 0L);
return pilKeySetValue(alias->keyword, value);
}
/**
* @brief
* Retrieve the format string from an alias object.
*
* @return The function returns the format string on success and @c NULL
* if an error occurred.
*
* @param alias Alias object.
*
* The function queries the alias object @em alias for the format string.
* The alias object must exist.
*/
const char *pilAliasGetFormat(PilAlias *alias)
{
assert(alias != 0L);
return alias->format;
}
/**
* @brief
* Set the format string of an alias object.
*
* @return The function returns @c EXIT_SUCCESS if no error occurred,
* otherwise the return value is @c EXIT_FAILURE.
*
* @param alias Alias object.
* @param format Format string.
*
* The function sets the format string @em format of the alias object
* @em alias. The alias object must exist.
*
* For details on the format string refer to the @b fprintf() documentation.
*/
int pilAliasSetFormat(PilAlias *alias, const char *format)
{
assert(alias != 0L && format != 0L);
if (alias->format)
pil_free(alias->format);
if (!(alias->format = pil_strdup(format)))
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
/**
* @brief
* Retrieve the comment from an alias object.
*
* @return The function returns the comment string on success and @c NULL
* if an error occurred.
*
* @param alias Alias object.
*
* The function queries the alias object @em alias for the comment string.
* The alias object must exist.
*/
const char *pilAliasGetComment(PilAlias *alias)
{
assert(alias != 0L);
return pilKeyGetComment(alias->keyword);
}
/**
* @brief
* Set the comment string of an alias object.
*
* @return The function returns @c EXIT_SUCCESS if no error occurred,
* otherwise the return value is @c EXIT_FAILURE.
*
* @param alias Alias object.
* @param comment Comment string.
*
* The function sets the comment string @em comment of the alias object
* @em alias. The alias object must exist.
*/
int pilAliasSetComment(PilAlias *alias, const char *comment)
{
assert(alias != 0L && comment != 0L);
return pilKeySetComment(alias->keyword, comment);
}
/**
* @brief
* Set the name, value, format and optionally the comment of an alias
* object.
*
* @return The function returns @c EXIT_SUCCESS if no error occurred,
* otherwise the return value is @c EXIT_FAILURE.
*
* @param alias Alias object.
* @param name Name string.
* @param value Value string.
* @param format Format string.
* @param comment Comment string or @c NULL.
*
* The function sets the @em name, @em value, @em format and, if it is
* provided, the @em comment of the alias object @em alias.
*/
int pilAliasSet(PilAlias *alias, const char *name, const char *value,
const char *format, const char *comment)
{
assert(alias != 0L && name != 0L && value != 0L && format != 0L);
if (pilKeySet(alias->keyword, name, value, comment) == EXIT_FAILURE)
return EXIT_FAILURE;
if (pilAliasSetFormat(alias, format) == EXIT_FAILURE)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
/**
* @brief
* Clear the contents of an alias object.
*
* @return Nothing.
*
* @param alias Alias object to be reset.
*
* The function resets the value of all members of the alias object
* @em alias to @c NULL.
*/
void pilAliasClear(PilAlias *alias)
{
assert(alias != 0L);
pilKeyClear(alias->keyword);
if (alias->format)
pil_free(alias->format);
alias->format = 0L;
return;
}
/**@}*/
|