/*
ReadLine.c
Author : Martin Hsu
This demo show how to implement read text file line by line in MiniOS7 system.
*/


#include "..\..\lib\VH2K.h"
#include <stdio.h>
#include "ini.h"
#include <string.h>

void main()
{

     int n,i,disk=0,c;
     char valStr[32];
     static FILE *fd;
     int fIndex=0;
     
     char diskStr[2][20]={"DiskA","DiskB"};
     
     char line[MAX_LINE_LENGTH],sTemp[20];
 	 int HighRamMode;
	 extern int bNeedDisableHighRam;
    
     InitLib();
    
     
start:	  
	  Print("Read Text file Demo...\n\r");
	  
	  Print("DiskA = 0\n");
	  Print("DiskB = 1:\n");
	  Print("Select the disk which store the text file ==>");
	  
	  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;
	  }
	     
     // check how many files in disk B
     n = GetFileNo_AB(disk);
     for(i=0;i<n;i++)
     {
	     Print("FILES Number: %d\r\n",i);
		 // to get file infomation 
	     fd = GetFileInfoByNo_AB(disk,i);
	     if(fd)
	     {
     		 if(disk==0)
	    	 {
				if(bNeedDisableHighRam) 
				{
					HighRamMode=GetHighRamMode();
					DisableHighRam();
				} 
			 }
	         // ReadLine function can only read  .txt or .ini or other text format file not binary file.
	         if(strcmp(fd->fname ,"txtfile.txt")==0)
	         {
	         	 Print("FILE: %s, Size: %d Time: %d.%d.%d %d:%d:%d\r\n",fd->fname, fd->size,fd->day,fd->month,fd->year, fd->hour,fd->minute,fd->sec);
		         fIndex=0;	         
		         while(fIndex<fd->size)
		         {
		         	fIndex=ReadLine(fd,line,fIndex);
		         	
		         	if(strlen(line)>0)
		         		Print("line next start point=%04d length=%03d, line= %s\n",fIndex,strlen(line),line);
		         	
		         }
	         }
			 if(bNeedDisableHighRam) 
			 {
					SetHighRam(HighRamMode);
			 }         
	     }
	     else
	     {
	         Print("FILE: %s\r\nGetInfo FAILED!\r\n",fd->fname);
	     }  
	}
 	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;
}

