How do I make an interactive command line interface? - c++

I am trying to write a tool to compare my files but I found it difficult to interactive with. I want to support 2 operations: 1) load my files into memory 2) compare the files already loaded.
The idea is like below
while (true) {
getline(&line, &linesize, stdin);
if (strlen(line) < 2) continue;
token = strtok(line, DELIM);
if (!strcmp(token,"load")) {
puts("you want to load something");
} else if (!strcmp(token, "compare")) {
puts("you want to compare something");
} else if (!strcmp(token, "exit")) {
puts("exiting...");
exit(1);
} else {
puts("Cannot parse, try again");
}
}
In terminal, if I want to compare some MyVeryLongFileNameFile.foo and AnotherVeryLongFileNameFile.bar, I can just type diff My\tab Ano\tab \enter and it will auto completes the filenames for me.
I would like to also have these kind of features in my program, like using tab to autocomplete, using up/down to choose from previous commands, etc. How should I achieve this?

Using the ncurses.h library help you accomplish this.

Related

getch() like function in dart

In the c/c++ languages, there is a function in the conio.h header file called getch() which lets you input only 1 character and doesn't echo it on the screen and once that character has been typed, it automatically goes to the next line of code without having to press enter.
I've tried using the stdin.readByteSync() in dart but it doesn't give me the functionality that getch() gives in c/c++. I'd like to know if there is a way to make a function or method in dart that behaves in the same manner as getch() does in c/c++. Thank you.
You just need to set the following option to false:
https://api.dart.dev/stable/2.8.2/dart-io/Stdin/lineMode.html
And if you are using Windows you also need to set the following to false first according to the documentation:
https://api.dart.dev/stable/2.8.2/dart-io/Stdin/echoMode.html
A simple working example, which are just repeating what you type, can be made like this. It does not work inside IntelliJ but works from CMD, PowerShell and Linux bash:
import 'dart:convert';
import 'dart:io';
void main() {
stdin.echoMode = false;
stdin.lineMode = false;
stdin.transform(utf8.decoder).forEach((element) => print('Got: $element'));
}
By doing this we can also do you own suggestion and use stdin.readByteSync() (just notice that if you get a UTF-8 input, a character can contain multiple bytes:
import 'dart:io';
void main() {
print(getch());
}
int getch() {
stdin.echoMode = false;
stdin.lineMode = false;
return stdin.readByteSync();
}
Thank you all for your contribution. However adding to the answer I got that went like this
import 'dart:io';
void main() {
print(getch());
}
int getch() {
stdin.echoMode = false;
stdin.lineMode = false;
return stdin.readByteSync();
}
I decided to add something to make it more like the getch() function in conio.h header file in c language. The code goes like this
import 'dart:io';
void main() {
print(getch());
}
String getch() {
stdin.echoMode = false;
stdin.lineMode = false;
int a = stdin.readByteSync();
return String.fromCharCode(a);
}
Although it only works on cmd, powershell and linux terminal and not on intelliJ, it is better than nothing. The most important thing is to get the foundation of dart for things like flutter and web. And with this little knowledge, I was put it into practice and make a simple and basic typing game in dart. The code is below:
import 'dart:io';
import 'dart:convert';
import 'dart:core';
void main() {
Stopwatch s = Stopwatch();
String sentence = 'In the famous battle of Thermopylae in 480 BC, one of the most famous battles in history, King Leonidas of Sparta said the phrase'
' Molon Labe which means \"come and take them\" in ancient greek to Xerxes I of Persia when the Persians asked the Spartans to lay'
' down their arms and surrender.';
List<String> sentenceSplit = sentence.split(' ');
int wordCount = sentenceSplit.length;
print('Welcome to this typing game. Type the words you see on the screen below\n\n$sentence\n\n');
for (int i=0; i<sentence.length; i++) {
if(i==1) {
s.start(); // start the timer after first letter is clicked
}
if(getch() == sentence[i]) {
stdout.write(sentence[i]);
}
else {
i--;
continue;
}
}
s.stop(); // stop the timer
int typingSpeed = wordCount ~/ (s.elapsed.inSeconds/60);
print('\n\nWord Count:\t$wordCount words');
print('Elapsed time:\t${s.elapsed.inSeconds} seconds');
print('Typing speed:\t$typingSpeed WPM');
}
String getch() {
stdin.echoMode = false;
stdin.lineMode = false;
int a = stdin.readByteSync();
return String.fromCharCode(a);
}
You can go ahead and advance to make it a way that when the user starts the
game again, it should show a different text so they don't get used to it. But anyway, that is it for this question. It is officially closed. Although, if you have anymore to add, feel free to drop it here. Thank you!

Ubuntu Java watchservice is broken

This is my code to using monitor change folder:
WatchService watcher = FileSystems.getDefault().newWatchService();
Path dir = Paths.get("/home/user/test/");
dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
System.out.println("Begin monitor to test folder: ");
for (;;) {
// wait for key to be signaled
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException x) {
return;
}
for (WatchEvent<?> event: key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
// The filename is the
// context of the event.
#SuppressWarnings("unchecked")
WatchEvent<Path> ev = (WatchEvent<Path>)event;
Path filename = ev.context();
if (filename.toString().startsWith(".")) continue;
if (kind == OVERFLOW) {
continue;
} else if (kind == ENTRY_CREATE) {
System.out.println(kind.name() + ":" +filename);
} else if (kind == ENTRY_DELETE) {
System.out.println(kind.name() + ":" +filename);
} else if (kind == ENTRY_MODIFY) {
System.out.println(kind.name() + ":" +filename);
}
}
boolean valid = key.reset();
if (!valid) {
break;
}
}
It's great on windown, Mac os, However when run on ubuntu 16.04, I to face the problem:
For existed files in watched folder: when I edit a file then I receiver create event, While i want to get modify event
Please help me
Thanks.
Make sure that the program you're using to edit the file with in Ubuntu doesn't create a hidden file. Some editors do this to make sure no changes are lost when the program crashes.
So make sure that your editor does not create a hidden file when you're editing the file and if it does use another program or handle the hidden "subfile".

How can I compress backup log files?

I'm using log4cplus in my project to do logging.
I created logger.conf and I will load it in the beginning of my application.
This is my logger.conf:
log4cplus.appender.Developer=log4cplus::RollingFileAppender
log4cplus.appender.Developer.DatePattern = ".yyyy-MM-dd"
log4cplus.appender.Developer.Schedule = HOURLY
log4cplus.appender.Developer.File=log/developer.log
log4cplus.appender.Developer.MaxFileSize=3MB
log4cplus.appender.Developer.MaxBackupIndex=10
log4cplus.appender.Developer.layout=log4cplus::PatternLayout
log4cplus.appender.Developer.layout.ContextPrinting=enabled
log4cplus.appender.Developer.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S,%Q} [%t] %p - %m%n
log4cplus.appender.Developer.Threshold=TRACE
log4cplus.logger.DEVELOPER=TRACE, Developer
This is how I load my logger.conf:
QString log_path = qApp->applicationDirPath() + "/log";
QDir().mkpath(log_path);
PropertyConfigurator logger(L"configs/logger.conf", Logger::getDefaultHierarchy());
logger.configure();
And whenever I want to log, I use the following line:
Logger::getInstance(L"DEVELOPER").log(INFO_LOG_LEVEL, L"..............");
I'd like to know two things:
How can I tell Log4Cplus to compress the backup logs?
In some post I saw this reply:
I need to create my own appender, inheriting from RollingFileAppender and then add a compression steps.
If it's possible, can anyone tell me how to do it, please? I don't know how to implement this.
How can I add a pattern to the name of these backup logs?
At the moment, Log4Cplus makes my back up like this:
developer.log.1
developer.log.2
developer.log.3
...
I'd like to add date and time to it.
You will need to implement you own Appender like this:
class NewFileAppender : public ::log4cplus::RollingFileAppender
{
void
newFileAppender::rollover()
{
helpers::LogLog & loglog = getLogLog();
// Close the current file
out.close();
out.clear(); // reset flags since the C++ standard specified that all the
// flags should remain unchanged on a close
// If maxBackups <= 0, then there is no file renaming to be done.
if (maxBackupIndex > 0)
{
rolloverCompressedFiles(filename, maxBackupIndex);
// Rename fileName to fileName.DATE
tstring target = filename + DATE;
int ret;
ret = file_rename (filename, target);
//TODO: compress using zlib
}
}
}

Parsing xml and dump in file each <> block

I am trying to parse xml with some simple C++ which has blocks like
<mgrwt event="1">
...
...
...
</mgrwt>
<mgrwt event="2">
...
...
...
</mgrwt>
Now, I have a bash script which acts on each of these blocks - So, my question is, how can I loop inside the xml ( I do not need RapidXML or something similar though) so that to easily dump to a small temp file each block ?
My parser looks like
bool begin_tag = false;
while (getline(in,line))
{
std::string tmp;
tmp=line;
if(line.find("<mgrwt event=")<=line.size()){
cout<<line<<endl;
begin_tag = true;
continue;
}
else if (tmp == "</mgrwt>")
{
begin_tag = false;
}
}
}
thanks
Alex
I would recommend using an XML parser for reading XML files. Checkout expat, POCO XML or other libraries.
If you can't for whatever reason, and the stuff you're reading always looks exactly the same as in your sample with no other formatting variations, you also should use find() to detect the end of the block:
else if(line.find("</mgrwt>")<=line.size())
{
begin_tag = false;
}

Populating a database with file names from directories

I have an application which behaves as a slideshow for all pictures in a folder. It is written in Borland's C++ Builder (9). It currently uses some borrowed code to throw the filenames into a listbox and save the listbox items as a text file.
I want to update this so that the filenames are stored in a proper database so that I can include extra fields and do proper SQL things with it.
So basically I would be able to work it out if I saw some 'sample' code doing the same thing.
So if anyone knows of any code that does this I would be greatful. It needs to be able to do it on certain file types... not just all the files.
You basically neeed to write a recursive function with a TDataSet parameter.
(I could not compile my code, so you get it "as is")
void AddFiles(AnsiString path, TDataSet *DataSet)
{
TSearchRec sr;
int f;
f = FindFirst(path+"\\*.*", faAnyFile, sr);
while( !f )
{
if(sr.Attr & faDirectory)
{
if(sr.Name != "." && sr.Name != "..")
{
path.sprintf("%s%s%s", path, "\\", sr.Name);
AddFiles(path, DataSet);
}
}
else
{
DataSet->Append();
DataSet->FieldByName("Name")->Value = sr.Name;
/* other fields ... */
DataSet->Post();
}
f = FindNext(sr);
}
FindClose(sr);
}