/*
 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
 */
 
/*
 *  data.c
 *  ViewALS300data
 *
 *  Created by Martin Štrympl on 11/5/10.
 *  Copyright 2010 School. All rights reserved.
 *
 */

#include "data.h"

char *LoadData(const char *t_file)
{
  
  FILE *fi_file;
  
  fi_file=fopen(t_file, "rb");
  
  if (fi_file)
  {
    long i8_size;
    
    fseek(fi_file, 0, SEEK_END);
    
    i8_size=ftell(fi_file);
    
    rewind(fi_file);
    
    if (i8_size>0)
    {
      char *t_file;
      
      t_file=(char *)malloc(i8_size+1);
      
      if (t_file)
      {
        if (fread(t_file, 1, i8_size, fi_file)==i8_size)
        {
          
          fclose(fi_file);
          
          t_file[i8_size]=0;
          
          return t_file;
        }
        free(t_file);
      }
    }
    fclose(fi_file);
  }
  return NULL;
}

struct sArray_TextArray * DataToArray(char *t_text)
{
  struct sArray_TextArray *ps_array;
  
  ps_array=Array_TextArray_Construct();
  
  if (ps_array)
  {
    char *t_begin, *t_find;
    
    t_begin=t_find=t_text;
    
    while (*t_find)
    {
      if (*t_find=='\n')
      {
        *t_find=0;
        if (t_find[1]=='\r')
        {
          t_find++;
        }
        
        if (Array_TextArray_Add(ps_array, t_begin)==false)
        {
          Array_TextArray_Destruct(ps_array);
          return NULL;
        }
        
        t_begin=t_find+1;
      }
      else if (*t_find=='\r')
      {
        *t_find=0;
        if (t_find[1]=='\n')
        {
          t_find++;
        }
        
        if (Array_TextArray_Add(ps_array, t_begin)==false)
        {
          Array_TextArray_Destruct(ps_array);
          return NULL;
        }
        
        t_begin=t_find+1;
      }
      t_find++;
    }
  }
  
  return ps_array;
}

void ViewData(struct sArray_TextArray *ps_array, uint32_t u4_from, uint32_t u4_to)
{
  uint32_t u4_size, u4_n, u4_k;
  
  u4_n=0;
  u4_size=Array_TextArray_Texts(ps_array);  
  if (u4_size>=38)
  {
    printf("HEADER:\n");
     /*
      Put #un%, , LaserVersion$ + tm$
      Put #un%, , FileDescription$ + tm$
      Put #un%, , LasReferenceX$ + tm$
      Put #un%, , LasReferenceY$ + tm$
      Put #un%, , LasReferenceX1$ + tm$
     */
    printf("LaserVersion: '%s'\n", Array_TextArray_Get(ps_array, u4_n+0));
    printf("FileDescription: '%s'\n", Array_TextArray_Get(ps_array, u4_n+1));
    printf("LasReferenceX: '%s'\n", Array_TextArray_Get(ps_array, u4_n+2));
    printf("LasReferenceY: '%s'\n", Array_TextArray_Get(ps_array, u4_n+3));
    printf("LasReferenceX1: '%s'\n", Array_TextArray_Get(ps_array, u4_n+4));
     /*
      Put #un%, , LasReferenceY1$ + tm$
      Put #un%, , TabReferenceZ$ + tm$
      Put #un%, , TabReferenceW$ + tm$
      Put #un%, , TabStepZ$ + tm$
      Put #un%, , TabStepW$ + tm$
      */
    printf("LasReferenceY1: '%s'\n", Array_TextArray_Get(ps_array, u4_n+5));
    printf("TabReferenceZ: '%s'\n", Array_TextArray_Get(ps_array, u4_n+6));
    printf("TabReferenceW: '%s'\n", Array_TextArray_Get(ps_array, u4_n+7));
    printf("TabStepZ: '%s'\n", Array_TextArray_Get(ps_array, u4_n+8));
    printf("TabStepW: '%s'\n", Array_TextArray_Get(ps_array, u4_n+9));
      /*
      Put #un%, , NumStepZ$ + tm$
      Put #un%, , NumStepW$ + tm$
      Put #un%, , TrimExe$ + tm$
      Put #un%, , usepsi$ + tm$
      Put #un%, , RRef1FocusX + tm$
      */
    printf("NumStepZ: '%s'\n", Array_TextArray_Get(ps_array, u4_n+10));
    printf("NumStepW: '%s'\n", Array_TextArray_Get(ps_array, u4_n+11));
    printf("TrimExe: '%s'\n", Array_TextArray_Get(ps_array, u4_n+12));
    printf("usepsi: '%s'\n", Array_TextArray_Get(ps_array, u4_n+13));
    printf("RRef1FocusX: '%s'\n", Array_TextArray_Get(ps_array, u4_n+14));
      /*
      Put #un%, , RRef1FocusY + tm$
      Put #un%, , RRef1Ang + tm$
      Put #un%, , RRef2FocusX + tm$
      Put #un%, , RRef2FocusY + tm$
      Put #un%, , RRef2Ang + tm$
      */
    printf("RRef1FocusY: '%s'\n", Array_TextArray_Get(ps_array, u4_n+15));
    printf("RRef1Ang: '%s'\n", Array_TextArray_Get(ps_array, u4_n+16));
    printf("RRef2FocusX: '%s'\n", Array_TextArray_Get(ps_array, u4_n+17));
    printf("RRef2FocusY: '%s'\n", Array_TextArray_Get(ps_array, u4_n+18));
    printf("RRef2Ang: '%s'\n", Array_TextArray_Get(ps_array, u4_n+19));
      /*
      Put #un%, , RRef3FocusX + tm$
      Put #un%, , RRef3FocusY + tm$
      Put #un%, , RRef3Ang + tm$
      Put #un%, , RCalFocusPos + tm$
      Put #un%, , RCalFocusANG + tm$
      */
    printf("RRef3FocusX: '%s'\n", Array_TextArray_Get(ps_array, u4_n+20));
    printf("RRef3FocusY: '%s'\n", Array_TextArray_Get(ps_array, u4_n+21));
    printf("RRef3Ang: '%s'\n", Array_TextArray_Get(ps_array, u4_n+22));
    printf("RCalFocusPos: '%s'\n", Array_TextArray_Get(ps_array, u4_n+23));
    printf("RCalFocusANG: '%s'\n", Array_TextArray_Get(ps_array, u4_n+24));
      /*
      TmpSaveString = Str(MasterRefXProbeA)
      Put #un%, , TmpSaveString + tm$
      TmpSaveString = Str(MasterRefYProbeA)
      Put #un%, , TmpSaveString + tm$
      TmpSaveString = Str(MasterRefXProbeB)
      Put #un%, , TmpSaveString + tm$
      TmpSaveString = Str(MasterRefYProbeB)
      Put #un%, , TmpSaveString + tm$
      TmpSaveString = Str(CoordXFiducial1)
      Put #un%, , TmpSaveString + tm$
      */
    printf("MasterRefXProbeA: '%s'\n", Array_TextArray_Get(ps_array, u4_n+25));
    printf("MasterRefYProbeA: '%s'\n", Array_TextArray_Get(ps_array, u4_n+26));
    printf("MasterRefXProbeB: '%s'\n", Array_TextArray_Get(ps_array, u4_n+27));
    printf("MasterRefYProbeB: '%s'\n", Array_TextArray_Get(ps_array, u4_n+28));
    printf("CoordXFiducial1: '%s'\n", Array_TextArray_Get(ps_array, u4_n+29));
      /*
      TmpSaveString = Str(CoordYFiducial1)
      Put #un%, , TmpSaveString + tm$
      TmpSaveString = Str(CoordXFiducial2)
      Put #un%, , TmpSaveString + tm$
      TmpSaveString = Str(CoordYFiducial2)
      Put #un%, , TmpSaveString + tm$
      Put #un%, , strAsseStep + tm$
      Put #un%, , strAsseOffset2 + tm$
      */
    printf("CoordYFiducial1: '%s'\n", Array_TextArray_Get(ps_array, u4_n+30));
    printf("CoordXFiducial2: '%s'\n", Array_TextArray_Get(ps_array, u4_n+31));
    printf("CoordYFiducial2: '%s'\n", Array_TextArray_Get(ps_array, u4_n+32));
    printf("strAsseStep: '%s'\n", Array_TextArray_Get(ps_array, u4_n+33));
    printf("strAsseOffset2: '%s'\n", Array_TextArray_Get(ps_array, u4_n+34));
      /*
      Put #un%, , strNCut + tm$
      Put #un%, , VisionRefPt + tm$
     */
    printf("strNCut: '%s'\n", Array_TextArray_Get(ps_array, u4_n+35));
    printf("VisionRefPt: '%s'\n", Array_TextArray_Get(ps_array, u4_n+36));
  }
  u4_k=0;
  for (u4_n=37;(u4_n+39)<u4_size;u4_n+=40)
  {
    printf("\n\nRECORD %u:\n",++u4_k);
    
    /*
     'start x
     startx As String * 13
     'start y
     starty As String * 13
     'stop x
     stopx As String * 13
     'stop y
     stopy As String * 13
     'valore ohmico
     ohmvalue As String * 11
     */
    printf("startx: '%s'\n", Array_TextArray_Get(ps_array, u4_n+0));
    printf("starty: '%s'\n", Array_TextArray_Get(ps_array, u4_n+1));
    printf("stopx: '%s'\n", Array_TextArray_Get(ps_array, u4_n+2));
    printf("stopy: '%s'\n", Array_TextArray_Get(ps_array, u4_n+3));
    printf("ohmvalue: '%s'\n", Array_TextArray_Get(ps_array, u4_n+4));
     /*
     'velocita' taglio
     speed As String * 7
     'corrente
     current As String * 7
     'freq. laser
     freq As String * 7
     'scanner h
     scanh As String * 5
     'scanner l
     scanl As String * 5
     */
    printf("speed: '%s'\n", Array_TextArray_Get(ps_array, u4_n+5));
    printf("current: '%s'\n", Array_TextArray_Get(ps_array, u4_n+6));
    printf("freq: '%s'\n", Array_TextArray_Get(ps_array, u4_n+7));
    printf("scanh: '%s'\n", Array_TextArray_Get(ps_array, u4_n+8));
    printf("scanl: '%s'\n", Array_TextArray_Get(ps_array, u4_n+9));
     /*
     'scanner g
     scang As String * 5
     'slope
     slope As String * 3
     'tolleranza accettazione pretest
     tolaccept As String * 5
     'tolleranza taglio finale
     tolcut As String * 5
     'tipo di taglio
     cutType As String * 4
     */
    printf("scang: '%s'\n", Array_TextArray_Get(ps_array, u4_n+10));
    printf("slope: '%s'\n", Array_TextArray_Get(ps_array, u4_n+11));
    printf("tolaccept: '%s'\n", Array_TextArray_Get(ps_array, u4_n+12));
    printf("tolcut: '%s'\n", Array_TextArray_Get(ps_array, u4_n+13));
    printf("cutType: '%s'\n", Array_TextArray_Get(ps_array, u4_n+14));
     /*
     'direzione inizio taglio
     cutDir As String * 3
     'percentuale primo stop
     cut1St As String * 5
     'percentuale prestop
     cutPre As String * 5
     'valore in volt scatto funzionale
     voltvalue As String * 11
     'mark condition
     mark As String * 3
     */
    printf("cutDir: '%s'\n", Array_TextArray_Get(ps_array, u4_n+15));
    printf("cut1St: '%s'\n", Array_TextArray_Get(ps_array, u4_n+16));
    printf("cutPre: '%s'\n", Array_TextArray_Get(ps_array, u4_n+17));
    printf("voltvalue: '%s'\n", Array_TextArray_Get(ps_array, u4_n+18));
    printf("mark: '%s'\n", Array_TextArray_Get(ps_array, u4_n+19));
     /*
     'taglio funzionale
     func As String * 3
     'step taglio serp
     serpstep As String * 6
     'identificatore di res.
     reslabel As String * 12
     'se 1 usa visione
     vision As String * 3
     'i seguenti parametri vengono utilizzati per il taglio multiplo
     'start2 X
     start2X As String * 13
     */
    printf("func: '%s'\n", Array_TextArray_Get(ps_array, u4_n+20));
    printf("serpstep: '%s'\n", Array_TextArray_Get(ps_array, u4_n+21));
    printf("reslabel: '%s'\n", Array_TextArray_Get(ps_array, u4_n+22));
    printf("vision: '%s'\n", Array_TextArray_Get(ps_array, u4_n+23));
    printf("start2X: '%s'\n", Array_TextArray_Get(ps_array, u4_n+24));
     /*
     'start2Y
     start2Y As String * 13
     'stop2X
     stop2X As String * 13
     'stop2Y
     stop2Y As String * 13
     'N. Cut Multipli
     strNCutMultiple As String * 13
     'sviluppi futuri
     sparedata As String * 1816
     */
    printf("start2Y: '%s'\n", Array_TextArray_Get(ps_array, u4_n+25));
    printf("stop2X: '%s'\n", Array_TextArray_Get(ps_array, u4_n+26));
    printf("stop2Y: '%s'\n", Array_TextArray_Get(ps_array, u4_n+27));
    printf("strNCutMultiple: '%s'\n", Array_TextArray_Get(ps_array, u4_n+28));
    printf("sparedata: '%s'\n", Array_TextArray_Get(ps_array, u4_n+29));
     /*
     'Posizione X Probe A
     recProbeAX As String * 13
     'Posizione Y Probe A
     recProbeAY As String * 13
     'Posizione X Probe B
     recProbeBX As String * 13
     'Posizione Y Probe B
     recProbeBY As String * 13
     'Stato Filtro 1
     recFilter1 As String * 3
     */
    printf("recProbeAX: '%s'\n", Array_TextArray_Get(ps_array, u4_n+30));
    printf("recProbeAY: '%s'\n", Array_TextArray_Get(ps_array, u4_n+31));
    printf("recProbeBX: '%s'\n", Array_TextArray_Get(ps_array, u4_n+32));
    printf("recProbeBY: '%s'\n", Array_TextArray_Get(ps_array, u4_n+33));
    printf("recFilter1: '%s'\n", Array_TextArray_Get(ps_array, u4_n+34));
     /*
     'Stato Filtro 2
     recFilter2 As String * 3
     'Stato Filtro 3
     recFilter3 As String * 3
     'Stato Filtro 4
     recFilter4 As String * 3
     'Stato Filtro PSI
     recFilterPSI As String * 3
     'Abilitazione AutoFuoco
     AutoFEnable As String * 3
     */
    printf("recFilter2: '%s'\n", Array_TextArray_Get(ps_array, u4_n+35));
    printf("recFilter3: '%s'\n", Array_TextArray_Get(ps_array, u4_n+36));
    printf("recFilter4: '%s'\n", Array_TextArray_Get(ps_array, u4_n+37));
    printf("recFilterPSI: '%s'\n", Array_TextArray_Get(ps_array, u4_n+38));
    printf("AutoFEnable: '%s'\n", Array_TextArray_Get(ps_array, u4_n+39));
  }
}
