/*
 ViewALS300data
 Copyright (C) 2010 Martin Štrympl
 
 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 3.0 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.
 
 Martin Štrympl
 xstrym00@stud.feec.vutbr.cz
 */
 
/* This code is copied from other Martin Štrympl's project under other licence. */

/*
 *  Array.c
 *  GerberConversion
 *
 *  Created by Martin Štrympl on 11/1/10.
 *  Copyright 2010 School. All rights reserved.
 *
 */

#include "Array.h"

struct sArray_TextArray
{
  char **at_texts;
  uint32_t u4_texts;
};

struct sArray_TextArray * Array_TextArray_Construct()
{
  struct sArray_TextArray *ps_array;
  
  ps_array = (struct sArray_TextArray *)malloc(sizeof(struct sArray_TextArray));
  
  if (ps_array)
  {
    ps_array->at_texts=NULL;
    ps_array->u4_texts=0;
  }
  
  return ps_array;
}
void Array_TextArray_Destruct(struct sArray_TextArray *ps_array)
{
  if (ps_array)
  {
    uint32_t u4_n;
    
    for (u4_n=0;u4_n<ps_array->u4_texts;u4_n++)
    {
      if (ps_array->at_texts[u4_n]) free(ps_array->at_texts[u4_n]);
    }
    
    if (ps_array->at_texts) free(ps_array->at_texts);
    
    free(ps_array);
  }
}

bool Array_TextArray_Add(struct sArray_TextArray *ps_array, const char *t_text)
{
  char *t_copy;
  uint32_t u4_len;
  
  if (t_text==NULL) return false;
  
  u4_len = strlen(t_text)+1;
  
  t_copy=(char *)malloc(u4_len);
  
  if (t_copy)
  {
    char **t_pam;
    
    t_pam=(char **)realloc(ps_array->at_texts, sizeof(char *)*(ps_array->u4_texts+1));
  
    if (t_pam)
    {
      memcpy(t_copy, t_text, u4_len);
      
      ps_array->at_texts=t_pam;
      
      ps_array->at_texts[ps_array->u4_texts]=t_copy;
      
      ps_array->u4_texts++;
      
      return true;
    }
    
    free(t_copy);
  }
  
  return false;
}
bool Array_TextArray_Set(struct sArray_TextArray *ps_array, uint32_t u4_index, const char *t_text);
const char * Array_TextArray_Get(struct sArray_TextArray *ps_array, uint32_t u4_index)
{
  if (u4_index<ps_array->u4_texts) return ps_array->at_texts[u4_index];
  
  return NULL;
}
char * Array_TextArray_GetCopy(struct sArray_TextArray *ps_array, uint32_t u4_index);
bool Array_TextArray_Remove(struct sArray_TextArray *ps_array, uint32_t u4_index);
uint32_t Array_TextArray_Texts(struct sArray_TextArray *ps_array)
{
  return ps_array->u4_texts;
}

