parser_tecgra_com/mainFORM.cpp
2021-09-01 19:46:41 +02:00

307 lines
8.7 KiB
C++

//---------------------------------------------------------------------------
#include <stdio.h>
#include <vcl.h>
#pragma hdrstop
#include "mainFORM.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Panel1->Height = btVerDetalles->Down ? 66 : 32;
loadFiles();
filelist->ItemIndex=0;
doFilterClick(0);
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::loadFiles(void)
{
TSearchRec sr;
int iAttributes = 0;
AnsiString filename;
iAttributes |= faReadOnly;
iAttributes |= faArchive;
iAttributes |= faAnyFile;
filelist->Items->Clear();
filename="*.js";
if (FindFirst(filename, iAttributes, sr) == 0)
{
do
{
if ((sr.Attr & iAttributes) == sr.Attr)
{
filelist->Items->Add(sr.Name);
}
} while (FindNext(sr) == 0);
FindClose(sr);
}
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::emptyLine( char *line )
{
char *p;
// TAB, SPACE, NEW LINE, RETURN, NULL
p = line;
while ( *p!=NULL && (*p=='\t' || *p==' ' || *p=='\n' || *p=='\r') ) p++;
return (*p==NULL);
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::getPRE(char *line, char *pre)
{
char *p;
*pre = NULL;
p = line;
while ( *p!=NULL && *p!=' ' )p++; if ( *p!=NULL ) p++;
while ( *p!=NULL && *p!=' ' && *p!='_' ) *(pre++)=*(p++); *pre=NULL;
return (*pre!=NULL);
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::getHEAD( char *line, int *width, char *text )
{
char *p, *t;
p = line;
*width = 0;
*text =NULL;
t = text;
while ( *p!=NULL && *p!='(' )p++; if ( *p!=NULL ) p++;
while ( *p!=NULL && *p!=',' )
{
if(*p!=' ') *width = *width*10 + ((*p)-'0');
p++;
}
if ( width>0 )
{
while ( *p!=NULL && *p!='"' )p++; if ( *p!=NULL ) p++;
while ( *p!=NULL && *p!='"' ) *(t++)=*(p++); *t=NULL;
}
return (*text!=NULL);
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::getFIXED_ROWS( char *line, int *fr )
{
char *p;
p = line;
*fr = 0;
while ( *p!=NULL && *p!='=' )p++; if ( *p!=NULL ) p++;
while ( *p!=NULL && *p!=';' ) {if(*p!=' ') *fr = *fr*10+(*p-'0'); p++; }
return *fr>0;
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::getANDsetDATA( char *line )
{
int col;
bool dev = false;
char *p, *pb;
p = line;
char buffer[80];
col = 0;
while ( *p!=NULL && *p!='(' )p++;
if ( *p=='(' )
{
if ( *p!=NULL ) p++;
if ( !emptyLine( p ) )
{
sg->RowCount++;
while ( *p!=NULL )
{
pb = buffer;
*buffer = NULL;
while ( *p!=NULL && *p!='"' ) p++; if ( *p!=NULL ) p++;
while ( *p!=NULL && *p!='"' ) *(pb++)=*(p++); *pb=NULL; if ( *p!=NULL ) p++;
if ( *buffer!=NULL )
{
dev = true;
sg->Cells[col][(sg->RowCount-1)]=AnsiString(buffer);
col++;
}
}
if ( !dev ) sg->RowCount--;
}
}
return dev;
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::readline(FILE *f,char *line)
{
fgets( line, 254, f );
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::loadFile(AnsiString filename)
{
bool dev;
FILE *f;
char line[255], text[80];
int width;
int status;
int currROW=1, currCOL=0;
sg->ColCount = 1;
sg->RowCount = 1;
#define READ_VAR_PREFIX 1
#define READ_COLUMNS 2
#define READ_FIXED_ROW 3
#define READ_DATA 4
status = READ_VAR_PREFIX;
dev = false;
if ( (f=fopen(filename.c_str(),"r"))!=NULL )
{
dev = true;
while (!feof(f))
{
readline(f,line);
if ( !emptyLine( line ) )
switch ( status )
{
case READ_VAR_PREFIX:
getPRE( line, varPRE );
status = READ_COLUMNS;
break;
case READ_COLUMNS:
if ( getHEAD( line, &width, text ) )
{
currCOL++;
sg->ColCount = currCOL;
sg->ColWidths[currCOL-1] = width;
sg->Cells[currCOL-1][currROW-1] = text;
} else {
status=READ_FIXED_ROW;
}
break;
case READ_FIXED_ROW:
getFIXED_ROWS( line, &(sg->FixedCols) );
status = READ_DATA;
break;
case READ_DATA:
if ( getANDsetDATA( line ) )
sg->FixedRows = 1;
break;
} // esac
} // elihw
} // fi
fclose(f);
if ( dev )
{
sg->Row = (sg->RowCount>0) ? 1 : 0;
sg->Col = sg->FixedCols;
}
return dev;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btPostClick(TObject *Sender)
{
DeleteFile( filelist->Items->Strings[filelist->ItemIndex]+".bak" );
RenameFile( filelist->Items->Strings[filelist->ItemIndex], filelist->Items->Strings[filelist->ItemIndex]+".bak" );
// Devolvemos la tabla al fichero...
saveFILE( filelist->Items->Strings[filelist->ItemIndex] );
ShowMessage("Archivo guardado!");
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::saveFILE( AnsiString filename )
{
bool dev = false;
int i,j;
FILE *f;
if ( (f=fopen( filename.c_str(), "w" ))!=NULL )
{
dev = true;
// Cabeceras de columna
fprintf(f,"var %s_col = new Array(\n",varPRE);
for (i=0;i<(sg->ColCount-1);i++)
fprintf(f,"new Array(%d,\"%s\"),\n",sg->ColWidths[i],sg->Cells[i][0].c_str());
fprintf(f,"new Array(%d,\"%s\")\n",sg->ColWidths[i],sg->Cells[i][0].c_str());
fprintf(f,");\n");
fprintf(f,"\n");
fprintf(f,"var %s_cola = %d;\n",varPRE,sg->FixedCols);
fprintf(f,"\n");
fprintf(f,"var %s_dat = new Array(\n",varPRE);
for (j=1;j<(sg->RowCount-1);j++)
{
fprintf(f,"new Array(" );
for (i=0;i<(sg->ColCount-1);i++)
fprintf(f," \"%s\", ",sg->Cells[i][j].c_str() );
fprintf(f," \"%s\" ),\n",sg->Cells[i][j].c_str() );
}
fprintf(f,"new Array(" );
for (i=0;i<(sg->ColCount-1);i++)
fprintf(f," \"%s\", ",sg->Cells[i][j].c_str() );
fprintf(f," \"%s\" )\n",sg->Cells[i][j].c_str() );
fprintf(f,");");
}
fclose(f);
return dev;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btVerDetallesClick(TObject *Sender)
{
Panel1->Height = btVerDetalles->Down ? 66 : 32;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
if ( MessageDlg("Aplicar cambio: "+tipoCambio->Items->Strings[tipoCambio->ItemIndex]+
"("+Edit1->Text+")"+
"\nen la columna: "+columnaCambio->Items->Strings[columnaCambio->ItemIndex],
mtConfirmation, TMsgDlgButtons() << mbYes << mbNo, 0) == mrYes )
{
int col, i;
float inc;
inc = Edit1->Text.ToDouble();
col = (int)columnaCambio->Items->Objects[columnaCambio->ItemIndex];
for (i=1;i<sg->RowCount;i++)
{
try {
if ( tipoCambio->ItemIndex==0 )
{
// lineal
sg->Cells[col][i] = AnsiString( (sg->Cells[col][i].ToDouble() + inc ) );
} else {
// porcentual
sg->Cells[col][i] = AnsiString( (int)(sg->Cells[col][i].ToDouble() * (1+inc/100) ) );
}
} catch(...) {
//nothing;
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::doFilterClick(TObject *Sender)
{
loadFile( filelist->Items->Strings[filelist->ItemIndex] );
columnaCambio->Items->Clear();
for (int i=(sg->FixedCols);i<sg->ColCount;i++)
columnaCambio->Items->AddObject(sg->Cells[i][0],(TObject *)i);
columnaCambio->ItemIndex = 0;
}
//---------------------------------------------------------------------------