Выбрать главу

At this point it would be useful to take a look at the directory workspace/record_sort/. It contains just two files, .cproject and .project, both of which are XML code describing the project. The .project file provides configuration information to the base Eclipse platform while the more extensive .cproject file provides information to CDT. It’s not necessary to understand the contents of these files, but it is useful to know they exist.

4.3 Adding Source Code to the Project

There are basically two ways to add source code to a C project. You can, of course, create a new file in an Editor window, or you can import existing files into the project. Execute File→Import… to bring up the Import Select dialog. Expand the General category and select File System. Click Next, then click Browse, and navigate to Home/EclipseSamples/record_sort and click OK.

This brings up the Import dialog shown in Figure 4.2. Select all three files and click Finish. Those files now show up in the Project Explorer view. Note that there is no record_sort.c file. That’s because you’re going to type it in yourself to get some practice with the CDT editor.

Figure 4.2: Import dialog.

Click the down arrow next to the New icon at the left end of the tool bar and select Source File from the drop down menu. Name it “record_sort.c.” An Editor window opens up with a preliminary header comment. The contents of record_sort.c are given in Figure 4.3, but don’t start typing until you read the next section.

/*

 * author Doug Abbott

 *

 * Simple demonstration of building and running a project under

 * Eclipse.

 *

 * Usage:

 * record_sort <filename> [1 | 2]

 *

 * Sorts records in <filename> either by name (1) or ID (2).

 * Default is name. Outputs sorted file to stdout.

*/

#include <stdio.h>

#include <stdlib.h>

#include "record_sort.h"

int main (int argc, char **argv) {

 int size, sort = 1;

 record_t *records;

 if (read_file (argv[1], &size, &records)) {

  printf ("Couldn't open file %s\n", argv[1]);

  exit(1);

 }

 if (argc > 2) sort = atoi (argv[2]);

 switch (sort) {

 case 1:

  sort_name (size, records);

  break;

 case 2:

  sort_ID (size, records);

  break;

 default:

  printf ("Invalid sort argument\n");

  return_records (size, records);

  exit (2);

 }

 write_sorted (size, records);

 return_records (size, records);

 return 0;

}

Figure 4.3: record_sort.c.

4.3.1 Content Assist

The CDT Editor has a number of features to make your coding life easier. These fall under the general heading of Content Assist. The basic idea of Content Assist is to reduce the number of keystrokes you must type by predicting what you’re likely to type based on the current context, scope, and prefix. Content Assist is invoked by typing Ctrl+Space and it’s also auto-activated when you type “.”, “->”, or “::”  following a struct or class name.

4.3.2 Code Templates

Code Templates are an element of Content Assist that provide starting points for frequently used sections of code. When you enter a letter combination followed by Ctrl+Space, a list of code templates that begin with that letter combination is displayed. Select one and it is automatically inserted into the file at the current cursor location.

Try this: after entering the #include lines in Figure 4.3, type “ma” Ctrl+Space. This brings up a template for the main() function. Note that the format of templates is independent of whichever Code Style you’ve selected and the default is KR. You can edit templates at Window→Preferences→C/C++→Editor→Templates. We’ll take a closer look at that later in the chapter. Other aspects of Content Assist can also be customized under Preferences.

4.3.3 Automatic Closing

As you type, note that whenever you type an opening quote (“), parenthesis [(], square ([) or angle (<) bracket, or brace ({), the Editor automatically adds the corresponding closing character and positions the cursor between the two. Type whatever is required in the enclosure and hit Enter. This positions the cursor just beyond the closing character. However, if you move the cursor out of the enclosed space, to copy and paste some text for example, the Enter key reverts to its normal behavior of starting a new line.

In the case of an opening brace, the closing brace is positioned according to the currently selected coding style, and the cursor is properly indented.

Finally, notice that as you type, appropriate entries appear in the Outline view identifying header files, functions, and if we had any, global variables.

4.4 The Program

Before moving on to building and running the project, let’s take a closer look at what it actually does. main() itself is pretty straightforward. It’s just a set of calls to functions declared in sort_utils.c that do the real work.

The function read_file() reads in a data file that is assumed to be organized as one record per line, where a record is a text name and a numeric ID. It allocates memory for an array of records and a separate allocation for each name field.

There are two sort functions: one to sort on the name field, and the other to sort on the ID field. Both of these implement the shell sort algorithm, named after its inventor Donald Shell. Shell sort improves performance over the simpler insertion sort by comparing elements separated by a gap of several positions.

After the record array is sorted, write_sorted() writes it to stdout. This could be redirected to a file, of course.

The final step is to return all of the allocated memory in the function return_records().

The program does virtually no “sanity checking” and, if you’re so inclined, you might want to build some in. There’s also very little in the way of error checking.

4.5 Building the Project

Once you’ve completed and saved the record_sort.c file, the next step is to build the project. All files that are created in, or imported into, a project automatically become a part of it and are built and linked into the final executable.

In the Project Explorer view, select the top-level record_sort entry. Then execute Project→Build Project or right-click and select Build Configurations→Build All. In the former case, the Active Configuration will be built. By default this is the Debug configuration. The Active Configuration can be changed by executing Project→Build Configurations→Set Active.