numericx_translate() not printing message

* numericx_translate() only returns status codes. Instead of returning
   the resulting translated string.
 * add result_string to numericx_translate() arguments, that is the
   variable to where to store the resulting translated string.
 * for the cli program, now the message is delegated to main. Also
   changed the doxygen comments of main().

Signed-off-by: Daniel Santos <dacs.git@brilhante.top>
This commit is contained in:
Daniel Santos 2022-03-17 11:56:52 +00:00
parent 089a2a899c
commit 4eb78790a9
1 changed files with 45 additions and 30 deletions

View File

@ -447,22 +447,24 @@ numeral_to_string(numeral_ptr* numeral, bool result_with_units_on_the_end)
* @param to_first_number_void - does the translate to numerical system start counting on the second number? * @param to_first_number_void - does the translate to numerical system start counting on the second number?
* @param from_infinite_base - is the translate from numerical system first numeral infinite? For example, if first numeral is 'A', then does 'A' == 'AA' == 'AAA' == 'AAAA' ... ? * @param from_infinite_base - is the translate from numerical system first numeral infinite? For example, if first numeral is 'A', then does 'A' == 'AA' == 'AAA' == 'AAAA' ... ?
* @param to_infinite_base - is the translate to numerical system first numeral infinite? For example, if first numeral is 'A', then does 'A' == 'AA' == 'AAA' == 'AAAA' ... ? * @param to_infinite_base - is the translate to numerical system first numeral infinite? For example, if first numeral is 'A', then does 'A' == 'AA' == 'AAA' == 'AAAA' ... ?
* @param number - number of the from numerical system, to be translated into the to numerical system * @param number - number of the from numerical system, to be translated into the to numerical system.
* @param result_string - string to where to store the result.
* *
* @return NULL in case of failure * @return EINVAL if argument of result_string is not NULL.
* @return string of translated number, in case of success * @return EDOM if number doesn't belong to the from numerical system.
* @return ERANGE if the resulting number cannot be represented, because of a from void number and a lack of void number in to.
* @return EXIT_SUCCESS in case of success.
*/ */
char* int
numericx_translate(char* from, bool from_units_on_the_end, bool from_first_number_void, bool from_infinite_base, char* to, bool to_units_on_the_end, bool to_first_number_void, bool to_infinite_base, char* number) numericx_translate(char* from, bool from_units_on_the_end, bool from_first_number_void, bool from_infinite_base, char* to, bool to_units_on_the_end, bool to_first_number_void, bool to_infinite_base, char* number, char** result_string)
{ {
/* result_string has to be NULL */
if( !(*result_string == NULL) )
return EINVAL;
/* Check if number belongs to it's numerical system */ /* Check if number belongs to it's numerical system */
if( !is_valid_number(from, number) ) if( !is_valid_number(from, number) )
{ return EDOM;
fprintf(stderr, "error: %s: %s.\nValid numerals are: \"%s\"\n",
PROG_NAME, strerror(EDOM), from);
return NULL;
}
/* _first and _last variables */ /* _first and _last variables */
char* from_first = from; char* from_first = from;
@ -496,9 +498,9 @@ numericx_translate(char* from, bool from_units_on_the_end, bool from_first_numbe
{ {
free_numeral(counting); free_numeral(counting);
free_numeral(result); free_numeral(result);
fprintf(stderr, "error: %s: unrepresentable void number\n", PROG_NAME); return ERANGE;
return NULL;
} }
decrement_number_string(number, from_first, from_last, from_first); decrement_number_string(number, from_first, from_last, from_first);
} }
@ -524,21 +526,27 @@ numericx_translate(char* from, bool from_units_on_the_end, bool from_first_numbe
} }
/* result to string (result_string) */ /* result to string (result_string) */
char* result_string = numeral_to_string(result, to_units_on_the_end); *result_string = numeral_to_string(result, to_units_on_the_end);
/* free memory */ /* free memory */
free_numeral(counting); free_numeral(counting);
free_numeral(result); free_numeral(result);
return result_string; return EXIT_SUCCESS;
} }
/* ||MAIN FUNCTION|| */ /* ||MAIN FUNCTION|| */
/** /**
* @brief Translate number. * @brief CLI program.
* *
* Main function that does the translation of the number. * This is a command-line interface program, that uses the 'numericx_*'
* functions. It receives on number and returns the translated number.
*
* @param argv[] - one number as a string
*
* @return EXIT_SUCCESS in case of a successful number translation
* @return EXIT_FAILURE in case of unsuccessful number translation
*/ */
int int
main(int argc, char* argv[]) main(int argc, char* argv[])
@ -567,28 +575,35 @@ main(int argc, char* argv[])
bool to_infinite_base = TO_INFINITE_BASE; bool to_infinite_base = TO_INFINITE_BASE;
bool from_infinite_base = FROM_INFINITE_BASE; bool from_infinite_base = FROM_INFINITE_BASE;
/* Check if number belongs to it's numerical system */
if( !is_valid_number(from, number) )
{
fprintf(stderr, "error: %s: %s.\nValid numerals are: \"%s\"\n",
PROG_NAME, strerror(EDOM), from);
free(from);
free(to);
exit(EDOM);
}
/* Translation */ /* Translation */
char* result = numericx_translate( char* result = NULL;
int status = numericx_translate(
from, from_units_on_the_end, from_first_number_void, from_infinite_base, from, from_units_on_the_end, from_first_number_void, from_infinite_base,
to, to_units_on_the_end, to_first_number_void, to_infinite_base, to, to_units_on_the_end, to_first_number_void, to_infinite_base,
number); number, &result);
/* Test for translation failure */ /* Test for translation failure */
if( result == NULL ) switch( status )
{ {
fprintf(stderr, "error: %s: Invalid translation\n", PROG_NAME); case EINVAL:
fprintf(stderr, "error: %s: %s. Resulting string variable has to be NULL.\n",
PROG_NAME, strerror(EINVAL));
break;
case EDOM:
fprintf(stderr, "error: %s: %s. Valid numerals are: \"%s\".\n",
PROG_NAME, strerror(EDOM), from);
break;
case ERANGE:
fprintf(stderr, "error: %s: Unrepresentable void number.\n", PROG_NAME);
break;
}
if( !(status == EXIT_SUCCESS) )
{
fprintf(stderr, "error: %s: Incapable of translating.\n", PROG_NAME);
free(from); free(from);
free(to); free(to);
numericx_free(result);
return EXIT_FAILURE; return EXIT_FAILURE;
} }