File: common.c

package info (click to toggle)
teg 0.11.2%2Bdebian-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 11,484 kB
  • ctags: 7,112
  • sloc: ansic: 16,775; sh: 8,559; python: 1,442; xml: 490; makefile: 399
file content (161 lines) | stat: -rw-r--r-- 3,084 bytes parent folder | download | duplicates (4)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*	$Id: common.c,v 1.29 2004/08/04 13:03:06 riq Exp $	*/
/* Tenes Empanadas Graciela
 *
 * Copyright (C) 2000 Ricardo Quesada
 *
 * Author: Ricardo Calixto Quesada <rquesada@core-sdi.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; only version 2 of the License
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */
/**
 * @file common.c
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */

#include "all.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>

char *g_colores[] = {
	N_("red"),
	N_("yellow"),
	N_("blue"),
	N_("black"),
	N_("pink"),
	N_("green"),
	N_("n/a")			/* color is not assigned yet */
};


/* XXX sync with common.h */
char *g_reglas[] = {
	N_("TEG"),
	N_("Risk"),
	N_("1914"),
	N_("other"),
};

/* XXX: sync these status with the one in common.h */
char *g_estados[] = {
	N_("disconnected"),
	N_("connected"),
	N_("game over"),
	N_("enabled"),
	N_("started"),
	N_("placing armies"),
	N_("postarmies"),
	N_("placing armies 2"),
	N_("postarmies 2"),
	N_("idle"),
	N_("placing armies 3"),
	N_("exchanging cards"),
	N_("postarmies 3"),
	N_("making a pact"),
	N_("starting turn"),
	N_("attacking"),
	N_("moving armies"),
	N_("regrouping"),
	N_("gettting card"),
	N_("ending turn")
};

/* returns an integer from /dev/random or 0 if it can't */
int get_int_from_dev_random( void )
{
	int fd;
	char buf[sizeof(int)];
	int l;
	int *ret;

	fd = open( RANDOM_DEVICE, O_RDONLY);
	if( fd < 0) {
		fprintf(stderr,"Couldn't open '%s'\n", RANDOM_DEVICE);
		return 0;
	}
	l = read( fd, buf, sizeof(buf));
	if( l != sizeof(buf) )
		fprintf(stderr,"Returning a not so random number. Read: %d\n",l);

	ret = (int *)&buf;

	close(fd);

	return *ret;
}

/* given the number of exchange, it says the numer of armies he deserves */
int cuantos_x_canje( int c )
{
	if( c < 1 )
		return 0;
	switch( c ) {
		case 0:
			return 0;
		case 1:
			return 4;
		case 2:
			return 7;
		case 3:
			return 10;
		default:
			return (c-1) * 5;
	}

}

TEG_STATUS strip_invalid( char *n )
{
	int l = strlen(n);
	int i;

	assert(n);

	for(i=0;i<l;i++) {
		if( n[i]=='=' || n[i]==';' || n[i]=='\\' || n[i]==',' || n[i]==':' || n[i]=='/' || n[i]=='%')
			n[i] = '.';
	}

	return TEG_STATUS_SUCCESS;
}

TEG_STATUS strip_invalid_msg( char *n )
{
	int l = strlen(n);
	int i;

	assert(n);

	for(i=0;i<l;i++) {
		if( n[i]=='"' )
			n[i]='\'';
	}

	return TEG_STATUS_SUCCESS;
}

int my_atoi( char *s )
{
	if( ! s )
		return -1;
	return atoi( s );
}