Modestus Moon OS  R4
CS 450 project
string.c File Reference
#include <system.h>
#include <string.h>
Include dependency graph for string.c:

Go to the source code of this file.

Functions

int strlen (const char *s)
 strlen returns the length of a string More...
 
char * strcpy (char *s1, const char *s2)
 strcpy copies one string to another string More...
 
int atoi (const char *s)
 atoi converts and ASCII string to an integer More...
 
int strcmp (const char *s1, const char *s2)
 strcmp compares two strings. More...
 
char * strcat (char *s1, const char *s2)
 strcat concatenates the contents of one string onto another. More...
 
int isspace (const char *c)
 isspace Determines if a character is a whitespace. More...
 
void * memset (void *s, int c, size_t n)
 memset Set a region of memory. More...
 
char * strtok (char *s1, const char *s2)
 strtok Split string into tokens. More...
 
int sprintf (char *str, int bufLength, const char *format,...)
 sprintf print with format to specified string buffer More...
 
int intToS (const int *const i, char *buf, int bufLength)
 intToS converts a signed integer to string More...
 
char is_conversion_specifier (char c)
 is_conversion_specifier checks to see if the character is one of the standard printf formats More...
 
int isnum (const char c)
 isnum inline helper function to check if a character is represents an ascii number More...
 

Function Documentation

int atoi ( const char *  s)

atoi converts and ASCII string to an integer

Parameters
sconst char *s, character pointer to a string.
Returns
the integer value of s.

Definition at line 46 of file string.c.

Referenced by createPCB(), setDate(), setPriority(), setTime(), and sprintf().

47 {
48  int num = 0;
49 
50  if(!s)
51  {
52  return 0;
53  }
54  while(*s==' '){ s++; }
55 
56  int isNeg = ( *s == '-');
57  if(isNeg || (*s == '+')){ s++; }
58  while((*s >= '0') && (*s <= '9' ))
59  {
60  num = (num*10) + (*s - '0');
61  s++;
62  }
63  if(isNeg)
64  {
65  num = num *(-1);
66  }
67 
68  return num; // return integer
69 }
int intToS ( const int *const  i,
char *  buf,
int  bufLength 
)

intToS converts a signed integer to string

Parameters
iinteger to convert
bufbuf to place the converted string
bufLengthlength of the buffer, string writing will not exceed this value
Returns
num of characters written to buffer

Definition at line 317 of file string.c.

References strlen().

Referenced by sprintf().

318 {
319  if(!buf)
320  {
321  return 0;
322  }
323 
324  int cpy=*i;
325  int length = 1;
326  if(*i < 0)
327  {
328  cpy*=-1;
329  length++;
330  }
331 
332  do{
333  length++;
334  }while((cpy/=10)); //find total length of string
335 
336  if(bufLength<length+1) //if the bufLength is less than the length+null term, return 0
337  {
338  return 0;
339  }
340 
341  cpy=(*i<0)?(*i) * -1: *i; //reset val copy
342  length--;
343  buf[length]='\0'; //insert null terminator at end of string
344  length--;
345  for(; length >= 0; length--) //convert int to char and copy into buffer backwards
346  {
347  buf[length] = cpy%10 + '0';
348  cpy/=10;
349  }
350  if(*i<0)
351  {
352  buf[0]='-';
353  }
354 
355  return strlen(buf);
356 }
int strlen(const char *s)
strlen returns the length of a string
Definition: string.c:10
char is_conversion_specifier ( char  c)

is_conversion_specifier checks to see if the character is one of the standard printf formats

Parameters
ccharacter to check
Returns
returns the character if the character is a format specifier, else returns 0

Definition at line 363 of file string.c.

Referenced by sprintf().

364 {
365  switch(c)
366  {
367  case 'c':
368  case 's':
369  case 'd': case 'i':
370  case 'u': case 'o': case 'x': case 'X':
371  case 'f': case 'F': case 'e': case 'E': case 'a': case'A': case 'g': case 'G':
372  case 'p':
373  return c;
374 
375  default: return 0;
376  }
377  return 0;
378 }
int isnum ( const char  c)
inline

isnum inline helper function to check if a character is represents an ascii number

Parameters
ccharacter to check
Returns
returns 1 if the character is an ascii number, else returns 0

Definition at line 385 of file string.c.

Referenced by sprintf().

386 {
387  return ((c >= '0') && (c <= '9'));
388 }
int isspace ( const char *  c)

isspace Determines if a character is a whitespace.

Parameters
ccharacter to check.
Returns
1 if it is a whitespace, 0 if it is not a whitespace.

Definition at line 114 of file string.c.

115 {
116  if (*c == ' ' ||
117  *c == '\n' ||
118  *c == '\r' ||
119  *c == '\f' ||
120  *c == '\t' ||
121  *c == '\v'){
122  return 1;
123  }
124  return 0;
125 }
void* memset ( void *  s,
int  c,
size_t  n 
)

memset Set a region of memory.

Parameters
sdestination.
cbyte to write.
ncount.
Returns
void.

Definition at line 139 of file string.c.

Referenced by init_idt(), init_paging(), kmain(), and loadr3().

140 {
141  unsigned char *p = (unsigned char *) s;
142  while(n--){
143  *p++ = (unsigned char) c;
144  }
145  return s;
146 }
int sprintf ( char *  str,
int  bufLength,
const char *  format,
  ... 
)

sprintf print with format to specified string buffer

Parameters
stra char *, place where the built string will be placed
bufLengththe size of the buffer of the char *, string writing will not exceed this value
formata format string confirming to the std library format
...any number of parameters that will be printed according to the format. if wrong type is specified, behavior is undefined
Returns
the number of characters placed in the output buffer plus the null character

Definition at line 229 of file string.c.

References arg_list, atoi(), init_arg_list, intToS(), is_conversion_specifier(), isnum(), strcpy(), and strlen().

230 {
231 
232  if(!str || bufLength <= 0) {
233  return 0;
234  }
235 
236  arg_list list;
237  init_arg_list(list, format);
238 
239  int newStrIndex = 0;
240  int index, formatLength = strlen(format);
241  for(index = 0; format[index] != '\0' && index <= formatLength; index++)
242  {
243 
244  //flag is -1 until fulfilled, flag is 0 if not requested
245  if(format[index] == '%')
246  {
247  char leftJustified=0, alwaysAppendSign=0, subSignWithSpace=0, altForm=0, padWithZeros=0;
248  int minFieldWidth=0, precision=0;
249  (void)padWithZeros;
250  for(; !is_conversion_specifier(format[index]); index++)
251  {
252  if(format[index]=='\0')
253  {
254  return 0; //if we reach the end of the string, exit as the format is missing
255  }
256  if(!minFieldWidth && !precision)
257  {
258  switch(format[index])
259  {
260  case '-': leftJustified=1; break;
261  case '+': if(!leftJustified) {alwaysAppendSign=1;} break;
262  case ' ': if(!leftJustified && !alwaysAppendSign){subSignWithSpace=1;} break;
263  case '#': if(!leftJustified && !alwaysAppendSign && !subSignWithSpace) {altForm=1;} break;
264  case '0': if(!leftJustified && !alwaysAppendSign && !subSignWithSpace && !altForm){padWithZeros=1;} break;
265  }
266  }
267  if(format[index] == '.'){ precision = -1;}
268  if(isnum(format[index])) //implement the * modifier here
269  {
270  if(precision==-1)
271  {
272  precision = atoi(&format[index]);
273  }
274  else if(!minFieldWidth)
275  {
276  minFieldWidth = atoi(&format[index]);
277  }
278  }
279  }//finished parsing one conversion
280  //need to implement all modifiers
281  if(format[index]=='d')
282  {
283  newStrIndex += intToS((int*)next_arg_in_list(&list, &format[index], 0), &str[newStrIndex], bufLength-newStrIndex);
284  }
285  else if(format[index]=='c')
286  {
287  str[newStrIndex]= **((char**)next_arg_in_list(&list, &format[index], 0));
288  newStrIndex++;
289  }
290  else if(format[index]=='s')
291  {
292  char *strToCopy = *((char**)next_arg_in_list(&list, &format[index], 0));
293  int lengthOfCopy = strlen(strToCopy);
294  strcpy(&str[newStrIndex], strToCopy);
295  newStrIndex += lengthOfCopy;
296  }
297  }
298  else
299  {
300  str[newStrIndex] = format[index];
301  newStrIndex++;
302  }
303  }
304  str[newStrIndex]=0;
305  return newStrIndex;
306 }
int intToS(const int *const i, char *buf, int bufLength)
intToS converts a signed integer to string
Definition: string.c:317
int atoi(const char *s)
atoi converts and ASCII string to an integer
Definition: string.c:46
#define init_arg_list(argList, argBeforeEllipses)
implemented in macro because there are no templates in C
Definition: arg_list.h:25
char is_conversion_specifier(char c)
is_conversion_specifier checks to see if the character is one of the standard printf formats ...
Definition: string.c:363
#define arg_list
Definition: arg_list.h:19
int strlen(const char *s)
strlen returns the length of a string
Definition: string.c:10
int isnum(const char c)
isnum inline helper function to check if a character is represents an ascii number ...
Definition: string.c:385
char * strcpy(char *s1, const char *s2)
strcpy copies one string to another string
Definition: string.c:26
char* strcat ( char *  s1,
const char *  s2 
)

strcat concatenates the contents of one string onto another.

Parameters
s1destination string.
s2source string.
Returns
The combined string s1 and s2.

Definition at line 101 of file string.c.

Referenced by klogv(), and kpanic().

102 {
103  char *rc = s1;
104  if (*s1) while(*++s1);
105  while( (*s1++ = *s2++) );
106  return rc;
107 }
int strcmp ( const char *  s1,
const char *  s2 
)

strcmp compares two strings.

Parameters
s1string 1 to compare.
s2string 2 to compare.
Returns
if the two strings are equal. If they are equal it will return 0, otherwise will return a non-zero integer.

Definition at line 77 of file string.c.

Referenced by date(), exec_comm(), helpDate(), helpFunc(), helpMCB(), helpPcb(), helpTime(), isEmpty(), mcbFunc(), pcbFunc(), pcbSearchFunc(), searchcompFunction(), shutdownFunc(), stringToClass(), and time().

78 {
79 
80  // Remarks:
81  // 1) If we made it to the end of both strings (i. e. our pointer points to a
82  // '\0' character), the function will return 0
83  // 2) If we didn't make it to the end of both strings, the function will
84  // return the difference of the characters at the first index of
85  // indifference.
86  int i = 0;
87  for(; (s1[i]) && (s2[i]) && (s1[i]==s2[i]); i++)
88  {
89  }
90 
91  return s1[i]-s2[i];
92 }
char* strcpy ( char *  s1,
const char *  s2 
)

strcpy copies one string to another string

Parameters
s1destination string. Character pointer to a string.
s2source string. Character pointer to a string.
Returns
return s1.

Definition at line 26 of file string.c.

Referenced by parse_comm(), serial_poll(), setupPCB(), and sprintf().

27 {
28  char* s1Cpy = s1;
29  while(*s2 != '\0') {
30  *s1 = *s2;
31  s1++;
32  s2++;
33  }
34  if(*s2 == '\0') {
35 
36  *s1 = *s2;
37  }
38  return s1Cpy; // return pointer to destination string
39 }
int strlen ( const char *  s)

strlen returns the length of a string

Parameters
scharacter pointer to a string
Returns
the length of the string

Definition at line 10 of file string.c.

Referenced by helpDate(), helpTime(), intToS(), setDate(), setTime(), setupPCB(), and sprintf().

11 {
12  int length = 0;
13  while(s[length] != '\0') {
14  length++;
15  }
16  //length++;
17  return length; // return length of string
18 }
char* strtok ( char *  s1,
const char *  s2 
)

strtok Split string into tokens.

Parameters
s1String to split
s2Delimeter.
Returns
String split into delimeters.

Definition at line 154 of file string.c.

References NULL.

Referenced by parse_comm().

155 {
156  static char *tok_tmp = NULL;
157  const char *p = s2;
158 
159  //new string
160  if (s1!=NULL){
161  tok_tmp = s1;
162  }
163  //old string cont'd
164  else {
165  if (tok_tmp==NULL){
166  return NULL;
167  }
168  s1 = tok_tmp;
169  }
170 
171  //skip leading s2 characters
172  while ( *p && *s1 ){
173  if (*s1==*p){
174  ++s1;
175  p = s2;
176  continue;
177  }
178  ++p;
179  }
180 
181  //no more to parse
182  if (!*s1){
183  return (tok_tmp = NULL);
184  }
185 
186  //skip non-s2 characters
187  tok_tmp = s1;
188  while (*tok_tmp){
189  p = s2;
190  while (*p){
191  if (*tok_tmp==*p++){
192  *tok_tmp++ = '\0';
193  return s1;
194  }
195  }
196  ++tok_tmp;
197  }
198 
199  //end of string
200  tok_tmp = NULL;
201  return s1;
202 }
#define NULL
Definition: system.h:4