/*
	XDemo28: Reads information from text files(basic)

	Compiler: BC++ 3.1
	Compile mode: large
	Project: user.c
		 v7000.c
		 vModbus.c			[after vcom3002.lib]
		 ..\Lib\7188EL.Lib
		 ..\Lib\TCPIPL.Lib
		 ..\Lib\VcomNNNN.Lib, with NNNN being the lib file's version.

		191 -> Reads data from the 1st * in the text file(config.dat).
		192 -> Reads data from the 2st * in the text file(config.dat).
		193 -> Reads data from the 3th * in the text file(config.dat).
                              
		In many applications, users need one text file to record some information.
		And the program can read it.
		You can use FSeek to get specific information in text files. 

		The format of the text file (config.ini) must follow the rules:
		1. The item information you want to read must be positioned after '*'.
		2. The comment cann't contain any '*'.
		3. KeyName and '=' must be seperated by least one space character.
		4. If the setting of KeyName is string type, you must add t
		   least one space character to the end of file(EOF).

		For example:
		=================================================
		config file sample for Xdemo28
		=================================================
		Star Item1  Item2  Item3  Item4   Comment        
		=================================================
		*    1st    19200  on     1.25    ;comment1      
		*    2nd    9600   off    -1.3	 ;comment2      
		*    3th    2400   off    19.34   ;comment3      
		= end of settings =                        		 
		=================================================
		comment...........                               
    
		Hardware: 7188E
     
		Note: to run this demo, you must load one extra text file
		(I wrote config28.ini for this demo) into 7188E.

		Refer 7188e\TCP\Doc\[Big5|Eng|Gb2312]\Vxcomm.htm 
			  7188e\TCP\Xserver\Xserver.htm
			  7188e\TCP\Xserver\Function.htm   
		to get more information.

		[2/Feb/2002] by Kevin
		[05,Oct,2004] Commented by Annita
*/

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

unsigned long FSeek(FILE_DATA far *file_pointer,char cMark,unsigned long lStart,int iTh);
    /*
    Seeks the iTh(th) cMark from the start position 
    to the EOF(end of file).
    
    cMark: find the position next to the cMark.
    lStart: the start position to seek the cMark. 
            (0: start position of the file)
    iTh: seek the iTh(th) cMark.
    
    Return value:
        0: can't find the cMark
       >0: the offset position next to the cMark
    */
    
void main(void)
{
    static FILE_DATA far *config;    
    int i;
    char sItem1[20];
	char sItem2[20];
	char sItem3[20];
	char sItem4[20];
    char sTemp[40];
    int HighRamMode;
    extern int bNeedDisableHighRam;
    
    /*	
    Xserver executes this function when received a package 
    form TCP port 10000	and the first two bytes are "19".
    
    Funtion of Xserver, Please refer to XDemo04 for detail description.
    */	
    
    unsigned long lPointer=0;
    int iTh; //iThe: to find information from ?th * in the text file.
    
    InitLib();

    config=GetFileInfoByName("config_1.ini");	//Opens one file by name.

    if(bNeedDisableHighRam) {
	HighRamMode=GetHighRamMode();
	DisableHighRam();
    }

    while(lPointer<config->size) 
    {
        lPointer=FSeek(config,'*',lPointer,1);
        if(lPointer) /* cannot find '*' */            
        {
            sscanf(config->addr+lPointer,"%s %s %s %s",sItem1,sItem2,sItem3,sItem4); 
            sprintf(sTemp,"Item1=%s Item2=%s Item3=%s Item4=%s", 
                             sItem1,sItem2,sItem3,sItem4);
            Print("%s\r\n",sTemp);
        }   
        else
        {
                break;                           
        }
    }

    if(bNeedDisableHighRam) {
	SetHighRam(HighRamMode);
    }
}
/*=====================================================================*/
/*====== Following functions are used to read config file.          ===*/
/*====== You can copy the functions to your own program.            ===*/
/*=====================================================================*/
unsigned long FSeek(FILE_DATA far *file_pointer,char cMark,unsigned long lStart,int iTh)
{
    /*
    Seeks the iTh(th) cMark from the start position 
    to the EOF(end of file).
    
    cMark: find the position next to the cMark.
    lStart: the start position to seek the cMark. 
            (0: start position of the file)
    iTh: seek the iTh(th) cMark.
    
    Return value:
        0: can't find the cMark
       >0: the offset position next to the cMark
    */
    
    char c;
    unsigned long i;
    int iOrder;
    
    i=lStart;
    iOrder=1;
    
    while(i<(file_pointer->size-1))
    {
        c=file_pointer->addr[i];
        if(c==cMark)
            if(iOrder==iTh)
            {    
                iTh++;
                return ++i; /* return the position next to the cMark. */
            }
            else
                iOrder++;
        i++;
    }
    return 0;
}
