How do you make C edit text? As in run the cursor over it and change it?
Printable View
How do you make C edit text? As in run the cursor over it and change it?
That is a very short question with a very long answer. To write a VERY simple editor is about 300 lines of code, I expect. [In fact, I did one as a "challenge" not so long ago - it was 274 lines in the main file, and a linked-list handling file of another 108 lines - along with one header-file of a few lines of prototypes copied from the linked-list source].
Of course, that is to load a text-file, edit it in some simple ways (delete line, add line), and save it back out again.
To "mark" text by for example holding shift and using the arrow-keys will involve some form of system-dependant code (or a system-independent library that does the system-dependant parts FOR YOU).
Of course, if we are not talking console, but a full Windows or Xwindows application, then it is both simpler and harder, as some things are done for you, but you need to write more code in other places simply to achieve a window on the screen, etc.
--
Mats
You need a setting for the text on screen (which is where the cursor is). This is probably WAY more complicated than you might have hoped. After all, barking up this tree won't really cut it:
Have a look into ncurses() or an actual GUI library.Code:#include <stdio.h>
int main() {
printf("XX XX\b\b\b\bY");
}
I don't want a GUI, and I don't care how long it takes, please just tell me how.
http://www.adrianxw.dk/SoftwareSite/index.html
http://pdcurses.sourceforge.net/
Read both, and learn about controlling the console screen.
Do you have any links for controlling the console screen?
You can't. I believe what you want to do and what matsp was talking about are not quite the same thing -- his program did not allow you to back the cursor up the screen and actually delete and replace something on the screen. Or I will eat an egg.
ncurses() runs in the console (ie, from the command-line); a lot of old pre-net era applications were that way. It's easier to use than a GUI API, but it will still take you awhile. There are some tutorials around. ncurses() is actually quite neat.
So it is either a GUI or ncurses?
No eggs needed here.
I have written code to walk about on the screen for various purposes (including implementig an Emacs-like editor written in PDP-11 assembler on 68K). But just handling the keyboard/screen in Windows or Linux is a major undertaking - I'd expect that to be at least 2-300 lines of code just to be able to put some text up and move about within it. That doesn't include any actual EDITING - that will add even more code. I counted up some 380 lines of code to do the other basic bits, and that's with a fair bit missing for editing a line of text in the way you can in DOS's "EDIT".
Yes, it can be done - and you can either use some curses version (pd or n) or you can write your own functions to move within the text on screen yourself. But if you have never done it before, you will probably end up spendig QUITE A LOT of time just doing that.
--
Mats
You haven't given me any idea how to write my own functions to do that.
What function(s) do you want me to give you ideas for? If you are using windows, you need to interface to the Console Functions. There are some of that in the FAQ, the rest is on the MSDN site http://msdn.microsoft.com/en-us/libr...73(VS.85).aspx
I'm sorry, but this forum isn't the "post a few lines of question and get 2-300 lines of working code" - you must have got us confused.
If you are using Linux/Unix, you need to use ANSI escape sequences (if you need this, try googling for "wiki ansi escape sequence" and try the "I'm feeling lucky" button).
--
Mats
Thanks for the Microsoft link.
Would it be easier in x86 assembly?
for me, or for you to explain.
How much do you really think we know about you and what your abilities are?
If you need to find some code to get an idea of where to start, then you need to scrape the web for it instead of asking us. Try looking at the code for these
http://code.google.com/search/#q=notepad%20replacement
http://sourceforge.net/projects/padpaper/
and you'll get an idea of how what you want to do is actually done. You might convince yourself to use something you found instead, too.
In general the more effort you put in the more help we can be.
I couldn't tell you much about assembly beyond the fact that you will find learning it more awkward than learning C. I could be wrong.
I also don't know anything about the windows console API (or whatever it's called), but it should not be much more (or less) complicated than curses. Like I said earlier, ncurses() is neat once you learn how to use it, and no doubt so are the other options. Pick which ever one you want. But you won't be finishing that up this evening.
Learning a basic Application Programming Interface is probably a good way to get experience and learn C at the same time. How much you'll have to struggle depends on how much you already know, but I really believe that you should be able to start tackling a console API not long after you get a grasp on datatypes and pointers.
And functions. Functions.
No, I know x86 assembly, I am wondering if it would be easier to explain how to edit text with it.
So would you be able to explain how then?
You can use the Windows API to move the console cursor.
Go to MSKnowledge Base and look up: SetConsoleCursorPosition(x,y).
Where x and y represent the the X and Y position, you want the cursor moved to.
Mixing C with assembly?
No, it's straight C, it just uses the Windows API (Application Program Interface).
You don't want assembly - oh! How you do *not* want assembly!! :)
No, but considering that both 68K and PDP-11 has generic "memory to memory" moves [and most other operations], and x86 hasn't, that would further extend the number of lines of code by some amount. For example, where in 68K you could do a memcpy() using this:
Yes, you can do that with rep movs if the registers are in the right place, but it is just ONE possible memory to memory operation - a common one, but there are plenty of places where copying stuff from a data structure to another data structure, for example, would require double the number of x86 instructions compared to the 68K instructions.Code:// assume A0 and A1 points to the memory to be copied, and D0 is the
memcpy:
move.b (a1)+, (a0)+
dbra d0, memcpy
ret
So a rough estimate would be that 7000 lines of 68K assembler turns into some 8500 lines of x86 assembler. Not bad, but a good bit of code to write if you are not used to write LARGE programs in assembler.
It is still MUCH easier to write this sort of thing in a language that has ready-made functions to do many string operations, output numbers in almost any format you like, etc, etc.
--
Mats
So what if you mix C and assembly so you could do it in the least possible time?
Yes!
If you feel that your assembler knowledge is BETTER than your C knowledge, then you probably should practice the C knowledge, as it is much more commercially valuable than assembler knowledge (not that assembler is not valuable, but more companies write in C than in x86-assembler).
And no, I doubt you will get it done faster by writing ANY of it in assembler.
The reason TAMIS was written in Assembler was to make it small and a RTS (RunTime Support) module. Pascal and C which were the languages available at the time needed their own RTS, so that could not be used for an RTS. Since the PDP-11 machine we shared at school had about 70 terminals, connected to ONE machine, sharing all the users for TAMIS into one 16KB region of memory would help the machine not swap the processes out so much - as the machine had 2MB of RAM. The previous editor was written in Pascal, and used up a total of about 150KB (using overlays, since the memory available to one process was 64KB). When 15-20 people are all using the "big" editor, the machine would run at crawling speed - even for it's days.
--
Mats
who cares, OP doesn't have a clue anyway
Well, thanks