aboutsummaryrefslogtreecommitdiff
path: root/util.c
blob: 20e8290cbfdc6606aa1e8857b3f4f0ddc638062b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

static void replace(char a[], char b[], int x, int y);

#define POOLSIZE 100000
static int ipool[POOLSIZE];
static int *ipoolp = ipool;

int* get_intarray(int size)
{
 if(ipoolp + size > ipool + POOLSIZE) ipoolp = ipool;
 int* ret = ipoolp;
 ipoolp += size;
 return ret;
}

static char cpool[POOLSIZE];
static char *cpoolp = cpool;

char* get_chararray(int size)
{
 if(cpoolp + size > cpool + POOLSIZE) cpoolp = cpool;
 char* ret = cpoolp;
 cpoolp += size;
 return ret;
}

/* Concatinates a and b and returns a different
   string. Only use when returned string can be recycled. */
char* cat(char a[], char b[]) {
 int len = strlen(a) + strlen(b) + 1;
/* if(len > RET) return "error";
 if(len + retindex > RET) retindex = 0;
 char* p = ret + retindex;
*/
 char *p = get_chararray(len);
 int i, j;
 i = j = 0;
 while((p[i++] = a[j++]) != '\0');
 i--;
 j = 0;
 while((p[i++] = b[j++]) != '\0');
 return p;
}

char* itoa(int i)
{
 char *num = get_chararray(5);
 int j;
 for(j = 0; j < 5; num[j++] = ' ');
 num[3] = '0';
 int o = 3;
 int negative = 0;
 if(i < 0) {
  negative = 1;
  i *= -1;
 }
 while(o >= 0 && i > 0) {
  num[o--] = i % 10 + '0';
  i /= 10;
 }
 if(o >= 0 && negative) num[o] = '-';
 num[4] = '\0';
 for(i = 0; num[i] == ' '; i++);
 return &num[i];
}

//sleep in tenths of a second
int bettersleep(int ds)
{
 struct timespec tim, tim2;
 tim.tv_sec = ds/10;
 tim.tv_nsec = (ds%10)*100000000L;
 return nanosleep(&tim , &tim2);
}
bgstack15