flash_utils_arch.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 Otto-von-Guericke-Universität Magdeburg
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
20 #ifndef FLASH_UTILS_ARCH_H
21 #define FLASH_UTILS_ARCH_H
22 
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <avr/pgmspace.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 /* this is documented in sys/include/flash_utils.h - let's not confuse
32  * Doxygen */
33 #ifndef DOXYGEN
34 
35 #define FLASH_ATTR __flash
36 #define PRIsflash "S"
37 #define TO_FLASH(x) __extension__({static FLASH_ATTR const char __c[] = (x); &__c[0];})
38 
39 static inline int flash_strcmp(const char *ram, FLASH_ATTR const char *flash)
40 {
41  return strcmp_P(ram, (const char *)flash);
42 }
43 
44 static inline int flash_strncmp(const char *ram, FLASH_ATTR const char *flash, size_t n)
45 {
46  return strncmp_P(ram, (const char *)flash, n);
47 }
48 
49 static inline size_t flash_strlen(FLASH_ATTR const char *flash)
50 {
51  return strlen_P((const char *)flash);
52 }
53 
54 static inline char * flash_strcpy(char *ram, FLASH_ATTR const char *flash)
55 {
56  return strcpy_P(ram, (const char *)flash);
57 }
58 
59 static inline char * flash_strncpy(char *ram, FLASH_ATTR const char *flash, size_t n)
60 {
61  return strncpy_P(ram, (const char *)flash, n);
62 }
63 
64 
65 static inline int flash_vprintf(FLASH_ATTR const char *flash, va_list args)
66 {
67  /* vprintf_P() is not provided by avr-libc. But vfprintf_P() with
68  * stdout as stream can be used to implement it */
69  return vfprintf_P(stdout, (const char *)flash, args);
70 }
71 
72 static inline int flash_vfprintf(FILE *stream, FLASH_ATTR const char *flash,
73  va_list args)
74 {
75  return vfprintf_P(stream, (const char *)flash, args);
76 }
77 
78 static inline int flash_vsnprintf(char *buf, size_t buf_len,
79  FLASH_ATTR const char *flash, va_list args)
80 {
81  return vsnprintf_P(buf, buf_len, (const char *)flash, args);
82 }
83 
84 static inline void flash_puts(FLASH_ATTR const char *flash)
85 {
86  puts_P((const char *)flash);
87 }
88 
89 static inline void * flash_memcpy(void *dest, FLASH_ATTR const void *src,
90  size_t n)
91 {
92  return memcpy_P(dest, (const void *)src, n);
93 }
94 
95 /* aliases need to be provided by the linker, as passing through va-args is
96  * not possible */
97 int flash_printf(FLASH_ATTR const char *flash, ...);
98 int flash_fprintf(FILE *stream, FLASH_ATTR const char *flash, ...);
99 int flash_snprintf(char *buf, size_t buf_len, FLASH_ATTR const char *flash, ...);
100 
101 #endif /* Doxygen */
102 
103 #ifdef __cplusplus
104 }
105 #endif
106 
108 #endif /* FLASH_UTILS_ARCH_H */
int flash_snprintf(char *buf, size_t buf_len, FLASH_ATTR const char *flash,...)
Like snprintf(), but the format string resides in flash.
int flash_vfprintf(FILE *stream, FLASH_ATTR const char *flash, va_list args)
Like vfprintf(), but the format string resides in flash.
char * flash_strncpy(char *ram, FLASH_ATTR const char *flash, size_t n)
Like strncpy(), but the source flash resides in flash.
char * flash_strcpy(char *ram, FLASH_ATTR const char *flash)
Like strcpy(), but the source flash resides in flash.
int flash_strncmp(const char *ram, FLASH_ATTR const char *flash, size_t n)
Like strncmp(), but the first string resides in flash.
#define FLASH_ATTR
C type qualifier required to place a variable in flash.
Definition: flash_utils.h:68
int flash_strcmp(const char *ram, FLASH_ATTR const char *flash)
Like strcmp(), but the second string resides in flash.
int flash_vprintf(FLASH_ATTR const char *flash, va_list args)
Like vprintf(), but the format string resides in flash.
void * flash_memcpy(void *dest, FLASH_ATTR const void *src, size_t n)
Like memcpy(), but src resides in flash.
int flash_fprintf(FILE *stream, FLASH_ATTR const char *flash,...)
Like fprintf(), but the format string resides in flash.
int flash_printf(FLASH_ATTR const char *flash,...)
Like printf(), but the format string resides in flash.
int flash_vsnprintf(char *buf, size_t buf_len, FLASH_ATTR const char *flash, va_list args)
Like vsnprintf(), but the format string resides in flash.
size_t flash_strlen(FLASH_ATTR const char *flash)
Like strlen(), but the string resides in flash.
void flash_puts(FLASH_ATTR const char *flash)
Like puts(), but the string resides in flash.
stdio wrapper to extend the C libs stdio