//**********************************************
// Filename:	AppFile.OGS
// Creation:	9/8/00 by SDB
// Copyright (c) OriginLab Corporation 1992-2000
// Purpose:	Import competitor data formats
//**********************************************
// Modification Log:
// SDB v6.1045 9/8/00, moved codes here from file.ogs
// LY  6/25/01, add ODAQ data import support
// SY  09/11/01 t9490 add SPC data import support
// GRD 2003.10.28 Remove window rename after import for SPDOS & SPWIN
************************************************/

//Only support SimgaPlot 2, rename this
//to 
//[ImportSigma]
//to enable the File:Import:SigmaPlot menu
[ImportSigma]
	run.section(File,Init);
	FDLOG.UseGroup(SigmaPlot);
	if (FDLOG.Open(A))
		return 1;
	%B=FDLOG.path$;
	switch(FDLOG.DefType)
	{
	case 1:
		run.section(ORDJNB,Main);
		break;
	case 2:
		run.section(,SPWIN);
		break;
	case 3:
		run.section(,SPDOS);
		break;
	};
	return 0;

[SPDOS]
	%M=%[%A,'.'];
	%N=%[%A,>'.'];
	repeat (wks.ncols) {delete %(%H,wks.ncols)};
	worksheet -a 2;
	wks.col1.name$=A;
	wks.col2.name$=B;
//	win -r %H %M;
	%M=%H;
//	%B=FDLOG.path$;
	zzid=file.open(%B%A,0);
	doc -uw;
	file.sigmados(%M,zzid);
	file.close();
	delete -v zzid;
	return 0;

[SPWIN]
	%M=%[%A,'.'];
	%N=%[%A,>'.'];
	repeat (wks.ncols) {delete %(%H,wks.ncols)};
	worksheet -a 2;
	wks.col1.name$=A;
	wks.col2.name$=B;
//	win -r %H %M;
	%M=%H;
//	%B=FDLOG.path$;
	zzid=file.open(%B%A,0);
	doc -uw;
	file.sigmawin(%M,zzid);
	file.close();
	delete -v zzid;
	return 0;


[ImportKaleid]
	if(run.LoadOC(OriginLab\kgraph.c) != 0)
	{
		type -b "kgraph.c not found";
		return 0;
	}
	run.section(File,Init);
	FDLOG.UseGroup(KaleidaGraph);
	if(FDLOG.Open(A))
		return 1;
	%B=FDLOG.path$;
	return readkgraph("%B%A",%H);


[OpenAppData]
	run.section(File,Init);
	FDLOG.UseGroup(KaleidaGraph);
	if(FDLOG.Open(A))
		return 1;

	%B=FDLOG.path$;
	if (System.Path.SavePath)
		System.Path.AppData$ = %B;
	return 0;

[Mathematica]
	run.section(File,Init);
	FDLOG.UseGroup(Mathematica);
	if(FDLOG.Open(A))
		return 1;
	%B=FDLOG.Path$;
	hFile=file.open(%B%A);
	doc -uw;
	err=file.mathematica(%H);
	if (err==1)
	{	
		switch (file.error)
		{
		case 39:
			//No worksheet
			type -b;
			type -b $General.NoWks;
			break;
		case 40:
			//No matrix
			type -b;
			type -b $General.NoMatrix;
			break;
		case 41:
			//Unsupported format
			type -b;
			type -b $General.BadFormat;
			break;
		}
	}
	file.close(hFile);
	return 0;

[WAV]
	run.section(File,Init);
	FDLOG.UseGroup(Sound);
	if(FDLOG.Open(A))
		return 1;
	%B=FDLOG.path$;
	if(run.LoadOC(OriginLab\ocWAV.c) != 0)
	{
		type -b "Origin C support program not found (ocWAV.c).";
		return 2;
	}
	nn = ocWAVReadFile(%B%A,%H);
	return nn;


[LabTech]
	run.section(File,Init);
	FDLOG.UseGroup(Labtech);
	if(FDLOG.Open(A))
		return 1;

	%B=FDLOG.path$;
	if(exist(%H)!=2)
	{
		win -t data origin;
	}
	fid=file.open(%B%A);
	fsize=file.size();
	if(fid)
	{
		%M=$binary.types;
		bintype=1;
		cols=1;
		getn ($binary.hdr1) bintype:M
		($binary.hdr2) cols ($binary.hdr3);
		if(cols>wks.ncols) worksheet -a $(cols-wks.ncols);
		thisrow=1;
		for(pos=file.getpos();pos<fsize;)
		{
			loop(ii,1,cols)
			{
				switch(bintype)
				{
				case 1: // Single float (4 bytes)
					nn=file.fread();
					col($(ii))[$(thisrow)]=nn;
					break;
				case 2: // Integer (2 bytes)
					nn=file.read(2);
					col($(ii))[$(thisrow)]=nn;
					break;
				}
			}
			pos=file.getpos();
			thisrow++;
		}
	}
	file.close();
	delete -v hfile;

	win -r %H %[%A,'.'];
	return 0;


[ODAQ]
	run.section(File,Init);
	FDLOG.UseGroup(ODAQ);
	if(FDLOG.Open(A))
		return 1;
	%B=FDLOG.path$;
	
	if(run.LoadOC(OriginLab\O_DAQ) != 0)
	{
		type -b "O_DAQ support Origin C program not found";
		return 0;
	}
	
	loadODAQ("%B%A", %H);

[ThermoGalacticSPC]
	run.section(File,Init);
	FDLOG.UseGroup(ThermoGalacticSPC);
	if(FDLOG.Open(A))
		return 1;
	%B=FDLOG.path$;
	
	open -s %B%A;
	return 0;

