11/*
2- WebUI Library Extras
2+ WebUI Library
33 https://webui.me
44 https://github.com/webui-dev/webui
55 Copyright (c) 2020-2025 Hassan Draga.
1111#ifndef _WEBUI_EXTENSIONS_H
1212#define _WEBUI_EXTENSIONS_H
1313
14+ #include "webui.h"
15+
1416#ifdef __cplusplus
1517extern "C" {
1618#endif
1719
18- /**
19- * This is to help the compiler identify whether to compile the extensions API funtions or not
20- * - If you are including the webui.c file to your source file directly,
21- * and wish to use the extensions API functions,
22- * please include this header file before including webui.c,
23- * or define this macro manually before including webui.c
24- * - In other cases, as long as the webui.c file is not included directly,
25- * you might need to define this macro in your compiler settings
26- */
27- #ifndef WEBUI_EXTENSION_API
28- #define WEBUI_EXTENSION_API
29- #endif
30-
31- #ifndef WEBUI_EXPORT
32- #define WEBUI_EXPORT
33- #warning "WEBUI_EXPORT not defined; Please include webui.h before webui_extensions.h"
34- #endif
35-
36- #include <stdbool.h>
37- #include <stddef.h>
38-
3920/**
4021 * @brief Construct a JavaScript string from a format string,
4122 * then execute it without waiting for the response. All clients.
@@ -51,7 +32,31 @@ extern "C" {
5132 *
5233 * @example webui_run_fmt(myWindow, "alert('Hello %s');", "World");
5334 */
54- WEBUI_EXPORT void webui_run_fmt (size_t window , const char * fmt , ...);
35+ void webui_run_fmt (size_t window , const char * fmt , ...) {
36+
37+ int len ;
38+ char * buf ;
39+
40+ // Precalculate actual string length
41+ va_list args ;
42+ va_start (args , fmt );
43+ len = vsnprintf (NULL , 0 , fmt , args );
44+ va_end (args );
45+
46+ if (len < 1 ) return ;
47+
48+ buf = (char * )webui_malloc (len + 1 );
49+ if (buf == NULL ) return ;
50+
51+ // Format and execute
52+ va_start (args , fmt );
53+ vsnprintf (buf , len + 1 , fmt , args );
54+ va_end (args );
55+
56+ webui_run (window , buf );
57+
58+ webui_free ((void * )buf );
59+ }
5560
5661/**
5762 * @brief Construct a JavaScript string from a format string,
@@ -75,12 +80,42 @@ WEBUI_EXPORT void webui_run_fmt(size_t window, const char* fmt, ...);
7580 * @example bool err = webui_script_fmt(myWindow, 0, myBuffer, myBufferSize,
7681 * "return %d + %d;", 4, 6);
7782 */
78- WEBUI_EXPORT bool webui_script_fmt (size_t window , size_t timeout ,
79- char * buffer , size_t buffer_length ,
80- const char * fmt , ...);
83+ bool webui_script_fmt (
84+ size_t window ,
85+ size_t timeout ,
86+ char * buffer ,
87+ size_t buffer_length ,
88+ const char * fmt , ...) {
89+
90+ int len ;
91+ char * buf ;
92+ bool status ;
93+
94+ // Precalculate actual string length
95+ va_list args ;
96+ va_start (args , fmt );
97+ len = vsnprintf (NULL , 0 , fmt , args );
98+ va_end (args );
99+
100+ if (len < 1 ) return false;
101+
102+ buf = (char * )webui_malloc (len + 1 );
103+ if (buf == NULL ) return false;
104+
105+ // Format and execute
106+ va_start (args , fmt );
107+ vsnprintf (buf , len + 1 , fmt , args );
108+ va_end (args );
109+
110+ status = webui_script (window , buf , timeout , buffer , buffer_length );
111+
112+ webui_free ((void * )buf );
113+
114+ return status ;
115+ }
81116
82117#ifdef __cplusplus
83118}
84119#endif
85120
86- #endif /*_WEBUI_EXTENSIONS_H */
121+ #endif /* _WEBUI_EXTENSIONS_H */
0 commit comments