add string demo
This commit is contained in:
@ -6,6 +6,7 @@
|
||||
#include "user/demo/PCSPKdemo.h"
|
||||
#include "user/demo/PreemptiveThreadDemo.h"
|
||||
#include "user/demo/SmartPointerDemo.h"
|
||||
#include "user/demo/StringDemo.h"
|
||||
#include "user/demo/TextDemo.h"
|
||||
#include "user/demo/VBEdemo.h"
|
||||
#include "user/demo/VectorDemo.h"
|
||||
@ -33,7 +34,7 @@ void MainMenu::run() {
|
||||
while (running) {
|
||||
input = this->listener.waitForKeyEvent();
|
||||
|
||||
if (input >= '0' && input <= '9') {
|
||||
if ((input >= '0' && input <= '9') || input == '!') {
|
||||
switch (input) {
|
||||
case '1':
|
||||
running_demo = scheduler.ready<TextDemo>();
|
||||
@ -66,6 +67,9 @@ void MainMenu::run() {
|
||||
case '0':
|
||||
running_demo = scheduler.ready<SmartPointerDemo>();
|
||||
break;
|
||||
case '!':
|
||||
running_demo = scheduler.ready<StringDemo>();
|
||||
break;
|
||||
}
|
||||
} else if (input == 'k') {
|
||||
scheduler.nice_kill(running_demo); // NOTE: If thread exits itself this will throw error
|
||||
|
54
c_os/user/demo/StringDemo.cc
Normal file
54
c_os/user/demo/StringDemo.cc
Normal file
@ -0,0 +1,54 @@
|
||||
#include "user/demo/StringDemo.h"
|
||||
#include "kernel/Globals.h"
|
||||
#include "user/lib/String.h"
|
||||
|
||||
void StringDemo::run() {
|
||||
kout.lock();
|
||||
kout.clear();
|
||||
|
||||
log.info() << "Allocating new string" << endl;
|
||||
bse::string str1 = "This is a dynamically allocated string!";
|
||||
kout << str1 << endl;
|
||||
|
||||
log.info() << "Reassign string" << endl;
|
||||
str1 = "Hello";
|
||||
kout << str1 << " has length " << str1.size() << endl;
|
||||
kout << "Again with strlen: Hello has length " << bse::strlen("Hello") << endl;
|
||||
|
||||
kout << "Adding strings: " << str1 << " + World" << endl;
|
||||
log.info() << "Adding strings" << endl;
|
||||
str1 = str1 + " World";
|
||||
kout << str1 << endl;
|
||||
|
||||
kout << "Hello += World" << endl;
|
||||
log.info() << "Hello += World" << endl;
|
||||
bse::string str3 = "Hello";
|
||||
str3 += " World";
|
||||
kout << str3 << endl;
|
||||
|
||||
kout << "String iterator!" << endl;
|
||||
for (const char c : str1) {
|
||||
kout << c << " ";
|
||||
}
|
||||
kout << endl;
|
||||
|
||||
log.info() << "Allocating new string" << endl;
|
||||
bse::string str2 = "Hello World";
|
||||
kout << "str1 == str2: " << static_cast<int>(str1 == str2) << endl;
|
||||
kout << "strcmp(Hello, Hello): " << bse::strcmp("Hello", "Hello") << endl;
|
||||
|
||||
log.info() << "Reassign str2" << endl;
|
||||
str2 = "Hello";
|
||||
|
||||
bse::array<char, 5> arr;
|
||||
arr[0] = 'H';
|
||||
arr[1] = 'e';
|
||||
arr[2] = 'l';
|
||||
arr[3] = 'l';
|
||||
arr[4] = 'o';
|
||||
kout << "bse::array<char, 5> to bse::string: " << arr << ", size: " << ((bse::string)arr).size() << endl;
|
||||
kout << "(bse::string)arr (" << arr << ") == str2 (" << str2 << "): " << static_cast<int>((bse::string)arr == str2) << endl;
|
||||
|
||||
kout.unlock();
|
||||
scheduler.exit();
|
||||
}
|
16
c_os/user/demo/StringDemo.h
Normal file
16
c_os/user/demo/StringDemo.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef __StringDemo_include__
|
||||
#define __StringDemo_include__
|
||||
|
||||
#include "kernel/Globals.h"
|
||||
|
||||
class StringDemo : public Thread {
|
||||
private:
|
||||
StringDemo(const StringDemo& copy) = delete;
|
||||
|
||||
public:
|
||||
StringDemo() : Thread("StringDemo") {}
|
||||
|
||||
void run() override;
|
||||
};
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user