/******************************************************************
PluginFileConverter.c
Written by Erwin Timmerman

This is a sample file for a Spectrum Viewer plug-in file converter. 

This plug-in should be an exe-file which takes 2 arguments:
- The input file path (the data file to convert)
- The output file path (the intermediate .svf file which will actually be read by SV)

SV will provide these paths, and will delete the temp .svf output file 
after reading it, thus making it appear like it had read the input file to
begin with.


*******************************************************************/

#define MAX_PATHNAME_LEN 260
#define BYTE char

#include "SVF.h"


// the used functions of this file
int ReadInputFile (char *Path);
int WriteTempOutputFile (char *Path);


// the global variables for the data sets
int   NrOfDataSets, NrOfElements;
float **XValues=0, **YValues=0; // the arrays where your data is stored
char  InputPath[MAX_PATHNAME_LEN] = "", 
	  XAxisName[256]="", YAxisName[256]="";


int main (int argc, char *argv[])
{
	if (argc != 3) return -1;

	strcpy(InputPath, argv[1]);
	
	ReadInputFile (argv[1]);
	WriteTempOutputFile (argv[2]);

	free(XValues); free(YValues);
					
	return 0;
}

int ReadInputFile (char *Path)
{
	FILE *FilePtr=0;
	int i, j;

	FilePtr = fopen(Path, "r");
	if (!FilePtr) return 0;

	// do your file reading and data extraction here
	// I've done some dummy declarations
	NrOfDataSets = 3;
	NrOfElements = 50;

	// reserve memory to hold the data values
	XValues = malloc(NrOfDataSets * NrOfElements * sizeof(float));
	YValues = malloc(NrOfDataSets * NrOfElements * sizeof(float));
	for (i=0; i<NrOfDataSets; i++) {
		XValues[i] = malloc(NrOfElements * sizeof(float));
		YValues[i] = malloc(NrOfElements * sizeof(float));
	}
	
	// fill the data values, with the values from the input file.
	// I'm doing dummy assignments here:
	for (i=0; i<NrOfDataSets; i++) {
		for (j=0; j<NrOfElements; j++) {
			XValues[i][j] = j;		// X will run from 0 to 199
			YValues[i][j] = (i+1)*j*j; // + rand(); // Y will be a random value (noise)
		}
	}

	// Make other assignments, for example axis names
	strcpy(XAxisName, "MyXAxis (arms)");
	strcpy(YAxisName, "MyYAxis (legs)");

	// Reading of input file done, close it and return
	fclose(FilePtr);
	
	return 1;
}

int WriteTempOutputFile (char *Path)
{
	SVF_DATASET_PARAMS *DS = 0;
	int i;

	// allocate Spectrum Viewer File data sets to store the data in
	DS = malloc(NrOfDataSets * sizeof(SVF_DATASET_PARAMS));

	// now convert your own read data to the SVF dataset(s)
	for (i=0; i<NrOfDataSets; i++) {

		// assign the X values and Y values
		DS[i].NrOfElements = NrOfElements;
		DS[i].X = XValues[i];
		DS[i].Y = YValues[i];
		// Z data is not supported in SV (yet)
		DS[i].Z = 0;

		// assign the data set names (the names that will appear on the name panel)
		strcpy(DS[i].DSName, "Plug-in test"); 
		if (NrOfDataSets > 1) {
			char TempString[16];
			sprintf(TempString, " (%i)", i+1);
			strcat(DS[i].DSName, TempString);
		}

		// assign the graph title and axis names for each individual data set, when needed
		// When not needed, assign zero (do not forget this!)
		DS[i].GraphTitle = 0; 

		DS[i].XName = malloc((strlen(XAxisName) + 1) * sizeof(char));
		strcpy (DS[i].XName, XAxisName);
		
		DS[i].YName = malloc((strlen(YAxisName) + 1) * sizeof(char));
		strcpy (DS[i].YName, YAxisName);
		
		DS[i].ZName = 0;

		// create an additional text block with extra parameter data
		// right-click on a data set name in SV to see this text block
		if (0) DS[i].DSInfo = 0;
		else {
			char DSInfo[1024];
			sprintf(DSInfo, "Data set no. %i\nMeasured by M. Yself", i+1);

			DS[i].DSInfo = malloc((strlen(DSInfo) + 1) * sizeof(char));
			strcpy (DS[i].DSInfo, DSInfo);
		}

		// set the style of the data set
		DS[i].Selected = 0;
		DS[i].Hidden = 0;
		DS[i].RightYaxis = 0;

		// Disable optional raw data block
		DS[i].DataBlock = 0;
		DS[i].NrOfBytes = 0;

		// Assign the data set file pointer.
		// This string points to the file SV *thinks* your data came from.
		// In this case, make it point to your original input data file.
		strcpy(DS[i].DataPath, InputPath);
	}
			
	WriteSVFFile(Path, KEEP_PATHS, NO_ASK_OVERWRITE, 0, 0, 0, 0, 0, NrOfDataSets, DS);
	FreeSVFDSet(&DS, NrOfDataSets, 1, 1, 0, 1, 1, 1, 0, 1, 0);
	
	return 1;
}