s502 assembler
A very simple assembler for the 6502 line of processors written in C
util.c
Go to the documentation of this file.
1 #include "debugmalloc.h"
2 
3 #include <string.h>
4 #include <stdlib.h>
5 
6 #include "util.h"
7 #include "logging.h"
8 
15 char* util_find_string_segment(char* ptr) {
16  char* end = ptr;
17  while (*end != ' ' && *end != '\0') end++;
18  return end;
19 }
20 
21 int util_match_char(char a, char b) {
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 }
31 
32 int util_match_string(char* first, char* second, int count) {
33  for (int i = 0; i < count; i++) {
34  if (!util_match_char(first[i], second[i])) return 1;
35  }
36  return 0;
37 }
38 
39 char** util_split_string(char* str, int* n) {
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 }
util_split_string
char ** util_split_string(char *str, int *n)
split a string into segments on spaces
Definition: util.c:39
util_find_string_segment
char * util_find_string_segment(char *ptr)
find pointer to next space or null in string
Definition: util.c:15
util_match_char
int util_match_char(char a, char b)
Case-insensitive character compare.
Definition: util.c:21
util_match_string
int util_match_string(char *first, char *second, int count)
case-insensitive memcmp
Definition: util.c:32
logging.h
logging and fancy-printing
util.h
various utility functions