📄 mel.c
字号:
int i;
for (i = 0; i < ref_str_len; i++)
if (ref_str[i] != test_str[i]) return 0;
return 1;
}
/*
---------------------------------------------------------------------------
module 1.2.3
parameters usually are part of a descriptor. get the next parameter name,
if any, and find out which parameter it is. note that MEL also allows
parameter values to be input, without their names, assuming a certain order.
algorithm synopsis: copy next parameter starting at current position
in descrip_str. if unrecognizable, it may be a value (parameter name
assumed) so allow for this possibility.
---------------------------------------------------------------------------
*/
static int get_param_name()
{
int i;
char temp_str[MELI_MAX_DESCRIP_STR_LEN+1];
char *old_descrip_str_ptr;
/* see if the maximum number of descriptors have already been read: */
/* (must be able to read another or at end of descriptor string.) */
/* this test prevents too many parameters from being read. */
if ((meli_datum.num_param >= meli_descrip[meli_datum.descrip_type].
max_num_param) && (*descrip_str_ptr != '\0')) {
mel_err.type = mel_too_many_param_err;
mel_err.start_line = meli_datum.start_line;
mel_err.end_line = meli_datum.end_line;
strcpy(mel_err.msg, descrip_str);
return mel_too_many_param_err;
};
/* create string to next comma, equal's sign, semicolon, or null terminator.
however, since this may be a value and not a parameter name, remember
the old string posn just in case: */
old_descrip_str_ptr = descrip_str_ptr;
for (i = 0; i < MELI_MAX_DESCRIP_STR_LEN; i++) {
if ((*descrip_str_ptr == ',') ||
(*descrip_str_ptr == ';') ||
(*descrip_str_ptr == '\0')) {
equals_sign_found_flag = 0;
descrip_str_ptr++;
temp_str[i] = '\0';
break;
} else if (*descrip_str_ptr == '=') {
equals_sign_found_flag = 1;
descrip_str_ptr++;
temp_str[i] = '\0';
break;
};
temp_str[i] = *descrip_str_ptr++;
}
/* find the parameter name that matches this built up name (assuming
it is a name): */
for (i = 0; i < meli_descrip[curr_descrip_index].max_num_param; i++)
if (name_match(meli_descrip[curr_descrip_index].param[i].name,
temp_str, meli_descrip[curr_descrip_index].param[i].min_name_len)) {
curr_param_index = i;
param_name_found_flag = 1;
meli_datum.param[datum_param_index].name_index = i;
return 0; /* the parameter's name has been found! */
};
/* if we get here then we went through the entire list without finding
a match. however, this may not be an error for the case of
parameter values being input in order (without their names): */
/* if an equal's sign was found, then this is definitely an error: */
if (equals_sign_found_flag) {
mel_err.type = mel_unknown_param_name_err;
mel_err.start_line = meli_datum.start_line;
mel_err.end_line = meli_datum.end_line;
strcpy(mel_err.msg, "Or misplaced equal's sign");
return mel_unknown_param_name_err;
};
/* in order to avoid confusion, once a parameter has been found, make it
no longer legal to enter just parameter values alone: */
if (param_name_found_flag) {
mel_err.type = mel_missing_param_name_err;
mel_err.start_line = meli_datum.start_line;
mel_err.end_line = meli_datum.end_line;
strcpy(mel_err.msg, "Or unrecognizable parameter name");
return mel_missing_param_name_err;
};
/* prepare for the next routine to read the next parameter value in
order: */
curr_param_index = datum_param_index;
meli_datum.param[datum_param_index].name_index = curr_param_index;
equals_sign_found_flag = 1; /* fake it */
descrip_str_ptr = old_descrip_str_ptr;
/* restore pointer so that value may be read by next routine. */
return 0;
}
/*
---------------------------------------------------------------------------
module 1.2.4
a parameter's value follows a parameter's name. read the value(s), if any,
and store them.
---------------------------------------------------------------------------
*/
static int get_param_values()
{
int i;
char temp_str[MELI_MAX_DESCRIP_STR_LEN+1];
enum mel_data_types data_type;
int result;
int curr_temp_posn;
int curr_array_index = 0;
int end_of_temp_str;
int single_quote_found_flag = 0;
/* create string to next comma, semicolon, or null terminator:
(an exception is to allow a comma or semicolon within a single-
quoted substring.) note: string may include array of values in
{brackets}. */
for (i = 0; i < MELI_MAX_DESCRIP_STR_LEN; i++) {
if (*descrip_str_ptr == '\'')
single_quote_found_flag = !single_quote_found_flag;
if (single_quote_found_flag) {
if (*descrip_str_ptr == '\0') {
left_parenthesis_found_flag = 0;
descrip_str_ptr++;
temp_str[i] = '\0';
break;
};
} else if ((*descrip_str_ptr == ',') ||
(*descrip_str_ptr == ';') ||
(*descrip_str_ptr == '\0')) {
left_parenthesis_found_flag = 0;
descrip_str_ptr++;
temp_str[i] = '\0';
break;
} else if (*descrip_str_ptr == '(') {
left_parenthesis_found_flag = 1;
descrip_str_ptr++;
temp_str[i] = '\0';
break;
};
temp_str[i] = *descrip_str_ptr++;
}
/* first see if the value of the parameter is "unknown": */
if (!strcmp(temp_str, "unknown")) {
meli_datum.param[datum_param_index].unknown_flag = 1;
return 0;
} else meli_datum.param[datum_param_index].unknown_flag = 0;
/* scan the string and interpret its value(s) (depending on which kind of
parameter this value belongs to): */
data_type = meli_descrip[curr_descrip_index].param[curr_param_index].type;
switch (data_type) {
case (mel_bool): /* read true or false (default is true) */
if ((temp_str[0] == '\0') || (temp_str[0] != 'f'))
meli_datum.param[datum_param_index].data.integer = 1;
else
meli_datum.param[datum_param_index].data.integer = 0;
result = 1; /* successful "reading" of value */
break;
case (mel_int): /* read an integer */
result = sscanf(temp_str, "%d",
&meli_datum.param[datum_param_index].data.integer);
break;
case (mel_real): /* read a real */
result = sscanf(temp_str, "%lf",
&meli_datum.param[datum_param_index].data.real);
break;
case (mel_str): /* read a string (may be surrounded by single
quotes*/
/* string MUST have single quotes if has blanks: */
if (temp_str[0] == '\'')
result = sscanf(&temp_str[1], "%[^']",
meli_datum.param[datum_param_index].data.string);
else
result = sscanf(temp_str, "%s",
meli_datum.param[datum_param_index].data.string);
break;
case (mel_int_array):
/* get ready to read numbers in array: */
end_of_temp_str = strlen(temp_str);
/* first make sure first and last characters in string were
curly brackets and adjust string length to get rid of them: */
if ((temp_str[0] != '{') || (temp_str[end_of_temp_str-1] != '}')) {
mel_err.type = mel_missing_bracket_err;
mel_err.start_line = meli_datum.start_line;
mel_err.end_line = meli_datum.end_line;
strncpy(mel_err.msg, temp_str, MEL_MAX_ERR_MSG_LEN);
mel_err.msg[MEL_MAX_ERR_MSG_LEN+1] = '\0';
return mel_missing_bracket_err;
};
temp_str[--end_of_temp_str] = '\0'; /* last char was bracket */
while (temp_str[end_of_temp_str - 1] == ' ')
temp_str[--end_of_temp_str] = '\0'; /* flush trailing blanks */
curr_temp_posn = 1; /* first char was curly bracket */
/* now read in values: */
result = 1; /* assume a good start */
while ((curr_temp_posn < end_of_temp_str) &&
(result != 0) && (result != EOF)) {
/* be careful not to overflow array: */
if (curr_array_index > MELI_MAX_PARAM_INT_ARRAY_LEN)
break;
/* purge till first non-blank character: */
for (; curr_temp_posn < end_of_temp_str; curr_temp_posn++)
if (temp_str[curr_temp_posn] != ' ') break;
/* read next integer into array: */
result = sscanf(&temp_str[curr_temp_posn], "%d",
&meli_datum.param[datum_param_index].
data.integer_array[curr_array_index++]);
/* purge till first blank character: */
for (; curr_temp_posn < end_of_temp_str; curr_temp_posn++)
if (temp_str[curr_temp_posn] == ' ') break;
};
break;
case (mel_real_array):
/* get ready to read numbers in array: */
end_of_temp_str = strlen(temp_str);
/* first make sure first and last characters in string were
curly brackets and adjust string length to get rid of them: */
if ((temp_str[0] != '{') || (temp_str[end_of_temp_str-1] != '}')) {
mel_err.type = mel_missing_bracket_err;
mel_err.start_line = meli_datum.start_line;
mel_err.end_line = meli_datum.end_line;
strncpy(mel_err.msg, temp_str, MEL_MAX_ERR_MSG_LEN);
mel_err.msg[MEL_MAX_ERR_MSG_LEN+1] = '\0';
return mel_missing_bracket_err;
};
temp_str[--end_of_temp_str] = '\0'; /* last char was bracket */
while (temp_str[end_of_temp_str - 1] == ' ')
temp_str[--end_of_temp_str] = '\0'; /* flush trailing blanks */
curr_temp_posn = 1; /* first char is curly bracket */
result = 1;
while ((curr_temp_posn < end_of_temp_str) &&
(result != 0) && (result != EOF)) {
/* be careful not to overflow array: */
if (curr_array_index > MELI_MAX_PARAM_REAL_ARRAY_LEN)
break;
/* purge till first non-blank character: */
for (; curr_temp_posn < end_of_temp_str; curr_temp_posn++)
if (temp_str[curr_temp_posn] != ' ') break;
/* read next real into array: */
result = sscanf(&temp_str[curr_temp_posn], "%lf",
&meli_datum.param[datum_param_index].
data.real_array[curr_array_index++]);
/* purge till first blank character: */
for (; curr_temp_posn < end_of_temp_str; curr_temp_posn++)
if (temp_str[curr_temp_posn] == ' ') break;
};
break;
case (mel_str_array):
/* get ready to read string into array: */
end_of_temp_str = strlen(temp_str);
/* first make sure first and last characters in string were
curly brackets and adjust string length to get rid of them: */
if ((temp_str[0] != '{') || (temp_str[end_of_temp_str-1] != '}')) {
mel_err.type = mel_missing_bracket_err;
mel_err.start_line = meli_datum.start_line;
mel_err.end_line = meli_datum.end_line;
strncpy(mel_err.msg, temp_str, MEL_MAX_ERR_MSG_LEN);
mel_err.msg[MEL_MAX_ERR_MSG_LEN+1] = '\0';
return mel_missing_bracket_err;
};
temp_str[--end_of_temp_str] = '\0'; /* last char was bracket */
while (temp_str[end_of_temp_str - 1] == ' ')
temp_str[--end_of_temp_str] = '\0'; /* flush trailing blanks */
curr_temp_posn = 1; /* first char is curly bracket */
result = 1;
while ((curr_temp_posn < end_of_temp_str) &&
(result != 0) && (result != EOF)) {
/* be careful not to overflow array: */
if (curr_array_index > MELI_MAX_PARAM_STR_ARRAY_LEN)
break;
/* purge till first non-blank character: */
for (; curr_temp_posn < end_of_temp_str; curr_temp_posn++)
if (temp_str[curr_temp_posn] != ' ') break;
/* read next real into array (remember 1st char may be
single quote, so two possibilites to consider): */
if (temp_str[curr_temp_posn] == '\'') {
result = sscanf(&temp_str[++curr_temp_posn], "%[^']",
meli_datum.param[datum_param_index].
data.string_array[curr_array_index++]);
/* purge till first char after next single quote: */
for (; curr_temp_posn < end_of_temp_str; curr_temp_posn++)
if (temp_str[curr_temp_posn] == '\'') break;
curr_temp_posn++;
} else {
result = sscanf(&temp_str[curr_temp_posn], "%s",
meli_datum.param[datum_param_index].
data.string_array[curr_array_index++]);
/* purge till next blank character: */
for (; curr_temp_posn < end_of_temp_str; curr_temp_posn++)
if (temp_str[curr_temp_posn] == ' ') break;
}
};
break;
}
meli_datum.param[datum_param_index].array_len = curr_array_index;
/* check to see if read was successful: */
if ((result == 0) || (result == EOF)) {
mel_err.type = mel_param_data_err;
mel_err.start_line = meli_datum.start_line;
mel_err.end_line = meli_datum.end_line;
strncpy(mel_err.msg, temp_str, MEL_MAX_ERR_MSG_LEN);
mel_err.msg[MEL_MAX_ERR_MSG_LEN+1] = '\0';
return mel_param_data_err;
};
return 0;
}
/*
---------------------------------------------------------------------------
module 1.2.5
sometimes the units associated with parameter data may change:
---------------------------------------------------------------------------
*/
static int get_param_units()
{
int i;
char temp_str[MELI_MAX_DESCRIP_STR_LEN+1];
/* are there any units to get? */
/* if not then return null string for units. */
if (!left_parenthesis_found_flag) {
strcpy(meli_datum.param[datum_param_index].units, "");
return 0;
}
/* create temporary string containing units: */
for (i = 0; i < MELI_MAX_DESCRIP_STR_LEN; i++) {
if (*descrip_str_ptr == ')') {
descrip_str_ptr++;
if (*descrip_str_ptr == ',' ||
*descrip_str_ptr == ';') descrip_str_ptr++;
/* normally, a comma or semicolon should follow the units.
lets be forgiving in case there isn't and not just flush
anything. */
temp_str[i] = '\0';
break;
} else if (*descrip_str_ptr == '\0') {
mel_err.type = mel_missing_paren_err;
mel_err.start_line = meli_datum.start_line;
mel_err.end_line = meli_datum.end_line;
temp_str[i] = '\0';
strncpy(mel_err.msg, temp_str, MEL_MAX_ERR_MSG_LEN);
mel_err.msg[MEL_MAX_ERR_MSG_LEN+1] = '\0';
return mel_missing_paren_err;
};
temp_str[i] = *descrip_str_ptr++;
}
/* copy this string to units area: */
temp_str[MELO_UNITS_STR_LEN+1] = '\0';
strcpy(meli_datum.param[datum_param_index].units, temp_str);
return 0;
}
/*
---------------------------------------------------------------------------
module 2.0
extract the type of descriptor that has been input.
see mel.doc for more information on using this procedure.
---------------------------------------------------------------------------
*/
char *meli_descrip_type()
{
return meli_descrip[meli_datum.descrip_type].name;
}
/*
---------------------------------------------------------------------------
module 3.0
extract the number of parameters that has been input.
see mel.doc for more information on using this procedure.
---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -