1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <syslog.h>
4 #include <unistd.h>
5 #include "ctronome.h"
6 #include "routines.h"
7 
str_dcopy(byte * source,byte * destination,byte delimiter,dword max)8 dword str_dcopy(byte *source, byte *destination, byte delimiter, dword max)
9 {
10  dword counter;
11  counter = 0;
12  while ( (source[counter] != 0) && (source[counter] != delimiter) &&
13          (counter < max) ) destination[counter] = source[counter++];
14 
15  if (source[counter] != delimiter)
16   return(0);
17  destination[counter] = 0;
18  return(counter);
19 }
20 
str_copy(byte * source,byte * destination,dword max)21 dword str_copy(byte *source, byte *destination, dword max)
22 {
23  dword counter;
24  counter = 0;
25  while ( (source[counter] != 0) &&
26          (counter < max) ) destination[counter] = source[counter++];
27  destination[counter] = 0;
28  return(counter);
29 }
30 
str_compare(byte * a,byte * b,byte delimiter)31 byte str_compare(byte *a, byte *b, byte delimiter)
32 {
33  dword i = 0;
34  while( (a[i] == b[i]) &&
35         (a[i] != 0) ) i++;
36  if( (a[i] == 0) &&
37      ( (b[i] == 0) || (b[i] == delimiter) ) )
38   return(1);
39  return(0);
40 }
41 
str_search(byte * buffer,byte search)42 int str_search(byte *buffer,byte search)
43 {
44  dword count = 0;
45  dword i = 0;
46  while((buffer[i] != 0) && (buffer[i++] != search));
47  if (buffer[--i] == search) return(i);
48  return(-1);
49 }
50 
str_replace(byte * buffer,byte search,byte replace)51 dword str_replace(byte *buffer,byte search,byte replace)
52 {
53  dword count = 0;
54  dword i = 0;
55  while(buffer[i] != 0){
56   if (buffer[i] == search){
57    buffer[i] = replace;
58    count++;
59   }
60   i++;
61  }
62  return(count);
63 }
64 
getnextline(byte * buffer,FILE * filehandle,dword max)65 dword getnextline(byte *buffer,FILE *filehandle, dword max)
66 {
67  dword length;
68  length = 0;
69  while(fread(&buffer[length],1,1,filehandle) &&
70        (buffer[length++] != '\n') &&
71        (length<max));
72 
73  if (length == max){
74     printf("line buffer overflow\n");
75     return(0);
76  }
77  buffer[length] = 0;
78  return(length);
79 }
80 
openread(byte * filename)81 FILE *openread(byte *filename)
82 {
83  FILE *filehandle;
84  if (!(filehandle = fopen(filename,"rb"))){
85   printf("cannot open %s\n",filename);
86   exit(1);
87  }
88  return(filehandle);
89 }
90 
91