1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-08 06:37:35 +00:00

Demos: Use new format functions.

This commit is contained in:
asynts 2020-10-07 12:21:32 +02:00 committed by Andreas Kling
parent d781f3f6b5
commit 560c52989c
6 changed files with 34 additions and 45 deletions

View file

@ -177,8 +177,8 @@ void Cube::timer_event(Core::TimerEvent&)
} }
if ((m_cycles % 50) == 0) { if ((m_cycles % 50) == 0) {
dbgprintf("%d total cycles. finished 50 in %d ms, avg %d ms\n", m_cycles, m_accumulated_time, m_accumulated_time / 50); dbgln("{} total cycles. finished 50 in {} ms, avg {} ms", m_cycles, m_accumulated_time, m_accumulated_time / 50);
m_stats->set_text(String::format("%d ms", m_accumulated_time / 50)); m_stats->set_text(String::formatted("{} ms", m_accumulated_time / 50));
m_accumulated_time = 0; m_accumulated_time = 0;
} }

View file

@ -32,34 +32,32 @@
int main(int argc, char** argv, char** envp) int main(int argc, char** argv, char** envp)
{ {
for (int i = 0; i < argc; ++i) { for (int i = 0; i < argc; ++i)
printf("argv[%d]: %s\n", i, argv[i]); outln("argv[{}]: {}", i, argv[i]);
}
char** env; char** env;
for (env = envp; *env; ++env) { for (env = envp; *env; ++env)
printf("env: %s\n", *env); outln("env: {}", *env);
}
for (auxv_t* auxvp = (auxv_t*)++env; auxvp->a_type != AT_NULL; ++auxvp) { for (auxv_t* auxvp = (auxv_t*)++env; auxvp->a_type != AT_NULL; ++auxvp) {
printf("AuxVal: Type=%ld, Val/Ptr=%p\n", auxvp->a_type, auxvp->a_un.a_ptr); outln("AuxVal: Type={}, Val/Ptr={}", auxvp->a_type, auxvp->a_un.a_ptr);
if (auxvp->a_type == AT_PLATFORM) { if (auxvp->a_type == AT_PLATFORM) {
printf(" Platform: %s\n", (char*)auxvp->a_un.a_ptr); outln(" Platform: {}", (char*)auxvp->a_un.a_ptr);
} else if (auxvp->a_type == AT_EXECFN) { } else if (auxvp->a_type == AT_EXECFN) {
printf(" Filename: %s\n", (char*)auxvp->a_un.a_ptr); outln(" Filename: {}", (char*)auxvp->a_un.a_ptr);
} else if (auxvp->a_type == AT_RANDOM) { } else if (auxvp->a_type == AT_RANDOM) {
auto byte_ptr = (uint8_t*)auxvp->a_un.a_ptr; auto byte_ptr = (uint8_t*)auxvp->a_un.a_ptr;
printf(" My Random bytes are: "); outln(" My Random bytes are: ");
for (size_t i = 0; i < 16; ++i) { for (size_t i = 0; i < 16; ++i)
printf("0x%2x ", byte_ptr[i]); new_out("{:#02x} ", byte_ptr[i]);
} outln();
printf("\n");
} }
} }
void* handle = dlopen("/usr/lib/libDynamicLib.so", RTLD_LAZY | RTLD_GLOBAL); void* handle = dlopen("/usr/lib/libDynamicLib.so", RTLD_LAZY | RTLD_GLOBAL);
if (!handle) { if (!handle) {
printf("Failed to dlopen! %s\n", dlerror()); warnln("Failed to dlopen! {}", dlerror());
return 1; return 1;
} }
@ -67,57 +65,57 @@ int main(int argc, char** argv, char** envp)
int* ptr_global = (int*)dlsym(handle, "global_lib_variable"); int* ptr_global = (int*)dlsym(handle, "global_lib_variable");
if (!ptr_global) { if (!ptr_global) {
printf("Failed to dlsym for \"global_lib_variable\"! %s\n", dlerror()); warnln("Failed to dlsym for \"global_lib_variable\"! {}", dlerror());
return 2; return 2;
} }
printf("Found global lib variable address: %p\n", ptr_global); outln("Found global lib variable address: {}", ptr_global);
printf("Global lib variable is %d\n", *ptr_global); outln("Global lib variable is {}", *ptr_global);
// Test getting a method from the library and calling it // Test getting a method from the library and calling it
void (*lib_func)(void) = (void (*)(void))dlsym(handle, "global_lib_function"); void (*lib_func)(void) = (void (*)(void))dlsym(handle, "global_lib_function");
printf("Found global lib function address: %p\n", lib_func); outln("Found global lib function address: {}", lib_func);
if (!lib_func) { if (!lib_func) {
printf("Failed to dlsym for \"global_lib_function\"! %s\n", dlerror()); warnln("Failed to dlsym for \"global_lib_function\"! {}", dlerror());
return 2; return 2;
} }
lib_func(); lib_func();
printf("I think I called my lib function!\n"); outln("I think I called my lib function!");
// Test getting a method that takes and returns arguments now // Test getting a method that takes and returns arguments now
const char* (*other_func)(int) = (const char* (*)(int))dlsym(handle, "other_lib_function"); const char* (*other_func)(int) = (const char* (*)(int))dlsym(handle, "other_lib_function");
printf("Found other lib function address %p\n", other_func); outln("Found other lib function address {}", other_func);
if (!other_func) { if (!other_func) {
printf("Failed to dlsym for \"other_lib_function\"! %s\n", dlerror()); warnln("Failed to dlsym for \"other_lib_function\"! {}", dlerror());
return 2; return 2;
} }
// Call it twice with different arguments // Call it twice with different arguments
String formatted_result = other_func(10); String formatted_result = other_func(10);
printf("(%d + %d = %d) %s\n", *ptr_global, 10, *ptr_global + 10, formatted_result.characters()); outln("({} + {} = {}) {}", *ptr_global, 10, *ptr_global + 10, formatted_result);
*ptr_global = 17; *ptr_global = 17;
formatted_result = other_func(5); formatted_result = other_func(5);
printf("(%d + %d = %d) %s\n", *ptr_global, 5, *ptr_global + 5, formatted_result.characters()); outln("({} + {} = {}) {}", *ptr_global, 5, *ptr_global + 5, formatted_result);
int ret = dlclose(handle); int ret = dlclose(handle);
if (ret < 0) { if (ret < 0) {
printf("Failed to dlclose! %s\n", dlerror()); warnln("Failed to dlclose! {}", dlerror());
return 3; return 3;
} }
printf("Bye for now!\n"); outln("Bye for now!");
return 0; return 0;
} }

View file

@ -63,12 +63,12 @@ int global_lib_variable = 1234;
void global_lib_function() void global_lib_function()
{ {
printf("Hello from Dynamic Lib! g_glob::m_i == %d\n", g_glob.get_i()); outln("Hello from Dynamic Lib! g_glob::m_i == {}", g_glob.get_i());
} }
const char* other_lib_function(int my_argument) const char* other_lib_function(int my_argument)
{ {
dbgprintf("Hello from Dynamic Lib, now from the debug port! g_glob::m_i == %d\n", g_glob.get_i()); dbgln("Hello from Dynamic Lib, now from the debug port! g_glob::m_i == {}", g_glob.get_i());
int sum = my_argument + global_lib_variable; int sum = my_argument + global_lib_variable;
@ -77,7 +77,7 @@ const char* other_lib_function(int my_argument)
// the malloc of the main program which would be what they call 'very crash'. // the malloc of the main program which would be what they call 'very crash'.
// Feels very Windows :) // Feels very Windows :)
static String s_string; static String s_string;
s_string = String::format("Here's your string! Sum of argument and global_lib_variable: %d", sum); s_string = String::formatted("Here's your string! Sum of argument and global_lib_variable: {}", sum);
return s_string.characters(); return s_string.characters();
} }
} }

View file

@ -62,7 +62,7 @@ int main(int argc, char* argv[])
} }
if ((grid_rows > 0) ^ (grid_columns > 0)) { if ((grid_rows > 0) ^ (grid_columns > 0)) {
fprintf(stderr, "Expected either both or none of 'grid-rows' and 'grid-cols' to be passed.\n"); warnln("Expected either both or none of 'grid-rows' and 'grid-cols' to be passed.");
return 1; return 1;
} }

View file

@ -70,9 +70,6 @@ static const Color s_palette[] = {
Color(0xCF, 0xCF, 0x6F), Color(0xEF, 0xEF, 0xC7), Color(0xFF, 0xFF, 0xFF) Color(0xCF, 0xCF, 0x6F), Color(0xEF, 0xEF, 0xC7), Color(0xFF, 0xFF, 0xFF)
}; };
/*
* Fire Widget
*/
class Fire : public GUI::Widget { class Fire : public GUI::Widget {
C_OBJECT(Fire) C_OBJECT(Fire)
public: public:
@ -173,7 +170,7 @@ void Fire::timer_event(Core::TimerEvent&)
} }
if ((cycles % 50) == 0) { if ((cycles % 50) == 0) {
dbgprintf("%d total cycles. finished 50 in %d ms, avg %d ms\n", cycles, timeAvg, timeAvg / 50); dbgln("{} total cycles. finished 50 in {} ms, avg {} ms", cycles, timeAvg, timeAvg / 50);
stats->set_text(String::format("%d ms", timeAvg / 50)); stats->set_text(String::format("%d ms", timeAvg / 50));
timeAvg = 0; timeAvg = 0;
} }
@ -181,9 +178,6 @@ void Fire::timer_event(Core::TimerEvent&)
update(); update();
} }
/*
* Mouse handling events
*/
void Fire::mousedown_event(GUI::MouseEvent& event) void Fire::mousedown_event(GUI::MouseEvent& event)
{ {
if (event.button() == GUI::MouseButton::Left) if (event.button() == GUI::MouseButton::Left)
@ -217,9 +211,6 @@ void Fire::mouseup_event(GUI::MouseEvent& event)
return GUI::Widget::mouseup_event(event); return GUI::Widget::mouseup_event(event);
} }
/*
* Main
*/
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
auto app = GUI::Application::construct(argc, argv); auto app = GUI::Application::construct(argc, argv);

View file

@ -46,7 +46,7 @@ int main(int argc, char** argv)
window->show(); window->show();
view.on_title_change = [&](auto& title) { view.on_title_change = [&](auto& title) {
window->set_title(String::format("%s - WebView", title.characters())); window->set_title(String::formatted("{} - WebView", title));
}; };
view.on_link_hover = [&](auto& url) { view.on_link_hover = [&](auto& url) {