Modestus Moon OS  R4
CS 450 project
string.h File Reference
#include <system.h>
#include <arg_list.h>
#include <core/serial.h>
Include dependency graph for string.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define printf(format, ...)
 printf is simply a wrapper macro around sprintf with built in terminal print builtin needs to be a macro to pass on varidic function args. Uses the C Language BUILT-IT macro to use marco varidic functionability. More...
 

Functions

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 * strcpy (char *s1, const char *s2)
 strcpy copies one string to another string More...
 
char * strcat (char *s1, const char *s2)
 strcat concatenates the contents of one string onto another. More...
 
int strlen (const char *s)
 strlen returns the length of a string More...
 
int strcmp (const char *s1, const char *s2)
 strcmp compares two strings. More...
 
char * strtok (char *s1, const char *s2)
 strtok Split string into tokens. More...
 
int atoi (const char *s)
 atoi converts and ASCII string to an integer 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...
 
int sprintf (char *str, int bufLength, const char *format,...) __attribute__((format(printf
 sprintf print with format to specified string buffer More...
 

Variables

int cdecl
 

Macro Definition Documentation

#define printf (   format,
  ... 
)
Value:
{ \
char buf[500]; \
sprintf(buf, 500, format, __VA_ARGS__); \
} \
int serial_print(const char *msg)
Definition: serial.c:59
int sprintf(char *str, int bufLength, const char *format,...) __attribute__((format(printf
sprintf print with format to specified string buffer

printf is simply a wrapper macro around sprintf with built in terminal print builtin needs to be a macro to pass on varidic function args. Uses the C Language BUILT-IT macro to use marco varidic functionability.

Definition at line 112 of file string.h.

Referenced by blockPCB(), createPCB(), deletePCB(), getDate(), heapTest(), help(), helpGetVersion(), initHeap(), listTest(), mcbFunc(), pcbFunc(), pcbTest(), printAlloc(), printFree(), printList(), printMCBFunc(), printPCBFunc(), resumePCB(), setDate(), setPriority(), showAllProcesses(), showBlockedProcesses(), showPCB(), showReadyProcesses(), suspendPCB(), testPrintFunc(), and unblockPCB().

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

Referenced by getTime(), pcbTest(), and shutdownFunc().

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

Variable Documentation

int cdecl

Definition at line 103 of file string.h.