Книга: Embedded Linux development using Eclipse

4.4 The Program

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.

Оглавление книги


Генерация: 0.131. Запросов К БД/Cache: 2 / 0
поделиться
Вверх Вниз