Change the text of text from command line in C
I am writing a small C program - log in, log out, create an account, send-a-message-to-another-deal with an account - I can build and modify, and update and polish until my C skills improve.
I'm trying to figure out how to implement the "write message" part of the option. After being prompted for the recipient, subject and body of the message, I want to be able to go back and edit them without rewriting the whole thing. So if I chose the Edit Post option, it would write out what I had and let me go back and change something.
I did something like this in x86 assembly, or started doing something like this, so I understand how this would be implemented at the machine level, but I don't know how to implement it in C. How should I do this?
Things like the following baffle me in C:
- Nice way to handle potentially long input
- How to write text and allow the user to edit it without going beyond a certain point.
- How to control the position of the cursor
Also, if this has been done before, and there is a library of functions for things like this (even a standard library that I don't know about), please note that I want to flip my own for learning purposes.
I guess the method I have in my head is to read the message body one character at a time so that I can account for the carriage return and create a multi-line message. But I'm not sure how I could get through this. I think it would be very difficult to do it from the command line (?).
If not impossible, move the cursor back and erase characters that are already out of the input buffer ... Would I have to "re-draw the screen" every time? How can I just take control of the entire console and just read and write keystrokes at specific positions? Or is it too close to the car? I kind of did it with assembly, but used 16-bit interrupts, which I am not allowed to use in C ... This is what I wrote in assembly:
(the image that used to be here is gone)
where the program converts the byte value to two character codes representing that byte, then jumps to the right column and writes the original byte (which appears as a character), then loops back and writes two more hexadecimal numbers, in the next slot ... And so on, left to right, top to bottom ... It was easy, but I have no idea how to implement this in C. All I can do is INT 21 style input and output, writing lines in the console that scrolls the window up, etc. .d.
a source to share
There is no way in ANSI C to create a portable string editor. If you give up on your own, you will have to restart it for every new operating system you want to run on.
If I can make a suggestion, I would use a pre-existing library to do all this difficult platform-related dirty work, and with that feet, learn to handle things like arbitrary length input, and such, Then when your code works ( and that's good), learn how to do all this dirty work and grab your library crutch. So you don't solve the whole thing - you break it down into more manageable pieces.
Even that simplifies things a little. It took me a while to learn how to handle arbitrary input length.
Also, know that if you want your code to be portable, removing a library dependency would mean that if you want to port it, you either need to: a) rewrite all that messy code to work, or b) add the library back ...
To end it all as a joke, this is your brain with libraries:
Pigmaei gigantum humeris impiti plusquam ipsi gigantes vident.
(If I see a little further, he stands on the shoulders of giants.)
- Isaac Newton
This is your brain without libraries:
If I didn’t see the others, it’s because the giants stood on my shoulders. - Hal Abelson
a source to share
You say you want to avoid using libraries (standard or otherwise), but unfortunately in C all input and output is done through libraries - the language itself has absolutely no I / O facilities.
So, you are doomed to use some kind of libraries. Given that you seem to want a text-based interface, I suggest taking a look at the portable version of curses , PDCurses .
I wrote a text editor.
I have been doing C for twenty years.
The data structure for text was the most complex single data structure I have ever written; it is a data structure that can, as you indicate, efficiently handle arbitrary length text.
If you're new to C, you bite off more than you can chew.
I suggest a simple data structure like a buffer or a linked list - you can't handle arbitrary length text, but that's better than nothing.
It is very system (OS) related. I think you are trying to do this on Windows.
You can use the Windows Console API for this .
If you want to explore some libraries for reference (before you start your own), GNU Readline is a good library .
a source to share
When it comes to data storage, I guess rope 'd is the best data structure available: in simplified form, it's a tree of strings. If you want to print it on screen, all you have to do is go through it and print it out. Splitting a row in two means a relatively simple tree operation: add a leaf. The hard part would be splitting the line itself (copy the line, set the old end to 0, add a sheet, add a pointer) ...
Now there is the problem of tracking the cursor ... You can leave the pavement for the roots to follow from the root to where the cursor is.
Also we are thinking about a scalable console ... That is, if you want your editor to work regardless of whether the terminal has 80 or 200 characters ...
a source to share