9511_project03

project 3 for algorithms & programming I (9511) prof. Cardozo
Index Commits Files Refs README LICENSE
source/utils.c (2178B)
   1 #include "../include/utils.h"
   2 
   3 status_t string_split(char *s, char **data, char *delim)
   4 {
   5     char *p, *tmp;
   6     size_t fields = 0;
   7 
   8     if(s == NULL || data == NULL || delim == NULL)
   9         return ERROR_NULL_POINTER;
  10 
  11     for(p = s; (tmp = strtok(p, delim)); p = NULL)
  12         strcpy(data[fields++], tmp);
  13 
  14     if(fields != IN_FILE_FIELDS)
  15         return ERROR_CORRUPT_DATA;
  16 
  17     return OK;
  18 }
  19 
  20 void clean_buffer(char *buf)
  21 {
  22     size_t i;
  23 
  24     for(i = 0; i < IN_FILE_MAX_LEN; i++)
  25         buf[i] = '\0';
  26 }
  27 
  28 void clean_array(char **data)
  29 {
  30     size_t i, j;
  31 
  32     for(i = 0; i < IN_FILE_FIELDS; i++) {
  33         for(j = 0; j < IN_FILE_FIELDS_MAX_LEN; j++)
  34             data[i][j] = '\0';
  35     }
  36 }
  37 
  38 status_t create_2darray(char ***arr, size_t r, size_t c) {
  39     size_t i;
  40 
  41     if(((*arr) = malloc(sizeof(char *) * r)) == NULL)
  42         return ERROR_MEMORY;
  43 
  44     for(i = 0; i < r; i++) {
  45         if(((*arr)[i] = calloc(sizeof(char), c)) == NULL) {
  46             destroy_2darray((*arr), c);
  47             return ERROR_MEMORY;
  48         }
  49     }
  50 
  51     return OK;
  52 }
  53 
  54 status_t destroy_2darray(char **arr, size_t r)
  55 {
  56     size_t i;
  57 
  58     if(arr == NULL) return ERROR_NULL_POINTER;
  59 
  60     for(i = 0; i < r; i++) {
  61         free(arr[i]);
  62         arr[i] = NULL;
  63     }
  64 
  65     free(arr);
  66     return OK;
  67 }
  68 
  69 status_t get_date(time_t *e, char *str)
  70 {
  71     struct tm tm;
  72 
  73     if(e == NULL || str == NULL) return ERROR_NULL_POINTER;
  74 
  75     /* Desactiva el horario de verano */
  76     tm.tm_isdst = 0;
  77 
  78     switch(str[2]) {
  79         case '/': 
  80             strptime(str, "%d/%m/%Y %H:%M:%S", &tm);
  81             *e = mktime(&tm);
  82             break;
  83         case '-':
  84             strptime(str, "%d-%m-%Y %H:%M:%S", &tm);
  85             *e = mktime(&tm);
  86             break;
  87         case '.':
  88             strptime(str, "%d.%m.%Y %H:%M:%S", &tm);
  89             *e = mktime(&tm);
  90             break;
  91 
  92         default: return ERROR_CORRUPT_DATA; break;
  93     }
  94     return OK;
  95 }
  96 
  97 bool is_valid_card(char *card_no)
  98 {
  99     size_t i, j, k;
 100     int arr[4][4], sum;
 101     char tmp[2];
 102 
 103     if(card_no == NULL) return false;
 104     if(strlen(card_no) != CARD_NO_VALID_LEN) return false;
 105 
 106     for(i = 0, k = 0, sum = 0; i < 4; i++) {
 107         for(j = 0; j < 4; j++) {
 108             arr[i][j] = (card_no[k++] - '0');
 109             if(j % 2) {
 110                 arr[i][j] *= 2;
 111                 sprintf(tmp, "%d", arr[i][j]);
 112                 if(!(strlen(tmp) - 1)) arr[i][j] = (tmp[0] - '0');
 113                 else arr[i][j] = ((tmp[0] - '0') + (tmp[1] - '0'));
 114 
 115             }
 116             sum += arr[i][j];
 117         }
 118     }
 119 
 120     return (sum % 10) ? false : true;
 121 }