Пример #1
0
int main(int argc , char* argv[])
{
    int fd = -1;
    int ret = 0;
    I2C_DATA_S i2c_data;
    unsigned int device_addr, reg_addr, reg_value;

    if(argc != 4)
    {
        printf("usage: %s <device_addr> <reg_addr> <value>. sample: %s 0x56 0x0 0x28\n", argv[0], argv[0]);
        return -1;
    }

    if (StrToNumber(argv[1], &device_addr))
    {
        return 0;
    }

    if (StrToNumber(argv[2], &reg_addr))
    {
        return 0;
    }

    if (StrToNumber(argv[3], &reg_value))
    {
        return 0;
    }

    fd = open("/dev/i2c", 0);
    if(fd < 0)
    {
        printf("Open i2c device error!\n");
        return -1;
    }

    i2c_data.dev_addr = device_addr;
    i2c_data.reg_addr = reg_addr;
    i2c_data.addr_byte = 1;
    i2c_data.data = reg_value;
    i2c_data.data_byte = 1;

    printf("write: device_addr:0x%2x; reg_addr:0x%2x; reg_value:0x%2x.\n", device_addr, reg_addr, reg_value);

    ret = ioctl(fd, I2C_CMD_WRITE, &i2c_data);
    if(ret)
    {
        printf("i2c write failed!\n");
        close(fd);
        return -1 ;
    }

    close(fd);

    return 0;
}
HI_RET himd(int argc , char* argv[])
{
    U32 ulAddr = 0;
    VOID* pMem  = NULL;
    LENGTH_T len;

    if (argc < 2)
    {
        printf("usage: %s <address>. sample: %s 0x80040000\n", argv[0], argv[0]);
        EXIT("", -1);
    }


    if (argc == 3)
    {
         if ( StrToNumber(argv[2], &len) != HI_SUCCESS)
        {
            len = DEFAULT_MD_LEN;
        }
    }
    else
    {
        len = DEFAULT_MD_LEN;
    }
    

    if( StrToNumber(argv[1], &ulAddr) == HI_SUCCESS)
    {
        printf("====dump memory %#010lX====\n", ulAddr);
        #ifdef PC_EMULATOR
        #define SHAREFILE "../shm"
        printf("**** is Emulator, use share file : %s ****\n", SHAREFILE);
        pMem = mmapfile(SHAREFILE , len);
        if (NULL == pMem)
        {
            EXIT("Memory Map error.", -1);
        }
        pMem += ulAddr;
        #else        
        pMem = memmap(ulAddr, len);
        if (NULL == pMem)
        {
            EXIT("Memory Map error.", -1);
        }        
        #endif
        
        hi_hexdump(STDOUT, pMem, len, 16);
    }
    else
    {
        printf("Please input address like 0x12345\n");
    }
    
    return 0;
}
Пример #3
0
//Main "main" function
//Take an input message and does the appropriate action
//Returns an output message
OMessagePtr ActionDo(Hospital* hospital, IMessagePtr iMessagePtr) {
    if(iMessagePtr == NULL)
        return OMessageCreate(IGNORED, NULL);
    else {
        char* name;
        char* description;
        char* name1;
        char* name2;
        int n;
        switch(iMessagePtr->inputType) {
            case NewDisease:
                name = iMessagePtr->arg1;
                description = iMessagePtr->arg2;
                if(HospitalNewDisease(hospital, name, description))
                    return OMessageCreate(OK, NULL);
                else
                    return OMessageCreate(IGNORED, NULL);
            case CopyDisease:
                name1 = iMessagePtr->arg1;
                name2 = iMessagePtr->arg2;
                if(HospitalCopyDisease(hospital, name1, name2))
                    return OMessageCreate(OK, NULL);
                else
                    return OMessageCreate(IGNORED, NULL);
            case ChangeDescription:
                name = iMessagePtr->arg1;
                n = StrToNumber(iMessagePtr->arg2);
                description = iMessagePtr->arg3;
                if(HospitalChangeDescription(hospital, name, n, description))
                    return OMessageCreate(OK, NULL);
                else
                    return OMessageCreate(IGNORED, NULL);
            case PrintDescription:
                name = iMessagePtr->arg1;
                n = StrToNumber(iMessagePtr->arg2);
                description = HospitalPrintDescription(hospital, name, n);
                if(description != NULL)
                    return OMessageCreate(OK, description);
                else
                    return OMessageCreate(IGNORED, NULL);
            case DeletePatient:
                name = iMessagePtr->arg1;
                if(HospitalDeletePatient(hospital, name))
                    return OMessageCreate(OK, NULL);
                else
                    return OMessageCreate(IGNORED, NULL);
            default:
                return OMessageCreate(IGNORED, NULL);
        }
    }
}
Пример #4
0
// binary string to hexadecimal string
char* BinStrToHexStr(char* binStr, char* hexStr)
{
    int i, j, t;
    Number binNum;
    Number hexNum;

    StrToNumber(binStr, &binNum);

    hexNum.sign = binNum.sign;
    hexNum.len = (int)ceil(binNum.len / 4.0);

    for (i = 0; i < hexNum.len; i++)
    {
        j = 4 * i;

        t = binNum.value[j];

        if (j + 1 < binNum.len)
            t += 2 * binNum.value[j + 1];

        if (j + 2 < binNum.len)
            t += 4 * binNum.value[j + 2];

        if (j + 3 < binNum.len)
            t += 8 * binNum.value[j + 3];

        hexNum.value[i] = t;
    }

    return NumberToStr(&hexNum, hexStr);
}
Пример #5
0
// change type: string to BigInt, use complement to store
BigInt* StrToBigInt(char* s, BigInt* a)
{
    char buf[BUFFER_SIZE];
    Number binNum;

    ChangeStringRadix(s, 10, 2, buf);    // string radix 10 to 2
    StrToNumber(buf, &binNum);           // string to Number
    return BinNumToBigInt(&binNum, a);   // Number to BigInt
}
Пример #6
0
int main(int argc , char* argv[])
{
	int fd = -1;
	int ret =0;
	unsigned int device_addr, reg_addr, reg_value, value;
		
	if(argc != 4)
    {
    	printf("usage: %s <device_addr> <reg_addr> <value>. sample: %s 0x56 0x0 0x28\n", argv[0], argv[0]);
        return -1;
    }
	
	fd = open("/dev/gpioi2c", 0);
    if(fd<0)
    {
    	printf("Open gpioi2c error!\n");
    	return -1;
    }
    
    if (StrToNumber(argv[1], &device_addr))
    {    	
    	return 0;
    }
    
    
    if (StrToNumber(argv[2], &reg_addr))
    {    
    	return 0;
    }
    
    if (StrToNumber(argv[3], &reg_value))
    {    
    	return 0;
    }
    
    printf("device_addr:0x%2x; reg_addr:0x%2x; reg_value:0x%2x.\n", device_addr, reg_addr, reg_value);
    
    value = ((device_addr&0xff)<<24) | ((reg_addr&0xff)<<16) | (reg_value&0xffff);
    
    ret = ioctl(fd, GPIO_I2C_WRITE, &value);
       
    return 0;
}
Пример #7
0
CurrentDateTime::CurrentDateTime()
{
	//fetch/store current local-daytime information
	auto tp = std::chrono::system_clock::now();
	time_t cstyle_t = std::chrono::system_clock::to_time_t(tp);
	char* cstyleinfo = std::ctime(&cstyle_t);
	// copy(deep) the data into the std::string as ::ctime() provides static data 
	// which might be overwritten in case someone call it again.
	std::string currentinfo{ cstyleinfo };

	//parse/store  the information
	auto dtstr = ParseDateTime(currentinfo);
	StrToNumber(dtstr);
}
/*memory dump bin*/
HI_RET himdb(int argc , char* argv[])
{
    U32 ulAddr = 0;
    VOID* pMem  = NULL;
    VOID* pBase = NULL;
    LENGTH_T len = DEFAULT_MD_LEN;

    FILENAME_T fn;
    if (argc >= 3)
    {
        if ( StrToNumber(argv[2], &len) != HI_SUCCESS)
        {
            len = DEFAULT_MD_LEN;
        }

        if (argc == 4)
        {
            strcpy(fn, argv[3]);
        }
        else
        {
            sprintf(fn, "md_%s-%s", argv[1], argv[2]);
        }
    }
    else
    {
        printf("usage: %s <address> <len> [filename]. sample: %s 0x80040000 \n", argv[0], argv[0]);
        EXIT("", -1);
    }
    

    if( StrToNumber(argv[1], &ulAddr) == HI_SUCCESS)
    {
        printf("====dump memory:<%#010lX><%lu> to file:<%s>====\n", 
                        ulAddr,
                        len,
                        fn);
        
        #ifdef PC_EMULATOR
        #define SHAREFILE "../shm"
        printf("**** is Emulator, use share file : %s ****\n", SHAREFILE);
        pBase  = mmapfile(SHAREFILE , len);
        pMem = pBase ;
        if (NULL == pMem)
        {
            EXIT("Memory Map error.", -1);
        }
        pMem = pBase + ulAddr;
        #else        
        pBase = memmap(ulAddr, len);
        pMem = pBase ;
        if (NULL == pMem)
        {
            EXIT("Memory Map error.", -1);
        }     
        
        #endif
        
        (void)hi_md2file(pMem, len, NULL, fn);
    }
    else
    {
        printf("Please input address like 0x12345\n");
    }

    (void)munmap(pBase, len);
    return 0;
}
Пример #9
0
int main(int argc , char* argv[])
{
	int fd = -1;
	int ret;
	I2C_DATA_S  i2c_data ;
	unsigned int device_addr, reg_addr, reg_addr_end, reg_value;

	if (argc < 3)
    {
    	printf("usage: %s <device_addr> <reg_addr> <end_reg_addr> <reg_width> <data_width>. sample: \n", argv[0]);
        printf("------------------------------------------------------------------------------------\n");
        printf("\t\t%s 0x56 0x0 0x10 2 2. \n", argv[0]);
        printf("\t\t%s 0x56 0x0 0x10. default reg_width and data_width is 1. \n", argv[0]);

        return -1;
    }

	fd = open("/dev/gpioi2c_ex", 0);
    if (fd<0)
    {
    	printf("Open gpioi2c_ex dev error!\n");
    	return -1;
    }

    if (StrToNumber(argv[1], &device_addr))
    {
    	return 0;
    }

    if (StrToNumber(argv[2], &reg_addr))
    {
    	return 0;
    }

    if(argc > 3)
    {
        if (StrToNumber(argv[3], &reg_addr_end))
        {
            return 0;
        }
        if (reg_addr_end < reg_addr)
        {
            printf("end addr(0x%2x) should bigger than start addr(0x%2x)\n",
                reg_addr_end, reg_addr);
            return 0;
        }

        if((argc > 4) && StrToNumber(argv[4], &reg_width))
            return 0;

        if((argc > 5) && StrToNumber(argv[5], &data_width))
            return 0;
    }
    else
    {
        reg_addr_end = reg_addr;
    }

    printf("dev_addr:0x%2x; reg_addr:0x%2x; reg_addr_end:0x%2x; reg_width: %d; data_width: %d. \n", device_addr,
            reg_addr, reg_addr_end, reg_width, data_width);
    {
        int cur_addr;

        for (cur_addr = reg_addr; cur_addr < reg_addr_end + 1; cur_addr ++)
        {
            i2c_data.dev_addr = device_addr ;
            i2c_data.reg_addr = cur_addr    ;
            i2c_data.addr_byte_num   = reg_width  ;
            i2c_data.data_byte_num   = data_width ;
            ret = ioctl(fd, GPIO_I2C_READ, &i2c_data);
            reg_value =  i2c_data.data ;
            printf("0x%x 0x%x\n", cur_addr, reg_value);
        }
    }

	close(fd);

    return 0;
}
Пример #10
0
int main(int argc , char* argv[])
{
	int fd = -1;
	int ret =0;
	
	unsigned int  PwmDirector; 
    unsigned int Period;
	
    PWM_DATA_S  stPwmData;
	
		
	if(argc != 3)
    {
    	printf("usage: %s  <Director> <Period>  sample: %s 0x56 0x0 0x28\n", argv[0], argv[0]);
        return -1;
    }
	
	fd = open("/dev/pwm", 0);
    if(fd<0)
    {
    	printf("Open pwm error!\n");
    	return -1;
    }
    
    if (StrToNumber(argv[1], &PwmDirector))
    {    	
    	return 0;
    }
    
    if (StrToNumber(argv[2], &Period))
    {    
    	return 0;
    }
    
    
    
    printf("PWM_Director:0x%4x;  Period:0x%4x.\n", PwmDirector,  Period);

	stPwmData.pwm_num = 0;
	stPwmData.period = 1000;
	stPwmData.enable = 1;

	if(PwmDirector == 0)
	{
		stPwmData.duty = Period;
	}
	else
	{
		stPwmData.duty = 1;
	}

	
    //for(i = 0;i < Period;i++)
    while(1)
    {
    /*
		if(PwmDirector == 0)
		{
			stPwmData.duty--;
		}
		else
		{
			stPwmData.duty++;
		}
	*/	
	    stPwmData.duty = 420;
	    ret = ioctl(fd, PWM_CMD_WRITE, &stPwmData);
	    printf("ret :%d\n", ret);
		usleep(66000);
		stPwmData.duty = 610;
		ret = ioctl(fd, PWM_CMD_WRITE, &stPwmData);
	    printf("ret :%d\n", ret);
		usleep(66000);
		
		
	}
    

    //printf("device_addr:0x%2x; reg_addr:0x%2x; reg_value:0x%2x.\n", device_addr, reg_addr, reg_value);

	close(fd);

    return 0;
}
Пример #11
0
int main(int argc , char* argv[])
{
	int fd = -1;
	int ret;
	unsigned int device_addr, reg_addr, reg_addr_end,reg_value, value;
	
	
	if ((argc != 3) && (argc != 4))
    {
    	printf("usage: %s <device_addr> <reg_addr>. sample: %s 0x56 0x0\n", argv[0], argv[0]);
        return -1;
    }
    
    if (StrToNumber(argv[1], &device_addr))
    {    	
    	return 0;
    }
       
    if (StrToNumber(argv[2], &reg_addr))
    {    
    	return 0;
    }
    	
	fd = open("/dev/gpioi2c", 0);
    if (fd<0)
    {
    	printf("Open gpioi2c dev error!\n");
    	return -1;
    }

    if (3 == argc)
    {
        printf("device_addr:0x%2x; reg_addr:0x%2x.\n", device_addr, reg_addr);
        value = ((device_addr&0xff)<<24) | ((reg_addr&0xff)<<16);        
        ret = ioctl(fd, GPIO_I2C_READ, &value);        
        reg_value = value&0xff;        
        printf("0x%2x\n", reg_value);
    }
    else if (4 == argc)
    {
        int cur_addr;
        if (StrToNumber(argv[3], &reg_addr_end))
        {
        	close(fd);
            return 0;
        }
        if (reg_addr_end < reg_addr)
        {
            printf("end addr(0x%2x) should bigger than start addr(0x%2x)\n",
                reg_addr_end, reg_addr);
            close(fd);
            return 0;
        }
        printf("device_addr:0x%2x; reg_addr_start:0x%2x; reg_addr_end:0x%2x.\n", 
            device_addr, reg_addr, reg_addr_end);
        for (cur_addr=reg_addr; cur_addr<reg_addr_end+1; cur_addr++)
        {
            value = ((device_addr&0xff)<<24) | ((cur_addr&0xff)<<16);   
            ret = ioctl(fd, GPIO_I2C_READ, &value);            
            reg_value = value&0xff;
            printf("0x%x 0x%x\n", cur_addr, reg_value);
        }
    }

    close(fd);
            
    return 0;
}
Пример #12
0
int main(int argc, char* argv[])
{
#if DYNAMIC_SEARCH_LIMIT
	if (argc < 2)
	{
		std::cerr << "You must specify search limit as a first command-line argument" << std::endl;
		return 0;
	}
	SearchLimit::value = static_cast<number>(StrToNumber(argv[1]));
	if (SearchLimit::value < 1000)
	{
		SearchLimit::value = 1000;
	}
	SearchLimit::LinearLimit = static_cast<number>(sqrt(SearchLimit::value * 2.0)) + 1;
	SearchLimit::MainPrimeTableBound = std::max<number>(SearchLimit::LinearLimit, 1000);
	SearchLimit::PrimeInversesBound = std::max<number>(static_cast<number>(sqrt(SearchLimit::value / 4)), CompileTimePrimes<CompileTimePrimesCount>::value);
	SearchLimit::SafeLimit = SearchLimit::value / 20;
#endif

	PrimeTablesInit();

	number numThreads = 0;
	char* startFrom = nullptr;
	char* stopAt = nullptr;
	unsigned int largestPrimePower = 1;
	number startPrime = 0;
	number primeLimit = 0;

	// Parse command line parameters
	for (int i = 1; i < argc; ++i)
	{
		if (strcmp(argv[i], "/bench") == 0)
		{
			g_PrintNumbers = false;
		}

		if (strcmp(argv[i], "/instrument") == 0)
		{
			// Quickly generate profile data for all hot (most used) code paths
			ProfileGuidedOptimization_Instrument();
			return 0;
		}

		if (strcmp(argv[i], "/test") == 0)
		{
			g_PrintNumbers = false;

			std::cout << "Testing CheckPair()...";
			flush(std::cout);
			if (!TestCheckPair())
			{
				std::cerr << "CheckPair() test failed" << std::endl;
				return 1;
			}
			std::cout << "done" << std::endl << "Testing amicable candidates...";
			flush(std::cout);
			if (!TestAmicableCandidates())
			{
				std::cerr << "Amicable candidates test failed" << std::endl;
				return 2;
			}
			std::cout << "done" << std::endl << "Testing MaximumSumOfDivisors3()...";
			flush(std::cout);
			if (!TestMaximumSumOfDivisors3())
			{
				std::cerr << "MaximumSumOfDivisors3() test failed" << std::endl;
				return 3;
			}
			std::cout << "done" << std::endl << "Testing primesieve...";
			flush(std::cout);
			if (!TestPrimeSieve())
			{
				std::cerr << "primesieve test failed" << std::endl;
				return 4;
			}
			std::cout << "done" << std::endl << "All tests passed" << std::endl;
			return 0;
		}

		if ((strcmp(argv[i], "/threads") == 0) && (i + 1 < argc))
		{
			numThreads = static_cast<number>(atoi(argv[++i]));
		}

		if ((strcmp(argv[i], "/from") == 0) && (i + 1 < argc))
		{
			startFrom = argv[++i];
		}

		if ((strcmp(argv[i], "/to") == 0) && (i + 1 < argc))
		{
			stopAt = argv[++i];
		}

		if (((strcmp(argv[i], "/largest_prime_power") == 0) || (strcmp(argv[i], "/lpp") == 0)) && (i + 1 < argc))
		{
			largestPrimePower = static_cast<unsigned int>(atoi(argv[++i]));
			if (largestPrimePower < 1)
			{
				largestPrimePower = 1;
			}
			if (largestPrimePower > 3)
			{
				largestPrimePower = 3;
			}
		}

		if (((strcmp(argv[i], "/large_primes_range") == 0) || (strcmp(argv[i], "/lpr") == 0)) && (i + 2 < argc))
		{
			startPrime = static_cast<unsigned int>(StrToNumber(argv[++i]));
			primeLimit = static_cast<unsigned int>(StrToNumber(argv[++i]));
		}
	}

	do
	{
		Timer t;
		RangeGen::Run(numThreads, startFrom, stopAt, largestPrimePower, startPrime, primeLimit);
		const double dt = t.getElapsedTime();
		printf("completed in %f seconds\n%u pairs found\n\n", dt, GetNumFoundPairsInThisThread());
	} while (!g_PrintNumbers);

	return 0;
}
Пример #13
0
// change string radix from srcRadix to dstRadix
char* ChangeStringRadix(char* str, int srcRadix, int dstRadix, char* resultStr)
{
    if (srcRadix < dstRadix)
    {
        char hexStr[BUFFER_SIZE];

        ChangeStringRadix(str, srcRadix, 2, resultStr);  // srcRadix to radix 2
        BinStrToHexStr(resultStr, hexStr);               // radix 2 to 16

        // radix 16 to dstRadix
        return ChangeStringRadix(hexStr, 16, dstRadix, resultStr);
    }

    if (srcRadix == dstRadix)
    {
        return strcpy(resultStr, str);
    }

    else  // srcRadix > dstRadix
    {
        int i, t;
        Number dividend;
        Number quotient;
        Number resultNum;

        // string to Number
        StrToNumber(str, &dividend);

        resultNum.len = 0;
        resultNum.sign = dividend.sign;

        while (dividend.len > 0)
        {
            quotient.len = dividend.len;

            // simulate the way we do division
            // when the cycle is end, t is the remainder
            for (t = 0, i = dividend.len - 1; i >= 0; i--)
            {
                t = t * srcRadix + dividend.value[i];
                quotient.value[i] = t / dstRadix;
                t = t % dstRadix;
            }

            // save the remainder
            resultNum.value[resultNum.len++] = t;

            // filter the unnecessary 0 in quotient
            for (i = quotient.len - 1; i >= 0 && quotient.value[i] == 0; i--);

            // set the next dividend length
            dividend.len = i + 1;

            // let the quotient be the next divident
            for (i = 0; i < dividend.len; i++)
            {
                dividend.value[i] = quotient.value[i];
            }
        }

        return NumberToStr(&resultNum, resultStr);
    }
}
Пример #14
0
int  CESBolanStack::PushScript(LPCTSTR ExpStr,CESVariableList* pVariableList,CESFunctionList * pFunctionList,int& LastLine)
{
	TCHAR TempID[MAX_IDENTIFIER_LENGTH];

	CESBolanStack TempStack;
	CESBolanStack OrginScript;
	ES_BOLAN Bolan;
	
	bool IsInStr;
	LastLine=1;

	CEasyBuffer Buffer(((UINT)_tcslen(ExpStr)+1)*sizeof(TCHAR));
	CEasyBuffer TempBuffer(((UINT)_tcslen(ExpStr)+1)*sizeof(TCHAR));

	TCHAR * ExpBuff=(TCHAR *)Buffer.GetBuffer();
	TCHAR * TempBuff=(TCHAR *)TempBuffer.GetBuffer();


	UINT Len=0;
	IsInStr=false;
	while(*ExpStr)
	{
		if(*ExpStr=='"') IsInStr=!IsInStr;

		if(IsInStr)
			ExpBuff[Len++]=(*ExpStr++);
		else
			ExpBuff[Len++]=toupper(*ExpStr++);		

	}
	ExpBuff[Len]=0;
	//CharUpper(ExpBuff);
	ExpStr=ExpBuff;

	//先提取原始布兰式
	while(*ExpStr)
	{
		Bolan.Clear();
		Bolan.Line=LastLine;
		if(*ExpStr==' '||*ExpStr=='\r'||*ExpStr=='\n'||*ExpStr=='	')
		{
			//空格、空行忽略
			if(*ExpStr=='\n')
				LastLine++;
			ExpStr++;
		}
		else if(*ExpStr=='/'&&*(ExpStr+1)=='/')
		{
			while((*ExpStr!='\r')&&(*ExpStr!='\n')&&(*ExpStr))
			{
				if(*ExpStr=='\n')
					LastLine++;
				ExpStr++;
			}
		}
		else if(*ExpStr=='"')//字符串
		{
			UINT i=0;
			ExpStr++;
			while(*ExpStr!='"')
			{
				//if(i>MAX_STRING_LENGTH)
				//	return 1001;
				if(*ExpStr==0)
					return 1002;
				TempBuff[i++]=*ExpStr++;
				if(*ExpStr=='"'&&*(ExpStr+1)=='"')
				{
					TempBuff[i++]=*ExpStr++;
					ExpStr++;
				}
			}
			TempBuff[i]=0;
			ExpStr++;

			Bolan.Type=BOLAN_TYPE_VALUE;
			Bolan.ValueType=VALUE_TYPE_STRING;
			Bolan.StrValue=TempBuff;
			Bolan.Level=0;
			OrginScript.Push(&Bolan);
		}
		else if((*ExpStr>='0'&&*ExpStr<='9')||*ExpStr=='.')	//数字
		{
			UINT i=0;
			while((*ExpStr>='0'&&*ExpStr<='9')||*ExpStr=='.'||*ExpStr=='F'||*ExpStr=='D'||*ExpStr=='I'||*ExpStr=='L'||*ExpStr=='E'||
				*ExpStr=='A'||*ExpStr=='B'||*ExpStr=='C'||*ExpStr=='X')
			{
				TempBuff[i++]=*ExpStr++;
			}
			TempBuff[i]=0;
			Bolan.Type=BOLAN_TYPE_VALUE;			
			Bolan.Level=0;
			StrToNumber(TempBuff,Bolan);
			OrginScript.Push(&Bolan);
		}
		else if(*ExpStr==':')		//跳转标识
		{
			UINT i=0;
			ExpStr++;
			while(CanMakeIdentifier(*ExpStr))
			{
				if(i>MAX_IDENTIFIER_LENGTH)
					return 1003;
				TempID[i++]=*ExpStr++;
			}
			TempID[i]=0;			
			Bolan.Type=BOLAN_TYPE_IDENTIFIER;
			Bolan.Index=IDENTIFIER_TYPE_JUMP_DEFINE;
			Bolan.Level=0;				
			Bolan.StrValue=TempID;
			OrginScript.Push(&Bolan);			
		}
		else if(*ExpStr==';'||*ExpStr=='\r'||*ExpStr=='\n')//行结束符
		{			
			Bolan.Type=BOLAN_TYPE_KEYWORD;
			Bolan.Level=0;
			Bolan.Index=KW_LINEEND;
			OrginScript.Push(&Bolan);
			//除去多余的行结束符
			while(*ExpStr==' '||*ExpStr==';'||*ExpStr=='\r'||*ExpStr=='\n')
			{
				if(*ExpStr=='\n')
					LastLine++;
				ExpStr++;
			}
		}
		else if(*ExpStr>='A'&&*ExpStr<='Z')//标识符
		{
			UINT i=0;
			while(CanMakeIdentifier(*ExpStr))
			{
				if(i>MAX_IDENTIFIER_LENGTH)
					return 1003;
				TempBuff[i++]=*ExpStr++;
			}
			TempBuff[i]=0;	
			if(_tcsicmp(TempBuff,_T("AND"))==0)
			{				
				Bolan.Type=BOLAN_TYPE_OPERATOR;
				Bolan.Level=15;
				Bolan.Index=OPERATOR_AND;
				OrginScript.Push(&Bolan);			
			}
			else if(_tcsicmp(TempBuff,_T("OR"))==0)
			{				
				Bolan.Type=BOLAN_TYPE_OPERATOR;
				Bolan.Level=15;
				Bolan.Index=OPERATOR_OR;
				OrginScript.Push(&Bolan);			
			}
			else if(_tcsicmp(TempBuff,_T("NOT"))==0)
			{				
				Bolan.Type=BOLAN_TYPE_OPERATOR;
				Bolan.Level=16;
				Bolan.Index=OPERATOR_NOT;
				OrginScript.Push(&Bolan);			
			}
			else
			{
				int KeyWord=FindKeyWord(TempBuff);
				if(KeyWord>=0)//关键字
				{
					Bolan.Type=BOLAN_TYPE_KEYWORD;
					Bolan.Level=0;
					Bolan.Index=KeyWord;
					OrginScript.Push(&Bolan);

				}
				else
				{		
					Bolan.Type=BOLAN_TYPE_IDENTIFIER;
					Bolan.Index=IDENTIFIER_TYPE_UNKNOW;
					Bolan.Level=0;				
					Bolan.StrValue=TempBuff;
					OrginScript.Push(&Bolan);					
				}
			}
		}
		else if(*ExpStr=='(')
		{			
			Bolan.Type=BOLAN_TYPE_OPERATOR;
			Bolan.Level=0;
			Bolan.Index=OPERATOR_LP;
			OrginScript.Push(&Bolan);
			ExpStr++;			
		}
		else if(*ExpStr==')')
		{
			Bolan.Type=BOLAN_TYPE_OPERATOR;
			Bolan.Level=0;
			Bolan.Index=OPERATOR_RP;
			OrginScript.Push(&Bolan);
			ExpStr++;
		}
		else if(*ExpStr=='=')
		{	
			if(*(ExpStr+1)=='=')
			{				
				Bolan.Type=BOLAN_TYPE_OPERATOR;
				Bolan.Level=20;
				Bolan.Index=OPERATOR_EQU;	
				OrginScript.Push(&Bolan);
				ExpStr+=2;
			}
			else
			{					
				Bolan.Type=BOLAN_TYPE_OPERATOR;
				Bolan.Level=10;
				Bolan.Index=OPERATOR_EVA;	
				OrginScript.Push(&Bolan);
				ExpStr++;
			}
		}
		else if(*ExpStr=='>')
		{			
			if(*(ExpStr+1)=='=')
			{				
				Bolan.Type=BOLAN_TYPE_OPERATOR;
				Bolan.Level=20;
				Bolan.Index=OPERATOR_MORE_EQU;	
				OrginScript.Push(&Bolan);
				ExpStr+=2;	
			}
			else
			{				
				Bolan.Type=BOLAN_TYPE_OPERATOR;
				Bolan.Level=20;
				Bolan.Index=OPERATOR_MORE;	
				OrginScript.Push(&Bolan);
				ExpStr++;				
			}
		}
		else if(*ExpStr=='<')
		{			
			if(*(ExpStr+1)=='=')
			{				
				Bolan.Type=BOLAN_TYPE_OPERATOR;
				Bolan.Level=20;
				Bolan.Index=OPERATOR_LESS_EQU;		
				OrginScript.Push(&Bolan);
				ExpStr+=2;	
			}
			else if(*(ExpStr+1)=='>')
			{				
				Bolan.Type=BOLAN_TYPE_OPERATOR;
				Bolan.Level=20;
				Bolan.Index=OPERATOR_DIFF;		
				OrginScript.Push(&Bolan);
				ExpStr+=2;	
			}
			else
			{					
				Bolan.Type=BOLAN_TYPE_OPERATOR;
				Bolan.Level=20;
				Bolan.Index=OPERATOR_LESS;			
				OrginScript.Push(&Bolan);
				ExpStr++;				
			}			

		}
		else if(*ExpStr=='+'||*ExpStr=='-')
		{
			//if(*ExpStr=='-'&&(ExpStr==ExpBuff||*(ExpStr-1)=='('))
			//{				
			//	Bolan.Type=BOLAN_TYPE_OPERATOR;
			//	Bolan.Index=OPERATOR_NEG;
			//	Bolan.Level=50;
			//	OrginScript.Push(&Bolan);
			//	ExpStr++;
			//}
			//else
			{						
				Bolan.Type=BOLAN_TYPE_OPERATOR;
				Bolan.Level=30;
				if(*ExpStr=='+') Bolan.Index=OPERATOR_ADD;
				else Bolan.Index=OPERATOR_SUB;	
				OrginScript.Push(&Bolan);
				ExpStr++;				
			}
		}
		else if(*ExpStr=='*'||*ExpStr=='/')
		{						
			Bolan.Type=BOLAN_TYPE_OPERATOR;
			Bolan.Level=40;
			if(*ExpStr=='*') Bolan.Index=OPERATOR_MUL;
			else Bolan.Index=OPERATOR_DIV;	
			OrginScript.Push(&Bolan);
			ExpStr++;
		}
		else if(*ExpStr==',')
		{
			Bolan.Type=BOLAN_TYPE_OPERATOR;
			Bolan.Index=OPERATOR_COMMA;
			Bolan.Level=0;
			OrginScript.Push(&Bolan);			
			ExpStr++;
		}		
		else
		{
			return 1005;				//非法字符
		}		
	}

	CESThread ESThread;

	ESThread.SetScript(&OrginScript);
	ESThread.SetVariableList(pVariableList);
	ESThread.SetFunctionList(pFunctionList);

	int RetCode=DealIdentifiers(&ESThread);
	LastLine=ESThread.GetLastLine();
	if(RetCode)
		return RetCode;

	

	
	for(UINT i=0;i<OrginScript.GetSize();i++)
	{
		ES_BOLAN * pBolan=OrginScript.GetAt(i);
		LastLine=pBolan->Line;
		switch(pBolan->Type)
		{
		case BOLAN_TYPE_VALUE:
			Push(pBolan);
			break;
		case BOLAN_TYPE_OPERATOR:
			if(pBolan->Index==OPERATOR_RP)
			{
				while(TempStack.GetTop()!=NULL&&(TempStack.GetTop()->Type!=BOLAN_TYPE_OPERATOR||TempStack.GetTop()->Index!=OPERATOR_LP))
				{				
					ES_BOLAN * pTemp=TempStack.Pop();					
					Push(pTemp);
				}			
				if(TempStack.GetTop()==NULL||TempStack.GetTop()->Type!=BOLAN_TYPE_OPERATOR||TempStack.GetTop()->Index!=OPERATOR_LP)
					return 2021;
				TempStack.Pop();
			}
			else if(pBolan->Index==OPERATOR_LP)
			{
				TempStack.Push(pBolan);
			}			
			else if(pBolan->Index==OPERATOR_NOP)
			{

			}
			else if(pBolan->Index==OPERATOR_COMMA)
			{
				while(TempStack.GetTop()!=NULL&&(TempStack.GetTop()->Type!=BOLAN_TYPE_OPERATOR||TempStack.GetTop()->Index!=OPERATOR_LP))
				{				
					ES_BOLAN * pTemp=TempStack.Pop();					
					Push(pTemp);
				}	
			}
			else
			{
				while(TempStack.GetTop()!=NULL&&TempStack.GetTop()->Level>=pBolan->Level)
				{		
					ES_BOLAN * pTemp=TempStack.Pop();				
					Push(pTemp);
				}	
				TempStack.Push(pBolan);
			}
			break;
		case BOLAN_TYPE_FUNCTION:
			while(TempStack.GetTop()!=NULL&&TempStack.GetTop()->Level>=pBolan->Level)
			{		
				ES_BOLAN * pTemp=TempStack.Pop();				
				Push(pTemp);
			}		
			TempStack.Push(pBolan);
			break;
		case BOLAN_TYPE_VARIABLE:
			Push(pBolan);
			break;
		case BOLAN_TYPE_KEYWORD:
			while(TempStack.GetTop()!=NULL)
			{		
				ES_BOLAN * pTemp=TempStack.Pop();			
				Push(pTemp);
			}	
			switch(pBolan->Index)
			{
			case KW_EXIT:
			case KW_IF:			
			case KW_THEN:
			case KW_ELSE:
			case KW_ELSEIF:				
			case KW_ENDIF:
			case KW_WHILE:				
			case KW_DO:				
			case KW_ENDWHILE:				
			case KW_BREAK:				
			case KW_CONTINUE:				
			case KW_GOTO:
				Push(pBolan);
				break;
			case KW_LINEEND:				
			case KW_INT:
			case KW_INT64:
			case KW_FLOAT:
			case KW_DOUBLE:
			case KW_STRING:				
				break;
			case KW_FUNCTION:				
			case KW_ENDFUN:				
				Push(pBolan);
				break;
			};			
			break;
		case BOLAN_TYPE_IDENTIFIER:
			while(TempStack.GetTop()!=NULL)
			{		
				ES_BOLAN * pTemp=TempStack.Pop();				
				Push(pTemp);
			}
			if(pBolan->Index!=IDENTIFIER_TYPE_VARIABLE_DEFINE)
				Push(pBolan);
			break;
		}
	}
	while(TempStack.GetTop()!=NULL)
	{		
		ES_BOLAN * pTemp=TempStack.Pop();
		if(pTemp->Type==BOLAN_TYPE_OPERATOR&&pTemp->Index==OPERATOR_LP)
			return 2021;
		Push(pTemp);
	}

	//设置各种控制语句
	ES_FUNCTION * pCurFunction=NULL;	

	for(UINT i=0;i<GetSize();i++)
	{
		ES_BOLAN * pBolan=GetAt(i);
		LastLine=pBolan->Line;
		if(pBolan->Type==BOLAN_TYPE_FUNCTION)
		{
			pBolan->Type=BOLAN_TYPE_OPERATOR;
			pBolan->Level=pBolan->Index;
			pBolan->Index=OPERATOR_CALL;
		}
		else if(pBolan->Type==BOLAN_TYPE_KEYWORD)
		{
			switch(pBolan->Index)
			{
			case KW_EXIT:
				pBolan->Type=BOLAN_TYPE_OPERATOR;
				pBolan->Index=OPERATOR_EXIT;
				break;
			case KW_IF:
			case KW_ELSEIF:
				{
					int EndPos=FindCoupleKeyWord(KW_ENDIF,KW_IF,KW_ENDIF,i+1);
					if(EndPos<0)
						return 2001;
					if(pBolan->Index==KW_ELSEIF)
					{
						pBolan->Type=BOLAN_TYPE_OPERATOR;
						pBolan->Index=OPERATOR_JMP;
						pBolan->Level=EndPos+1;
					}
					int Pos=GetNextKeyWord(i+1);
					if(Pos<0)
						return 2001;
					ES_BOLAN * pThenBolan=GetAt(Pos);
					if(pThenBolan->Type!=BOLAN_TYPE_KEYWORD&&pThenBolan->Index!=KW_THEN)
						return 2001;
					int ElseIfPos=FindCoupleKeyWord(KW_ELSEIF,KW_IF,KW_ENDIF,i+1,EndPos+1);
					int ElsePos=FindCoupleKeyWord(KW_ELSE,KW_IF,KW_ENDIF,i+1,EndPos+1);
					pThenBolan->Type=BOLAN_TYPE_OPERATOR;
					pThenBolan->Index=OPERATOR_JZ;
					if(ElseIfPos>=0)
						pThenBolan->Level=ElseIfPos+1;
					else if(ElsePos>=0)
						pThenBolan->Level=ElsePos+1;
					else
						pThenBolan->Level=EndPos+1;
				}					
				break;
			case KW_ELSE:
				{
					int EndPos=FindCoupleKeyWord(KW_ENDIF,KW_IF,KW_ENDIF,i+1);
					if(EndPos<0)
						return 2001;
					pBolan->Type=BOLAN_TYPE_OPERATOR;
					pBolan->Index=OPERATOR_JMP;
					pBolan->Level=EndPos+1;
				}
				break;
			case KW_WHILE:
				{
					int Pos=GetNextKeyWord(i+1);
					if(Pos<0)
						return 2011;
					ES_BOLAN * pDoBolan=GetAt(Pos);
					if(pDoBolan->Type!=BOLAN_TYPE_KEYWORD||pDoBolan->Index!=KW_DO)
						return 2011;
					Pos=FindCoupleKeyWord(KW_ENDWHILE,KW_WHILE,KW_ENDWHILE,i+1);
					if(Pos<0)
						return 2011;
					ES_BOLAN * pEndBolan=GetAt(Pos);
					pDoBolan->Type=BOLAN_TYPE_OPERATOR;
					pDoBolan->Index=OPERATOR_JZ;
					pDoBolan->Level=Pos+1;
					pEndBolan->Type=BOLAN_TYPE_OPERATOR;
					pEndBolan->Index=OPERATOR_JMP;
					pEndBolan->Level=i+1;
				}
				break;			
			case KW_ENDWHILE:
				{
					return 2011;
					//int Pos=FindCoupleKeyWordReverse(KW_WHILE,KW_WHILE,KW_ENDWHILE,i-1);
					//if(Pos<0)
					//	return 2011;
					//pBolan->Type=BOLAN_TYPE_OPERATOR;
					//pBolan->Index=OPERATOR_JMP;
					//pBolan->Level=Pos+1;
				}
				break;
			case KW_BREAK:
				{
					int Pos=FindCoupleKeyWord(KW_ENDWHILE,KW_WHILE,KW_ENDWHILE,i);
					if(Pos<0)
						return 2012;
					pBolan->Type=BOLAN_TYPE_OPERATOR;
					pBolan->Index=OPERATOR_JMP;
					pBolan->Level=Pos+1;
				}
				break;
			case KW_CONTINUE:
				{
					int Pos=FindCoupleKeyWordReverse(KW_WHILE,KW_WHILE,KW_ENDWHILE,i);
					if(Pos<0)
						return 2013;
					pBolan->Type=BOLAN_TYPE_OPERATOR;
					pBolan->Index=OPERATOR_JMP;
					pBolan->Level=Pos+1;
				}
				break;
			case KW_GOTO:
				{
					ES_BOLAN *pIDBolan=GetAt(i+1);
					if(pIDBolan==NULL)
						return 2014;
					if(pIDBolan->Type!=BOLAN_TYPE_IDENTIFIER)
						return 2014;
					int Pos=FindIdentifier(0,pIDBolan->StrValue);
					if(Pos<0)
						return 2015;
					pBolan->Type=BOLAN_TYPE_OPERATOR;
					pBolan->Index=OPERATOR_JMP;
					pBolan->Level=Pos+1;
				}
				break;
			case KW_FUNCTION:
				{
					int Pos=FindKeyWord(KW_ENDFUN,i,-1);
					if(Pos<0)
						return 2016;
					pBolan->Type=BOLAN_TYPE_OPERATOR;
					pBolan->Index=OPERATOR_JMP_FUNC;
					pBolan->Level=Pos+1;

					
				}
				break;
			case KW_ENDFUN:
				{					
					if(pCurFunction==NULL)
						return 2019;					
					pCurFunction->FunEndPos=i+1;
					pBolan->Type=BOLAN_TYPE_OPERATOR;
					pBolan->Index=OPERATOR_RET;
				}
				break;				
			}
		}
		else if(pBolan->Type==BOLAN_TYPE_IDENTIFIER&&pBolan->Index==IDENTIFIER_TYPE_FUNCTION_DEFINE)
		{
			pCurFunction=pFunctionList->FindFunction(pBolan->StrValue);
			if(pCurFunction==NULL)
			{
				return 2019;
			}
			if(pCurFunction->Type!=FUNCTION_TYPE_SCRIPT)
			{
				return 2019;
			}
			pCurFunction->FunStartPos=i;

			//反转函数参数顺序
			RetCode=ReverseFunctionParam(this,i+1,pCurFunction->ParaCount);
			if(RetCode)
				return RetCode;
		}
	}

	//删除多余的布兰式
	for(int i=(int)GetSize()-1;i>=0;i--)
	{
		ES_BOLAN * pBolan=GetAt(i);
		if(pBolan->Type!=BOLAN_TYPE_VALUE&&
			pBolan->Type!=BOLAN_TYPE_OPERATOR&&
			pBolan->Type!=BOLAN_TYPE_VARIABLE&&(pBolan->Type!=BOLAN_TYPE_KEYWORD||pBolan->Index!=KW_FUNCTION))
		{
			DeleteBolan(this,pFunctionList,i);
		}
	}

	//进行变量绑定

	ESThread.SetScript(this);
	RetCode=DoVariableBind(&ESThread,LastLine);
	if(RetCode)
		return RetCode;

	return 0;
	
}
Пример #15
0
int main(int argc , char* argv[])
{
    int fd = -1;
    int ret = 0;
    unsigned int piris_pos;
    PIRIS_STATUS_E sta;
    int step = 0;
    int toclose = 0;
    int i = 0;
    unsigned int TestSample = 0;

    if (argc < 2)
    {
        printf("usage: %s \n <cmd(a: action, o: origin, s: status, t: test IRIS sample)> \n <pos> . \n sample: %s a 92\n", argv[0], argv[0]);
        return -1;
    }

    fd = open("/dev/piris", 0);
    if (fd < 0)
    {
        printf("Open piris error!\n");
        return -1;
    }


    if (*argv[1] == 'a')
    {
        if (StrToNumber(argv[2], (unsigned int*)&piris_pos))
        {
            close(fd);
            return 0;
        }
        ret = ioctl(fd, PIRIS_SET_ACT_ARGS, &piris_pos);
    }
    else if (*argv[1] == 'o')
    {
        ret = ioctl(fd, PIRIS_SET_ORGIN, &piris_pos);
    }
    else if (*argv[1] == 's')
    {
        ret = ioctl(fd, PIRIS_GET_STATUS, &sta);

        if (sta == PIRIS_BUSY)
        {
            printf("piris running!!!\n");
        }
        else
        {
            printf("piris idle!!\n");
        }
    }
    else if (*argv[1] == 't')
    {
        if (StrToNumber(argv[2], (unsigned int*)&TestSample))
        {
            close(fd);
            return 0;
        }
        switch (TestSample)
        {
            case 0:
                i = 0;
                step = 0;
                toclose = 0;
                while (i < 10000)
                {
                    ioctl(fd, PIRIS_GET_STATUS, &sta);
                    if (sta != PIRIS_BUSY)
                    {
                        if (toclose == 1)
                        {
                            step = 92;
                            toclose = 0;
                        }
                        else
                        {
                            step = 0;
                            toclose = 1;
                        }

                        piris_pos = step;
                        ioctl(fd, PIRIS_SET_ACT_ARGS, &piris_pos);
                        i++;
                    }
                }
                break;
            case 1:
                step = 0;
                toclose = 0;
                while (step != 92)
                {
                    ioctl(fd, PIRIS_GET_STATUS, &sta);
                    if (sta != PIRIS_BUSY)
                    {
                        if (toclose == 1)
                        {
                            step -= 19;
                            toclose = 0;
                        }
                        else
                        {
                            step += 20;
                            toclose = 1;
                        }

                        piris_pos = step;
                        ioctl(fd, PIRIS_SET_ACT_ARGS, &piris_pos);
                    }
                }
                break;

            default:
                break;
        }
    }

    close(fd);

    return ret;
}