Solved

text editor in C

Posted on 2001-07-10
11
241 Views
Last Modified: 2013-12-13
Hello.

I am working a project that requires me to develop a new test editor using C programming language.

This text editor should be able to allow the user to do the ff:
    * insert a new char/string
    * delete a char/line
    * save a file in text format.

I'm not that really proficient in C, but i need to have a starting to point to work with in this project.

Any suggestions/comments will be highly appreciated. But please make them simple.

Thanks in advance.
0
Comment
Question by:ylen
11 Comments
 
LVL 47

Expert Comment

by:dbrunton
Comment Utility
You are probably going to need an array of arrays to store your data in and a global variable to identify where your cursor is in these arrays.  That's your basic data structure and all else followis from that.
0
 
LVL 2

Expert Comment

by:smitty1276
Comment Utility
Do you have to "wrap" the lines, or can the user just type and type on a single line as long as possible?  That will make a HUGE difference in how you need to store the data.  A fixed 2D array won't work if the lines aren't wrapped.  

If they are wrapped, you will have to check the length of the lines.  When the line reaches the max length (80 chars?) you will have to drop to the next line.  If the user is in INSERT mode (are you keeping up with that?) you will have to shift the entire array of arrays, starting at the bottom line and moving up, to the right by one character.
0
 
LVL 22

Expert Comment

by:cookre
Comment Utility
One way of dealing with variable length lines is to store them sequentially with each line prefaced with its' length.  

Insertion performance can be enhanced by using a doubly linked chain of lines.
0
 
LVL 2

Expert Comment

by:MadYugoslav
Comment Utility
In my early projects I made some editor in TurboC 2.01.
Main structures are:

#define  MAXLINES  2000
#define  MAX_LONG   129
#define  WIN_HIGHT   23
typedef  char *LINESS[MAXLINES];
typedef struct {
     LINESS lines;
      char filename[75];
      int  win_hight;     /* Editor window height */
      int  max_long;      /* Max line length */
      int  insert_status; /* INSERT / OVERWRITE */
      int  caps_status;   /* CAPS LOCK-a ( ON / OFF ) */
      int  last_line;     /* Last line */
      int  temp_row;      /* Current file line */
      int  temp_col;      /* Current file column */
      int  screen_row;     /* Current screen line */
      int  screen_col;     /* Current screen column */
      int  first_row;    /* First visible file line */
      int  last_row;     /* Last visible file line */
      int  first_col;      /* First visible file column */
} TEXT_FILE;

All data are saved in that structure.
Text is saved in structure LINESS which is array of char pointers.
There is dynamic allocating for each row of text (on maximum line length).

Any questions.
0
 

Author Comment

by:ylen
Comment Utility
Thanks for the replies folks.

I know those ideas are going to be really useful. But I what i really wanted is an editor which is similar to vi.
A simple version of vi, that is.

Say for example, the program name is NewEdit.c. If the user type in 'NewEdit newText.txt', the program should be able to remember that the file has to be named as newTest.txt, and a screen will be open to allow that user to start typing/editing. How do i do this?

It's really the UI that's givinng me a hard time.

Shall wait for your comments/replies.

Thanks again.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 2

Expert Comment

by:smitty1276
Comment Utility
You can variable to hold the file name...

void main( int argc, char **argv )
{
  //assuming the command line you used above was used
  char *fileName = argv[1];

}

Of course, you may want to thoroughly check out all of the command line arguments to make sure that they didn't enter things in the wrong order.
0
 
LVL 24

Expert Comment

by:SunBow
Comment Utility
> It's really the UI that's giving me a hard time.

You may want to also say what is already done (or will be soon) so we need not be redundantly repetetive and can focus better. [not that I good at that part, ;) ]

Can we say you want us focus on the command line interpreter piece? Or the visual editing? We can skip macros, of course. And fancy mass replacements. True?

You may well benefit from having the very last line (or top edge) of your screen be completely reserved for entry of manual commands. This avails you of good visual feedback of the input hitting your code (and text). Consider using the exact opposite of screen (bottom or top) to be simple status display, such as for the currently defined current filename. Current row&column. Etc.

You may want to invent commands. Once they are mapped out, come back and ask on_it.  For example, one command or special key on keyboard can be reserved to move the program focus from command line entry to visual editing. So you really have two Main program functions, that can nearly run complete on their own fairly running standalone.

Once in the visual editing main program, one main new piece is functions utilizing the cursor keys. Now, is this the place where you need the more help? The MadYugoslav structure seems fit to start with for a stripped down VI emulation.
0
 
LVL 24

Expert Comment

by:SunBow
Comment Utility
> allow that user to start typing/editing. How do i do this?

1) Fill screen displaying lines retrieved
2) Place cursor on last line for my example of command line entry
3) Enter something like "IHello World" <enter>
4) The screen should now place "Hello World" on very first line, while all other lines scroll down.

In my language, I defined symbol
"I" for insert command
and <enter> to complete it (also part of text entry).

You too must define a minimal command set. I take it you may be confused on the visual handling with mixing of commands, input characters, cursors, etc.

Thus I suggest you first get the commands to work, using less visual technique, but more manageable.

While doing so, your code should increase in value for functions like scrolling and buffering, so you can focus more on UI having confidence that the other scrolling and buffering is already in order (or, have you already done that part?)
0
 
LVL 2

Expert Comment

by:curri
Comment Utility
Also, which library are you using for screen management ? may I recommend curses (it is available for unix and windows, probably for almost everything else :).

Basically you need some way of displaying characters at specific places on the screen. With just cout it is going to be a huge pain :)

When I did my first text editor I used direct memory access on the video memory (under DOS, about 10 years ago :)

Orlando
0
 
LVL 54

Expert Comment

by:nico5038
Comment Utility

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
 - PAQ'd and pts removed
Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

Nic;o)
0
 
LVL 5

Accepted Solution

by:
Netminder earned 0 total points
Comment Utility
Per recommendation, points NOT refunded and question closed.

Netminder
EE Admin
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A short article about a problem I had getting the GPS LocationListener working.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now