/***************************************************************************
                          hacha_speed_split.c  -  description
                             -------------------
    begin                : lun sep 2 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>
#include <string.h>
#include <math.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 hacha_speed_split (char file_name[], char master_part_name[], double total_size, double partial_size)
{
/* split file fisrt_file_name (size: total_size & partial_size in byttes)  patron parts */
	/* returns 0 all correct
                    1 incorrect size any part
                    2 can't open file to write
                    3 can't open first_file
                    4 can't open any part
 */

    double size_file (char[]);
    int sizetopart(double,double);
    void itoa (int,char[],int);
    void iftoa (double ,char []);
    char control_information ( char[], char[], char[]);
 
	int result = 0;
    int NumParts;
    FILE *current_file;
    FILE *real_file;
    double total,partial;
/*	double auxF;*/
  /*  char patron_name[Kfile_name_size];*/
/*    int leng_patron;*/
    char current[Kfile_name_size];
    char auxS[Kfile_name_size];
    char auxS2[Kfile_name_size];
    int correct = 1;
/*    int auxI,i=0;*/
   char control_byte;
    int count_file = 0;

    int *buffer;

	buffer = (int *)calloc(partial_size,sizeof(int));
    
    /* ajust total_size if is need */
    if ( total_size < partial_size) partial_size = total_size;

    /* calculate number parts */
    NumParts = sizetopart(total_size,partial_size);

    /* calculate file size */
    total = total_size;
   partial = partial_size;

   /* GO! */

   strcpy(current,master_part_name);
	strcat(current,".0");

    current_file=fopen(current,"w");
    if (current_file == NULL) /* check if is opened */
    {
			result = 3;
			if (master_part_name==NULL)
			{
				printf("\aError: file not opened, check file name\n");
			}
			else
			{
				printf("\aError: file \"%s\" can't be opened, check permision\n",current);
			}
	}
	else /* file is opened */
    {
			/* put aditional info in first file :D */
			iftoa(total_size,auxS);
			iftoa(partial_size,auxS2);
			control_byte = control_information(file_name,auxS,auxS2);
			fprintf(current_file,"?????%c",control_byte);
			fputc(0,current_file);
			fputc(0,current_file);
			fputc(0,current_file);
			fprintf(current_file,"?????%s?????%s?????%s?????",file_name,auxS,auxS2);

			fclose(current_file);

			real_file=fopen(file_name,"r");
			if (real_file == NULL)
			{
				result = 2;
				if (file_name==NULL)
				{
					printf("\aError: file to split cannot be opened, check filename\n");
				}
				else
				{
					printf("\aError: file to split \"%s\" file cannot be opened, check if exist\n",file_name);
				}
			}
			else
			{

				strcpy(current,master_part_name);
				itoa(count_file,auxS,10);
				strcat(current,".");
				strcat(current,auxS); /* generate file name */

				current_file = fopen (current,"a");
				if (current_file == NULL)
				{
					printf("\a\nError: file \"%s\" can't be opened, check permision or free space\n",current);
					result = 1;
				}
				else
				{
					printf("Creating file: %s",current);
					partial = partial_size;

			/*		bytte = fgetc(real_file);
					while(!feof(real_file) && partial >0)
					{
						total--;
						partial--;
						fputc(bytte,current_file);
						bytte = fgetc(real_file);
					}*/
					fread(buffer,1,partial_size,real_file);
					fwrite(buffer,1,partial_size,current_file);
					total = total - partial_size;


					fclose(current_file);
					if (partial_size != (size_file(current) - (control_byte  +9)))
					{
						result = 4;
						printf("\a\nError: file \"%s\" incorrect size (%.0lf)(%.0lf), stop joined\n",current,partial_size,(size_file(current) - (control_byte  + 17)));
					}
					else
					{
						strcpy(current,master_part_name);
						count_file++;
						printf(" - OK (%.2lf\%)\n",100-(total/total_size)*100);

						while( (count_file < (NumParts - 1)) && correct )
						{
							itoa(count_file,auxS,10);
							strcat(current,".");
							strcat(current,auxS);
							current_file=fopen(current,"wb");
							if(current_file == NULL)
							{
								printf("\a\nError: file \"%s\" can't be opened, check permision or free space\n",current);
								correct = 0;
								result = 1;
							}
							else
							{
								printf("Creating file: %s",current);

							/*	partial = partial_size;

								fputc(bytte,current_file);
								partial--;
								total--;
								bytte = fgetc(real_file);
								while(!feof(real_file) && partial >0)
								{
									total--;
									partial--;
									fputc(bytte,current_file);
									bytte = fgetc(real_file);
								}*/
								fread(buffer,1,partial_size,real_file);
								fwrite(buffer,1,partial_size,current_file);
								total = total - partial_size;


								fclose(current_file);
								if (partial_size == size_file(current))
								{
									strcpy(current,master_part_name);
									count_file++;
									printf(" - OK (%.2lf\%)\n",100-(total/total_size)*100);
								}
								else
								{
									result = 1;
									correct = 0;
									printf("\a\nError: file \"%s\" incorrect size, stop splited\n",current);
								}
							}
						}

						if (correct && (count_file < NumParts ))
						{
							itoa(count_file,auxS,10);
							strcat(current,".");
							strcat(current,auxS);
							current_file=fopen(current,"wb");
							if(current_file == NULL)
							{
								printf("\a\nError: file \"%s\" can't be opened, check permision or free space\n",current);
								result = 1;
							}
							else
							{
								printf("Creating file: %s",current);

							/*	partial = partial_size;

								fputc(bytte,current_file);
								partial--;
								total--;
								bytte = fgetc(real_file);
								while(!feof(real_file) && partial >0)
								{
									total--;
									partial--;
									fputc(bytte,current_file);
									bytte = fgetc(real_file);
								}*/
								fread(buffer,1,total,real_file);
								fwrite(buffer,1,total,current_file);
								


								fclose(current_file);
								if (total == size_file(current))
								{
						/*			total = total - partial_size;
									count_file++;
									printf(" - OK (%.2lf\%)\n",100-(total/total_size)*100);*/
									printf(" - OK (%.2lf\%)\n",(float)100);
								}
								else
								{
									result = 1;
									printf("\a\nError: file \"%s\" incorrect size, stop splited\n",current);
								}
							}


							
						}
					}
				}
				fclose(real_file);
			}
		}

		return result;
}