📄 funchand.cpp
字号:
// Copyright (c) 1996 Federal Highway Administration
//
// This software has been developed for the Federal Highway Administration
// by Viggen Corporation under contract with Oak Ridge National Lab.
//
// Permission to use, copy, and distribute this software for any purpose
// without fee is hereby granted, provided that the above copyright notice
// appears in all copies and that both the copyright and this permission notice
// appear in the supporting documentation.
//
// Permission to modify this software is granted provided that the above
// copyright and this permission notice appears in the modified software.
//
// This software is provided "as is" with no warranty expressed or implied.
//
// For additional information, please go to the Web site www.ntcip.org.
//
/*******************************************************************************
*
* Copyright (c) 1997 Viggen Corporation
*
* Permission to use, copy, and distribute this software for any purpose
* without fee is hereby granted, provided that this copyright notice
* appears in all copies and this permission notice appears in the
* supporting documentation.
*
* Permission to modify this software is granted provided that the above
* copyright and this permission notice appears in the modified software.
*
******************************************************************************/
/*********************************************
*
* funchand.cpp
*
* 9/29/97 Glenn Pruitt
*********************************************/
#include <string>
using namespace std;
//Function handlers
//funchand.cpp
#include <stdlib.h>
#include <stdio.h>
#include <dos.h>
#include <string.h>
#include <math.h>
#include <windows.h>
#include <winbase.h>
#include "api.h"
#include "symboltab.h"
#include "executor.h"
#include "lexical.h"
#include "funchand.h"
#include "util.h"
const int ERROR_PROTOCOL_MISMATCH=1;
const int ERROR_PROTOCOL_INVALID_RESPONSE=2;
const int ERROR_NO_RESPONSE=16;
const int ERROR_DECODE_ENCODING_STYLE=32;
const int ERROR_DECODE_NO_INSTANCE=64;
const int ERROR_DECODE_OBJECT_MISMATCH=128;
const int ERROR_DATA_INVALID_SEQUENCE=1024;
const int ERROR_DATA_INVALID_LENGTH=2048;
const int ERROR_DATA_SCRAMBLED=4096;
const int ERROR_DATA_INVALID_ADDRESS=8192;
const int ERROR_DATA_INVALID_CONTROL=16384;
const int ERROR_DATA_INVALID_CRC=32768;
const int ERROR_DATA_INVALID_IPI=65536;
void read_input_file(struct SYTAB *st);
struct ARGUMENT *fh_outfile(struct ARGUMENT *arglist,
struct SYTAB *st)
{
int outfile_index;
FILE *ofp;
outfile_index = symbol_table_lookup(st,"outfile");
realloc_string_size(st,outfile_index,strlen(arglist->sval)+1);
set_value_in_st(st,outfile_index,0,0.0,arglist->sval,0);
if((ofp = fopen(arglist->sval,"wt"))==NULL)
{
info_message("cannot open outfile\n");
return NULL;
}
fclose(ofp);
return NULL;
}
struct ARGUMENT *fh_out(struct ARGUMENT *arglist,
struct SYTAB *st)
{
FILE *ofp;
int outfile_index;
HANDLE outputFile;
DWORD numWritten;
DWORD outputFileSize=0;
outfile_index = symbol_table_lookup(st, "outfile");
if(outfile_index >= 0)
{
ofp = fopen(get_symbol_string_value(st,outfile_index),"at");
if (ofp != NULL)
{
fputs(arglist->sval, ofp);
fclose(ofp);
}
else
info_message("Error writing to output file.\n");
/* outputFile = CreateFile(get_symbol_string_value(st, outfile_index),
GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL);
if (outputFile)
{
outputFileSize = GetFileSize(outputFile, NULL);
SetFilePointer(outputFile, outputFileSize, NULL, FILE_BEGIN);
if (!WriteFile(outputFile, arglist->sval, strlen(arglist->sval),
&numWritten, NULL))
{
//info_message("Error writing to outfile");
}
WriteFile(outputFile, "\n", strlen(arglist->sval),
&numWritten, NULL);
CloseHandle(outputFile);
}
/* else
{
info_message("Error opening outfile");
}*/
}
/* else
{
info_message("Outfile not specified prior to out.\n");
}*/
return NULL;
}
struct ARGUMENT *fh_infile(struct ARGUMENT *arglist,
struct SYTAB *st)
{
int infile_index;
infile_index = symbol_table_lookup(st,"infile");
// realloc_string_size(st,infile_index,strlen(arglist->sval)+1);
set_value_in_st(st,infile_index,0,0.0,arglist->sval,0);
//Open file and read invars. - just like magic
read_input_file(st);
return NULL;
}
struct ARGUMENT *fh_inuser(struct ARGUMENT *arglist)
{
char *temp;
struct ARGUMENT *ret_node;
int i;
ret_node = (struct ARGUMENT *)safe_alloc(1,sizeof(struct ARGUMENT));
temp = NULL;
i = GetInput(&temp, arglist->sval, (void *)GetCurrentThread());
ret_node->arg_type = SY_STRING_TYPE;
ret_node->next = NULL;
ret_node->ival = i;
if(temp != NULL)
{
ret_node->sval = (char *)safe_alloc(strlen(temp) +1,sizeof(char));
strcpy(ret_node->sval,temp);
delete temp;
}
else
{
ret_node->sval = (char *)safe_alloc(1,sizeof(char));
strcpy(ret_node->sval,"");
}
return(ret_node);
}
struct ARGUMENT *fh_invar(struct ARGUMENT *arglist,
struct SYTAB *st)
{
set_invar(st,arglist->ival);
return NULL;
}
struct ARGUMENT *fh_randomInt(struct ARGUMENT *arglist)
{
//generate a random integer from 1 to N.
//Function arguments:
// int maxval - the maximum returnable value
int max;
struct ARGUMENT *ret_node;
max = arglist->ival;
ret_node = (struct ARGUMENT *)safe_alloc(1,sizeof(struct ARGUMENT));
ret_node->arg_type = SY_INT_TYPE;
ret_node->ival = (rand() % max) + 1;
return(ret_node);
}
struct ARGUMENT *fh_randomFloat()
{
//Function arguments:
// none
struct ARGUMENT *ret_node;
ret_node = (struct ARGUMENT *)safe_alloc(1,sizeof(struct ARGUMENT));
ret_node->arg_type = SY_FLOAT_TYPE;
ret_node->fval = (float)(rand() % 6384) / 6384.0;
return(ret_node);
}
struct ARGUMENT *fh_sleep(struct ARGUMENT *arglist)
{
//Function arguments:
// int number of milliseconds
int dlay;
dlay = arglist->ival;
Sleep((DWORD)dlay);
return NULL;
}
struct ARGUMENT *fh_intToFloat(struct ARGUMENT *arglist)
{
//Function arguments:
// int value to be converted
struct ARGUMENT *ret_node;
ret_node = (struct ARGUMENT *)safe_alloc(1,sizeof(struct ARGUMENT));
ret_node->arg_type = SY_FLOAT_TYPE;
ret_node->fval = (float)arglist->ival;
ret_node->next = NULL;
return(ret_node);
}
struct ARGUMENT *fh_intToString(struct ARGUMENT *arglist)
{
//Function arguments:
// int value to be converted
struct ARGUMENT *ret_node;
char *temp;
ret_node = (struct ARGUMENT *)safe_alloc(1,sizeof(struct ARGUMENT));
temp = (char *)safe_alloc(512,sizeof(char));
sprintf(temp,"%ld",arglist->ival);
ret_node->sval = (char *)safe_alloc(strlen(temp)+1,sizeof(char));
ret_node->arg_type = SY_STRING_TYPE;
ret_node->next = NULL;
strcpy(ret_node->sval,temp);
safe_free(temp);
return(ret_node);
}
struct ARGUMENT *fh_stringToInt(struct ARGUMENT *arglist)
{
struct ARGUMENT *ret_node;
ret_node = (struct ARGUMENT *)safe_alloc(1,sizeof(struct ARGUMENT));
ret_node->ival = atol(arglist->sval);
ret_node->arg_type = SY_INT_TYPE;
ret_node->next = NULL;
return(ret_node);
}
struct ARGUMENT *fh_floatToString(struct ARGUMENT *arglist)
{
//Function arguments:
// float value to be converted
struct ARGUMENT *ret_node;
char *temp;
ret_node = (struct ARGUMENT *)safe_alloc(1,sizeof(struct ARGUMENT));
temp = (char *)safe_alloc(512,sizeof(char));
sprintf(temp,"%g",arglist->fval);
ret_node->sval = (char *)safe_alloc(strlen(temp)+1,sizeof(char));
ret_node->arg_type = SY_STRING_TYPE;
ret_node->next = NULL;
strcpy(ret_node->sval,temp);
safe_free(temp);
return(ret_node);
}
struct ARGUMENT *fh_floor(struct ARGUMENT *arglist)
{
//Function arguments:
// float value to be rounded down
struct ARGUMENT *ret_node;
ret_node = (struct ARGUMENT *)safe_alloc(1,sizeof(struct ARGUMENT));
ret_node->arg_type = SY_INT_TYPE;
ret_node->ival = (long)floor((double)(arglist->fval));
return(ret_node);
}
struct ARGUMENT *fh_ceiling(struct ARGUMENT *arglist)
{
//Function arguments:
// float value to be rounded up
struct ARGUMENT *ret_node;
ret_node = (struct ARGUMENT *)safe_alloc(1,sizeof(struct ARGUMENT));
ret_node->arg_type = SY_INT_TYPE;
ret_node->ival = (long)ceil((double)(arglist->fval));
return(ret_node);
}
struct ARGUMENT *fh_compare(struct ARGUMENT *arglist)
{
struct ARGUMENT *ret_node;
ret_node = (struct ARGUMENT *)safe_alloc(1,sizeof(struct ARGUMENT));
ret_node->arg_type = SY_INT_TYPE;
ret_node->ival = strcmp(arglist->sval,arglist->next->sval);
return(ret_node);
}
struct ARGUMENT *fh_copy(struct ARGUMENT *arglist,
struct SYTAB *st)
{
/* char *temp;
int length;
length = get_symbol_string_size(st,arglist->ival);
temp = (char *)safe_alloc(length, sizeof(char));
if(length < ((int)strlen(arglist->next->sval)))
strncpy(temp,arglist->next->sval, length);
else
strncpy(temp,arglist->next->sval, strlen(arglist->next->sval));
set_value_in_st(st, arglist->ival, 0, 0.0, temp, 0);
safe_free(temp);
*/
set_value_in_st(st, arglist->ival, 0, 0.0, arglist->next->sval, 0);
return NULL;
}
struct ARGUMENT *fh_concat(struct ARGUMENT *arglist,
struct SYTAB *st)
{
char *temp;
int length;
length = get_symbol_offset_value(st,arglist->ival) +
strlen(arglist->next->sval) +1;
temp = (char *)safe_alloc(length, sizeof(char));
strcpy(temp,get_symbol_string_value(st,arglist->ival));
strcat(temp,arglist->next->sval);
set_value_in_st(st, arglist->ival, 0, 0.0, temp, 0);
safe_free(temp);
return NULL;
}
struct ARGUMENT *fh_beep(struct ARGUMENT *arglist)
{
//Function arguments:
// int frequency
// int duration in milliseconds
int freq;
int dur;
freq = arglist->ival;
dur = arglist->next->ival;
// sound(freq);
// delay(dur);
// nosound();
Beep((DWORD)freq,(DWORD)dur);
return NULL;
}
struct ARGUMENT *fh_getError(struct ARGUMENT *arglist,struct SYTAB *st)
{
struct ARGUMENT *ret_node;
char *message;
int err_type;
long statusval;
long indexval;
ret_node = (struct ARGUMENT *)safe_alloc(1,sizeof(struct ARGUMENT));
err_type = GetError((unsigned long)(arglist->next->ival), &message,
&statusval, &indexval);
set_value_in_st(st, arglist->ival, 0, 0.0, message, 0);
set_value_in_st(st, arglist->next->next->ival, statusval , 0.0, "", 0);
set_value_in_st(st, arglist->next->next->next->ival, indexval, 0.0, "", 0);
ret_node->ival = err_type;
ret_node->arg_type = SY_INT_TYPE;
ret_node->next = NULL;
return(ret_node);
}
struct ARGUMENT *fh_getResponseValue(struct ARGUMENT *arglist,
struct SYTAB *st)
{
char *valuestring;
valuestring = NULL;
GetResponseValue((unsigned long)(arglist->ival), (unsigned char **)&valuestring);
set_value_in_st(st, arglist->next->ival, 0, 0.0, valuestring, 0);
if(valuestring != NULL)
delete valuestring;
return(NULL);
}
struct ARGUMENT *fh_calcCRC(struct ARGUMENT *arglist)
{
struct ARGUMENT *ret_node;
unsigned short *crc;
char hexout[5];
int length;
char *hexstr;
length = strlen(arglist->sval);
ret_node = (struct ARGUMENT *)safe_alloc(1,sizeof(struct ARGUMENT));
hexstr = (char *)safe_alloc(length/2 + 1,sizeof(char));
crc = NULL;
//first argument is a string of hex values
//convert each two-byte hex value to one byte ascii string
//call calc CRC
AsciiToHex(arglist->sval,(unsigned char *)hexstr,length);
CalcCrc((unsigned char *)hexstr, (short)(length/2), crc);
memset(hexout,'\0',5);
HexShortToAscii(crc,hexout,1);
//convert resulting two bytes to a four-byte hex string
ret_node->sval = (char *)safe_alloc(3,sizeof(char));
strcpy(ret_node->sval,hexout);
ret_node->arg_type = SY_STRING_TYPE;
ret_node->next = NULL;
safe_free(hexstr);
return(ret_node);
}
struct ARGUMENT *fh_getNodeInfo(struct ARGUMENT *arglist,
struct nodeInfo *node, char *lnn)
{
struct ARGUMENT *ret_node;
strcpy(lnn,arglist->sval);
ret_node = (struct ARGUMENT *)safe_alloc(1,sizeof(struct ARGUMENT));
if (node->syntax != NULL)
{
delete [] node->syntax;
node->syntax = NULL;
}
if (node->access != NULL)
{
delete [] node->access;
node->access = NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -