9511_project03

project 3 for algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README LICENSE
source/user.c (1875B)
   1 #include "../include/user.h"
   2 
   3 status_t user_create(user_t **user)
   4 {
   5     if((*user = (user_t *)malloc(sizeof(user_t))) == NULL)
   6         return ERROR_MEMORY;
   7 
   8     (*user)->id = 0;
   9     (*user)->c = 0;
  10     (*user)->d = 0;
  11 
  12     return OK;
  13 }
  14 
  15 status_t user_destroy(user_t **user)
  16 {
  17     if(user == NULL) return ERROR_NULL_POINTER;
  18 
  19     free(*user);
  20     *user = NULL;
  21 
  22     return OK;
  23 }
  24 
  25 status_t user_set_data(user_t *user, ulong id, ulong c, ulong d) 
  26 {
  27     if(user == NULL) return ERROR_NULL_POINTER;
  28 
  29     user->id = id;
  30     user->c = c;
  31     user->d = d;
  32 
  33     return OK;
  34 }
  35 
  36 int user_equals(const void *a, const void *b)
  37 {
  38     user_t *A, *B;
  39 
  40     if(a == NULL || b == NULL) return ERROR_NULL_POINTER;
  41 
  42     A = (user_t *)a;
  43     B = (user_t *)b;
  44 
  45     if(A->id == B->id) return 1;
  46 
  47     return 0;
  48 }
  49 
  50 status_t user_print_as_csv(const void *u, FILE *fp)
  51 {
  52     if(u == NULL || fp == NULL) return ERROR_NULL_POINTER;
  53 
  54     user_t *user = (user_t *)u;
  55 
  56     fprintf(fp, "%ld%s%ld%s%ld\n", user->id, CSV_OUT_FILE_DELIM, user->c, CSV_OUT_FILE_DELIM, user->d);
  57 
  58     return OK;
  59 }
  60 
  61 status_t user_print_as_xml(const void *u, FILE *fp)
  62 {
  63     if(u == NULL || fp == NULL) return ERROR_NULL_POINTER;
  64 
  65     user_t *user = (user_t *)u;
  66 
  67     fprintf(fp, "\t\t<%s>%ld</%s>\n", XML_STR_ID, user->id, XML_STR_ID);
  68     fprintf(fp, "\t\t<%s>%ld</%s>\n", XML_STR_CREDIT, user->c, XML_STR_CREDIT);
  69     fprintf(fp, "\t\t<%s>%ld</%s>\n", XML_STR_DEBIT, user->d, XML_STR_DEBIT);
  70 
  71     return OK;
  72 }
  73 
  74 int user_comparator_credits_minmax(const void *a, const void *b)
  75 {
  76     user_t *A, *B;
  77 
  78     if(a == NULL || b == NULL) return ERROR_NULL_POINTER;
  79 
  80     A = *(user_t **)a;
  81     B = *(user_t **)b;
  82 
  83     if(A->c > B->c) return 1;
  84     else if(A->c == B->c) return 0;
  85     return -1;
  86 }  
  87 
  88 int user_comparator_credits_maxmin(const void *a, const void *b)
  89 {
  90     user_t *A, *B;
  91 
  92     if(a == NULL || b == NULL) return ERROR_NULL_POINTER;
  93 
  94     A = *(user_t **)a;
  95     B = *(user_t **)b;
  96 
  97     if(A->c < B->c) return 1;
  98     else if(A->c == B->c) return 0;
  99     return -1;
 100 }