modules/ud/ud_misc.c

/* [<][>]
[^][v][top][bottom][index][help] */

FUNCTIONS

This source file includes following functions.
  1. attribute_free
  2. attribute_upd
  3. attribute_new1
  4. attribute_new
  5. object_free
  6. object_new
  7. transaction_free
  8. transaction_new

/***************************************
  $Revision: 1.8 $

  Miscellaneous functions to support UD

  Status: NOT REVUED, NOT TESTED

 Author(s):       Chris Ottrey, Andrei Robachevsky

  ******************/ /******************
  Modification History:
        andrei (17/01/2000) Created.
  ******************/ /******************
  Copyright (c) 2000                              RIPE NCC
 
  All Rights Reserved
  
  Permission to use, copy, modify, and distribute this software and its
  documentation for any purpose and without fee is hereby granted,
  provided that the above copyright notice appear in all copies and that
  both that copyright notice and this permission notice appear in
  supporting documentation, and that the name of the author not be
  used in advertising or publicity pertaining to distribution of the
  software without specific, written prior permission.
  
  THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
  AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
  DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 ***************************************/
#include "ud.h"
#include "ud_int.h"

void attribute_free(void *data, void *ptr)
/* [<][>][^][v][top][bottom][index][help] */
{
Attribute_t *attr = data;

        free(attr->value);
        free(attr);
}


Attribute_t *attribute_upd(Attribute_t *attr, int newtype, char *newvalue)
/* [<][>][^][v][top][bottom][index][help] */
{
 attr->type=newtype;
 free(attr->value);
 attr->value=g_strdup(newvalue);
 return(attr);
}

Attribute_t *attribute_new1(int type, const char *value) 
/* [<][>][^][v][top][bottom][index][help] */
{
char *n;
Attribute_t *attr = NULL;      

      attr = (Attribute_t *)calloc(1, sizeof(Attribute_t)+1);
      attr->type = type;
      attr->value = g_strdup(value);
      /* Remove the tailing \n */
      n = index(attr->value, '\n');
      if (n != NULL) {
        *n = '\0';
      }
      /* Strip the whitespace */
      g_strstrip(attr->value);
      return(attr);

}

Attribute_t *attribute_new(const char *line) {
/* [<][>][^][v][top][bottom][index][help] */
  Attribute_t *attr = NULL;
  int type;
  char *colon;
  gchar *token;

  
  colon = index(line, ':'); 
  if (colon != NULL) {
    if (line[0] =='*') {
      token = g_strndup(line+1, 2);
      type = DF_attribute_code2type(token);
    }
    else {
      token = g_strndup(line, (colon - line));
      type = DF_attribute_name2type(token);
    }
    if(token)free(token);

    colon+=2;
    if (type >= 0) {
        attr=attribute_new1(type, colon);
    }
    else {
/*      fprintf(stderr, "ERROR: Bad attribute: %s\n", token);*/
    }
  }
  else {
/*    fprintf(stderr, "ERROR: Not an attribute: %s\n", line);*/
  }
  return attr;
} /* attribute_new() */


void object_free(Object_t *obj) {
/* [<][>][^][v][top][bottom][index][help] */
  if (obj) {
   g_slist_foreach(obj->attributes, attribute_free, NULL);
   g_slist_free(obj->attributes);
   g_string_free(obj->object, TRUE);
   free(obj);
  }
} /* object_free() */

Object_t *object_new(const char *line) {
/* [<][>][^][v][top][bottom][index][help] */
  Object_t *obj = NULL;

  int type;
  char *colon;
  gchar *token;

  colon = index(line, ':'); 
  if (colon != NULL) {
    if (line[0] =='*') {
      token = g_strndup(line+1, 2);
      type = DF_class_code2type(token);
    }
    else {
      token = g_strndup(line, (colon - line));
      type = DF_class_name2type(token);
    }
    if(token)free(token);
    
    if (type >= 0) {
      obj = (Object_t *)calloc(1, sizeof(Object_t)+1);
      obj->attributes = NULL;
      obj->object = g_string_sized_new(STR_XXXL);
      obj->type = type;
    }
    else {
/*      fprintf(stderr, "ERROR: Object has an invalid class: %s\n");*/
    }
  }
  else {
/*    fprintf(stderr, "ERROR: No colon found in line: %s\n", line);*/
  }

  return obj;
} /* object_new() */




/************************************************************
*void transaction_free(Transaction_t *tr)
*
* **********************************************************/ 
void transaction_free(Transaction_t *tr) {
/* [<][>][^][v][top][bottom][index][help] */
  if(tr) {
    g_string_free(tr->error_script, TRUE);
    /* free nic_handle_t structure used for NHR stuff */
    if(tr->nh)free_nh(tr->nh);
    if(tr->save)free(tr->save);
    free(tr);
  }  
} /* transaction_free() */

/************************************************************
*Transaction_t *transaction_new(SQ_connection_t *sql_connection, C_Type_t class_type)
*
* **********************************************************/ 
Transaction_t *transaction_new(SQ_connection_t *sql_connection, C_Type_t class_type) {
/* [<][>][^][v][top][bottom][index][help] */
  Transaction_t *tr = (Transaction_t *)calloc(1, sizeof(Transaction_t));

  if (tr != NULL) {
    tr->sql_connection = sql_connection;
    tr->class_type = class_type;
    tr->thread_upd=TR_UPDATE;
    tr->thread_ins=TR_INSERT;
    tr->object_id = 0; /* Just in case*/
    tr->succeeded = 1;
    tr->error=0;
    tr->error_script = g_string_sized_new(STR_XL);
    tr->dummy = 0; /* By default do not create dummies except for sets*/
    tr->ndummy=0;
    tr->action=100;
    tr->load_pass=0; /* by default*/
    tr->sequence_id=1; /* we start from 1*/
    tr->save=NULL;
    tr->nh=NULL;
    tr->packptr=NULL;
  }
  return tr;
} /* transaction_new() */

/* [<][>][^][v][top][bottom][index][help] */