/*
ReadLine.c
Author : Martin Hsu
This demo show how to implement INI file in MiniOS7 system.

*/


#include "..\..\lib\vp2k.h"
#include <stdio.h>
#include "ini.h"


void main()
{
     int n,i,disk=0;
     char valStr[32];
     static FILE *fd;
     int fIndex=0;
     int iVal=0,c;
     long lVal=0;
     float fVal=0.0;
     int ret=0;
     char diskStr[2][20]={"DiskA","DiskB"};
     
     char line[MAX_LINE_LENGTH],sTemp[20];
     
 	 int HighRamMode;
	 extern int bNeedDisableHighRam;
    
      InitLib();
start:	  
	  Print("\n\nRead INI Demo...\n\r");
	  
	  Print("Select the disk which store the config.ini\n");
	  Print("\t DiskA = 0\n");
	  Print("\t DiskB = 1: ");
	  LineInput(sTemp,10);
	  sscanf(sTemp,"%d", & disk);
	  
	  if(disk >1 || disk <0)
	  {
	  	Print("Select wrong disk, it must be 0 or 1\n");
	  	goto start;
	  }
	  
	  // Demo for string data
	  ret = GetINIString("AAAA","C001","String not found", valStr,32,disk,"config.ini");
	  if(!ret )
	  {
	  	Print("Can not find config.ini in disk %s\n", diskStr[disk]);
	  }
	  else
	  {
	  	Print("C001 get string data = %s \n",valStr);
	  }	  
	  
	  // Demo for integer data
	  ret = GetINIString("AAAA","C002","10", valStr,32,disk,"config.ini");
	  if(!ret )
	  {
	  	Print("Can not find config.ini in disk %s\n", diskStr[disk]);
	  }
	  else
	  {
		  // convert decimal format string to integer data
		  sscanf(valStr,"%d", &iVal);
		  Print("C002 = %s Convert to decimal int data=%d\n",valStr,iVal);
	  }
	  
	  // Demo for long data
	  ret = GetINIString("AAAA","C003","10000", valStr,32,disk,"config.ini");
	  if(!ret )
	  {
	  	Print("Can not find config.ini in disk %s\n", diskStr[disk]);
	  }
	  else
	  {
		  // convert decimal format string to long data
		  sscanf(valStr,"%ld", &lVal);	  
		  Print("C003 = %s Convert to long decimal data =%ld\n",valStr,lVal);
	  }
	  
	  // Demo for long data
	  ret = GetINIString("AAAA","C004","10000", valStr,32,disk,"config.ini");
	  if(!ret )
	  {
	  	Print("Can not find config.ini in disk %s\n", diskStr[disk]);
	  }
	  else
	  {
		  // convert hex format string to long data
		  sscanf(valStr,"%lx", &lVal);
		  Print("C004 = %s , Convert to long hex data= %lx\n",valStr,lVal);	  
	  }
	  // Demo for float data
	  ret = GetINIString("AAAA","C005","0.5", valStr,32,disk,"config.ini");
	  if(!ret )
	  {
	  	Print("Can not find config.ini in disk %s\n", diskStr[disk]);
	  }
	  else
	  {
		  // convert float string to float data
		  sscanf(valStr,"%f",&fVal);
		  Print("C005 = %s , Convert float data= %f\n",valStr, fVal);
	  }
	    
	  // Demo for tring to read comment which is marked with ';'
	  // the result will give input default string "0000"
	  ret = GetINIString("AAAA","C006","0000", valStr,32,disk,"config.ini");
	  if(!ret )
	  {
	  	Print("Can not find config.ini in disk %s\n", diskStr[disk]);
	  }
	  else
	  {
	  	Print("C006 can not find data after ';' = %s \n",valStr);
	  }
	  	 
	  // Demo for tring to find a key which is not exist
	  // the result will give input default string "0000"
	  ret = GetINIString("AAAA","C007","0000", valStr,32,disk,"config.ini");
	  if(!ret )
	  {
	  	Print("Can not find config.ini in disk %s\n", diskStr[disk]);
	  }
	  else
	  {
	  	Print("C007 try to find a non-exist key = %s \n",valStr);	
	  } 
	  
	  Print("Press any key to retry this demo , 'q' or 'Q' to quit this demo !\r");
	  c=Getch(); 
	  if(c=='q' || c=='Q') 	            	
		return;
	  else
	  	goto start;
}

