better compilation on host
[aversive.git] / include / aversive / pgmspace.h
1 /*  
2  *  Copyright Droids Corporation, Microb Technology, Eirbot (2005)
3  * 
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Revision : $Id: pgmspace.h,v 1.1.2.4 2007-11-21 21:54:38 zer0 Exp $
19  *
20  */
21
22 /**
23  * This file is used for compatibility between host and avr : with
24  * this we can emulate pgmspace on a host.
25  */
26
27 #ifndef _AVERSIVE_PGMSPACE_H_
28 #define _AVERSIVE_PGMSPACE_H_
29
30 #ifndef HOST_VERSION
31
32 #include <avr/pgmspace.h>
33 #define PGMS_FMT "%S"
34
35
36 #else
37
38 #include <stdint.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <stdarg.h>
42
43 typedef void prog_void;
44 typedef char prog_char;
45 typedef unsigned char prog_uchar;
46 typedef int8_t prog_int8_t;
47 typedef uint8_t prog_uint8_t;
48 typedef int16_t prog_int16_t;
49 typedef uint16_t prog_uint16_t;
50 typedef int32_t prog_int32_t;
51 typedef uint32_t prog_uint32_t;
52 typedef int64_t prog_int64_t;
53
54
55 static inline int memcmp_P(const void *s1,
56                            const prog_void *s2, unsigned n)
57 {
58         return memcmp(s1, s2, n);
59 }
60
61 static inline void *memcpy_P(void *s1,
62                              const prog_void *s2, unsigned n)
63 {
64         return memcpy(s1, s2, n);
65 }
66
67 static inline char *strcat_P(char *s1, const prog_char *s2)
68 {
69         return strcat(s1, s2);
70 }
71
72 static inline char *strcpy_P(char *s1, const prog_char *s2)
73 {
74         return strcpy(s1, s2);
75 }
76
77 static inline char *strncpy_P(char *s1, const prog_char *s2,
78                               unsigned n)
79 {
80         return strncpy(s1, s2, n);
81 }
82
83 static inline int strcmp_P(const char *s1, const prog_char *s2)
84 {
85         return strcmp(s1, s2);
86 }
87
88 static inline int strncmp_P(const char *s1, const prog_char *s2,
89                             unsigned n)
90 {
91         return strncmp(s1, s2, n);
92 }
93
94 static inline unsigned strlen_P(const prog_char *s)
95 {
96         return strlen(s);
97 }
98
99 static inline int vfprintf_P(FILE *stream,
100                              const prog_char *s, va_list ap)
101 {
102         return vfprintf(stream, s, ap);
103 }
104
105 static inline int vsprintf_P(char *buf, const prog_char *s,
106                              va_list ap)
107 {
108         return vsprintf(buf, s, ap);
109 }
110
111 #define PGM_P const char *
112 #define PSTR(x) x
113 #define PROGMEM
114 #define printf_P(arg...) printf(args)
115 #define sprintf_P(buf, args...) sprintf(buf, args)
116 #define snprintf_P(buf, n, args...) snprintf(buf, n, args)
117
118 static inline uint32_t pgm_read_dword(const prog_void *x)
119 {
120         return *(uint32_t *)x;
121 }
122
123 static inline uint16_t pgm_read_word(const prog_void *x)
124 {
125         return *(uint16_t *)x;
126 }
127
128 static inline uint8_t pgm_read_byte(const prog_void *x)
129 {
130         return *(uint8_t *)x;
131 }
132
133 #define PGMS_FMT "%s"
134
135 #endif /* HOST_VERSION */
136 #endif /* _AVERSIVE_PGMSPACE_H_ */
137
138