/***************************************************************************
                          get_information.c  -  description
                             -------------------
    begin                : vie ago 23 2002
    copyright            : (C) 2002 by deabru
    email                : deabru@nubbis.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#include <stdio.h>
#include <stdlib.h>

#define Kiocont 5 /* pos of que control bit */
#define Kioname 14/* when init the file cuted name */
#define Kfile_name_size 256 /* max file name size */
#define Ktamnum 30 /* max number size */

int get_information (char first_file_name[], char file_name[], char total_size[], char partial_size[], int *pos_init_file, char *control)
{
/* get all util information for reconstruct the cutted file */
    /* Local variables */
    char auxC;
/*    int auxI;*/
    int i = 0;
    int result = 5;
    FILE *first_file;

    /* init variables */
    *pos_init_file = 4;

    /* init function code */
    first_file=fopen(first_file_name,"r");
    if (first_file==NULL) /* check if file is really opened */
    {
	if (first_file_name==NULL)
	{
	    printf("\aError: file not opened, check if exist\n");
	}
	else
	{
	    printf("\aError: file \"%s\" not opened, check if exist\n",first_file_name);
	}
    }
    else /* if file is opened */
    {
	result--; /* check */

        /* get control bite */
	/* ignore ?'s */
	i=0;
	while (i<Kiocont && !feof(first_file))
	{
	    if ( fgetc(first_file) != '?' )
	    {
		printf("\aError: file \"%s\" opened not correct, control bit not found\n",first_file_name);
		fclose(first_file);
		return result;
	    }
	    i++;
	    (*pos_init_file)++;
	}
	/* 2 get bit */
	*control = fgetc(first_file); /* get control bit */

	/* ajust local variables */
	result--;
	i++;
	(*pos_init_file)++;

	/* get file name */
	/* 1 ignore ?'s */
	while (i<Kioname && !feof(first_file))
	{
	    auxC = fgetc(first_file);
	    if ( auxC != '?' && auxC != '\0')
	    {
		printf("\aError: file \"%s\" opened not correct, name file not found\n",first_file_name);
		fclose(first_file);
		return result;
	    }
	    i++;
	    (*pos_init_file)++;
	}

	i=0;
	/* 2 get file cutted name */
	do
	{
	    auxC = fgetc(first_file);
	    file_name[i] = auxC;
	    i++;
	    (*pos_init_file)++;
	} while ( auxC != '?' );
	file_name[i-1] = '\0';
	result--;

	/* get total size */
	/* 1 ignore ?'s */
	i=1;
	while (i<5 && !feof(first_file))
	{
	    auxC = fgetc(first_file);
	    if ( auxC != '?' && auxC != '\0')
	    {
		printf("\aError: file \"%s\" opened not correct, total size not found\n",first_file_name);
		fclose(first_file);
		return result;
	    }
	    i++;
	    (*pos_init_file)++;
	}

	i=0;
	/* 2 get total size file */
	do
	{
	    auxC = fgetc(first_file);
	    total_size[i] = auxC;
	    i++;
	    (*pos_init_file)++;
	} while ( auxC != '?' );
	total_size[i-1] = '\0';
	result--;

	/* get partial size */
	/* 1 ignore ?'s */
	i=1;
	while (i<5 && !feof(first_file))
	{
	    auxC = fgetc(first_file);
	    if ( auxC != '?' && auxC != '\0')
	    {
		printf("\aError: file \"%s\" opened not correct, partial size not found\n",first_file_name);
		fclose(first_file);
		return result;
	    }
	    i++;
	    (*pos_init_file)++;
	}

	i=0;
	/* 2 get partial size file */
	do
	{
	    auxC = fgetc(first_file);
	    partial_size[i] = auxC;
	    i++;
	    (*pos_init_file)++;
	} while ( auxC != '?' );
	partial_size[i-1] = '\0';
	result--;

	fclose(first_file);
    }

    return result;
}