summaryrefslogtreecommitdiff
path: root/lib/str2int.c
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2025-03-25 17:36:52 -0400
committerFreya Murphy <freya@freyacat.org>2025-03-25 17:38:22 -0400
commit6af21e6a4f2251e71353562d5df7f376fdffc270 (patch)
treede20c7afc9878422c81e34f30c6b010075e9e69a /lib/str2int.c
downloadcomus-6af21e6a4f2251e71353562d5df7f376fdffc270.tar.gz
comus-6af21e6a4f2251e71353562d5df7f376fdffc270.tar.bz2
comus-6af21e6a4f2251e71353562d5df7f376fdffc270.zip
initial checkout from wrc
Diffstat (limited to 'lib/str2int.c')
-rw-r--r--lib/str2int.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/str2int.c b/lib/str2int.c
new file mode 100644
index 0000000..c0f777d
--- /dev/null
+++ b/lib/str2int.c
@@ -0,0 +1,51 @@
+/**
+** @file str2int.c
+**
+** @author Numerous CSCI-452 classes
+**
+** @brief C implementations of common library functions
+*/
+
+#ifndef STR2INT_SRC_INC
+#define STR2INT_SRC_INC
+
+#include <common.h>
+
+#include <lib.h>
+
+/**
+** str2int(str,base) - convert a string to a number in the specified base
+**
+** @param str The string to examine
+** @param base The radix to use in the conversion
+**
+** @return The converted integer
+*/
+int str2int( register const char *str, register int base ) {
+ register int num = 0;
+ register char bchar = '9';
+ int sign = 1;
+
+ // check for leading '-'
+ if( *str == '-' ) {
+ sign = -1;
+ ++str;
+ }
+
+ if( base != 10 ) {
+ bchar = '0' + base - 1;
+ }
+
+ // iterate through the characters
+ while( *str ) {
+ if( *str < '0' || *str > bchar )
+ break;
+ num = num * base + *str - '0';
+ ++str;
+ }
+
+ // return the converted value
+ return( num * sign );
+}
+
+#endif