about summary refs log tree commit diff stats
path: root/numericx.c
diff options
context:
space:
mode:
authorDaniel Santos <dacs.git@brilhante.top>2022-03-17 11:56:52 +0000
committerDaniel Santos <dacs.git@brilhante.top>2022-03-17 11:56:52 +0000
commit4eb78790a98a888c347f4d499a6f3d00a08c43d6 (patch)
tree60d0f4b81b827bd826a8a35c2e72e4425b4eb7f4 /numericx.c
parent089a2a899c9daa0861e5776991244c40ec88a2fa (diff)
downloadnumericx-c-4eb78790a98a888c347f4d499a6f3d00a08c43d6.tar.gz
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>
Diffstat (limited to 'numericx.c')
-rw-r--r--numericx.c75
1 files changed, 45 insertions, 30 deletions
diff --git a/numericx.c b/numericx.c
index 56e3438..9b3b8c3 100644
--- a/numericx.c
+++ b/numericx.c
@@ -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 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 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 string of translated number, in case of success
+ * @return EINVAL if argument of result_string is not NULL.
+ * @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*
-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)
+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, char** result_string)
 {
+	/* result_string has to be NULL */
+	if( !(*result_string == NULL) )
+		return EINVAL;
 
 	/* 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);
-		return NULL;
-	}
+		return EDOM;
 
 	/* _first and _last variables */
 	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(result);
-				fprintf(stderr, "error: %s: unrepresentable void number\n", PROG_NAME);
-				return NULL;
+				return ERANGE;
 			}
+
 			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) */
-	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_numeral(counting);
 	free_numeral(result);
 
-	return result_string;
+	return EXIT_SUCCESS;
 }
 
 /* ||MAIN FUNCTION|| */
 
 /**
- * @brief Translate number.
+ * @brief CLI program.
+ *
+ * This is a command-line interface program, that uses the 'numericx_*'
+ * functions. It receives on number and returns the translated number.
  *
- * Main function that does the translation of the 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
 main(int argc, char* argv[])
@@ -567,28 +575,35 @@ main(int argc, char* argv[])
 	bool to_infinite_base = TO_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 */
-	char* result = numericx_translate(
+	char* result = NULL;
+	int status = numericx_translate(
 			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,
-			number);
+			number, &result);
 
 	/* Test for translation failure */
-	if( result == NULL )
+	switch( status )
+	{
+		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: Invalid translation\n", PROG_NAME);
+		fprintf(stderr, "error: %s: Incapable of translating.\n", PROG_NAME);
 		free(from);
 		free(to);
+		numericx_free(result);
+
 		return EXIT_FAILURE;
 	}