s502 assembler
A very simple assembler for the 6502 line of processors written in C
Functions
util.h File Reference

various utility functions More...

Go to the source code of this file.

Functions

char * util_find_string_segment (char *ptr)
 find pointer to next space or null in string More...
 
int util_match_char (char a, char b)
 Case-insensitive character compare. More...
 
int util_match_string (char *first, char *second, int count)
 case-insensitive memcmp More...
 
char ** util_split_string (char *str, int *n)
 split a string into segments on spaces More...
 

Detailed Description

various utility functions

Definition in file util.h.

Function Documentation

◆ util_find_string_segment()

char* util_find_string_segment ( char *  ptr)

find pointer to next space or null in string

Parameters
ptrstring to search in
Returns
address of next space or null character in string

Definition at line 15 of file util.c.

15  {
16  char* end = ptr;
17  while (*end != ' ' && *end != '\0') end++;
18  return end;
19 }

◆ util_match_char()

int util_match_char ( char  a,
char  b 
)

Case-insensitive character compare.

Parameters
afirst char to compare
bsecond char to compare
Returns
0 if the two characters match, 1 otherwise

Definition at line 21 of file util.c.

21  {
22  return a == b ||
23  (
24  (
25  (('a' <= a && a <= 'z') || ('A' <= a && a <= 'Z')) && // a is a letter
26  (('a' <= b && b <= 'z') || ('A' <= b && b <= 'Z')) // b is a letter
27  ) &&
28  (a - b == 'A' - 'a' || a - b == 'a' - 'A')
29  );
30 }

Referenced by Token::token_get_addressmode(), and util_match_string().

◆ util_match_string()

int util_match_string ( char *  first,
char *  second,
int  count 
)

case-insensitive memcmp

Parameters
counthow many bytes to match
firstfirst string to compare
secondsecond string to compare
Returns
0 if the strings match, 1 otherwise

Definition at line 32 of file util.c.

32  {
33  for (int i = 0; i < count; i++) {
34  if (!util_match_char(first[i], second[i])) return 1;
35  }
36  return 0;
37 }

References util_match_char().

Referenced by directive_compile(), and instruction_find().

◆ util_split_string()

char** util_split_string ( char *  str,
int *  n 
)

split a string into segments on spaces

Parameters
strstring to split
nint pointer to return segment count to
Returns
an array of substrings

Uses a not-so-nice trick to enable easier handling
The returned *char[] also contains a buffer with the actual substring data
This also means simply freeing it is all what is needed to be done on the caller side

Definition at line 39 of file util.c.

39  {
40  // function to split a string into substrings on spaces
41  // uses an ugly trick to avoid multiple buffers
42  // (we NEED to store a copy of the original data bc we must write 0 terminators)
43  int l = strlen(str);
44 
45  // count segments
46  int m = 1;
47  for (int i = 0; i < l; i++)
48  if (str[i] == ' ') m++;
49  *n = m;
50 
51  // the buffers stores the *char[]-s and then the copy of the data
52  char** r = malloc(sizeof(char*) * m + l + 1);
53 
54  char* buf = (char*)r + sizeof(char*) * m;
55  strcpy(buf, str);
56 
57  // first segemtn is the begining of the string
58  r[0] = buf;
59  // fill the table and write 0 terminators
60  int j = 1;
61  for (int i = 0; i < l; i++)
62  if (str[i] == ' ') {
63  buf[i] = 0;
64  r[j++] = buf + i + 1;
65  }
66  return r;
67 }

Referenced by compile_data(), and compile_pad().

util_match_char
int util_match_char(char a, char b)
Case-insensitive character compare.
Definition: util.c:21