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 
9 #pragma once
10 
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <avr/pgmspace.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /* this is documented in sys/include/flash_utils.h - let's not confuse
31  * Doxygen */
32 #ifndef DOXYGEN
33 
34 #define FLASH_ATTR __flash
35 #define PRIsflash "S"
36 #define TO_FLASH(x) __extension__({static FLASH_ATTR const char __c[] = (x); &__c[0];})
37 
38 static inline int flash_strcmp(const char *ram, FLASH_ATTR const char *flash)
39 {
40  return strcmp_P(ram, (const char *)flash);
41 }
42 
43 static inline int flash_strncmp(const char *ram, FLASH_ATTR const char *flash, size_t n)
44 {
45  return strncmp_P(ram, (const char *)flash, n);
46 }
47 
48 static inline size_t flash_strlen(FLASH_ATTR const char *flash)
49 {
50  return strlen_P((const char *)flash);
51 }
52 
53 static inline char * flash_strcpy(char *ram, FLASH_ATTR const char *flash)
54 {
55  return strcpy_P(ram, (const char *)flash);
56 }
57 
58 static inline char * flash_strncpy(char *ram, FLASH_ATTR const char *flash, size_t n)
59 {
60  return strncpy_P(ram, (const char *)flash, n);
61 }
62 
63 static inline int flash_vprintf(FLASH_ATTR const char *flash, va_list args)
64 {
65  /* vprintf_P() is not provided by avr-libc. But vfprintf_P() with
66  * stdout as stream can be used to implement it */
67  return vfprintf_P(stdout, (const char *)flash, args);
68 }
69 
70 static inline int flash_vfprintf(FILE *stream, FLASH_ATTR const char *flash,
71  va_list args)
72 {
73  return vfprintf_P(stream, (const char *)flash, args);
74 }
75 
76 static inline int flash_vsnprintf(char *buf, size_t buf_len,
77  FLASH_ATTR const char *flash, va_list args)
78 {
79  return vsnprintf_P(buf, buf_len, (const char *)flash, args);
80 }
81 
82 static inline void flash_puts(FLASH_ATTR const char *flash)
83 {
84  puts_P((const char *)flash);
85 }
86 
87 static inline void * flash_memcpy(void *dest, FLASH_ATTR const void *src,
88  size_t n)
89 {
90  return memcpy_P(dest, (const void *)src, n);
91 }
92 
93 /* aliases need to be provided by the linker, as passing through va-args is
94  * not possible */
95 int flash_printf(FLASH_ATTR const char *flash, ...);
96 int flash_fprintf(FILE *stream, FLASH_ATTR const char *flash, ...);
97 int flash_snprintf(char *buf, size_t buf_len, FLASH_ATTR const char *flash, ...);
98 
99 #endif /* Doxygen */
100 
101 #ifdef __cplusplus
102 }
103 #endif
104 
stdio wrapper to extend the C libs stdio
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:67
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.