Bruce Eckel - Thinking In C++. Volume 2 - Practical Programming

Здесь есть возможность читать онлайн «Bruce Eckel - Thinking In C++. Volume 2 - Practical Programming» весь текст электронной книги совершенно бесплатно (целиком полную версию без сокращений). В некоторых случаях можно слушать аудио, скачать через торрент в формате fb2 и присутствует краткое содержание. Год выпуска: 2003, ISBN: 2003, Издательство: Prentice Hall, Жанр: Программирование, на английском языке. Описание произведения, (предисловие) а так же отзывы посетителей доступны на портале библиотеки ЛибКат.

Thinking In C++. Volume 2: Practical Programming: краткое содержание, описание и аннотация

Предлагаем к чтению аннотацию, описание, краткое содержание или предисловие (зависит от того, что написал сам автор книги «Thinking In C++. Volume 2: Practical Programming»). Если вы не нашли необходимую информацию о книге — напишите в комментариях, мы постараемся отыскать её.

Best selling author Bruce Eckel has joined forces with Chuck Allison to write
, the sequel to the highly received and best selling
. Eckel is the master of teaching professional programmers how to quickly learn cutting edge topics in C++ that are glossed over in other C++ books. In
, the authors cover the finer points of exception handling, defensive programming and string and stream processing that every C++ programmer needs to know. Special attention is given to generic programming where the authors reveal little known techniques for effectively using the Standard Template Library. In addition, Eckel and Allison demonstrate how to apply RTTI, design patterns and concurrent programming techniques to improve the quality of industrial strength C++ applications. This book is targeted at programmers of all levels of experience who want to master C++.

Thinking In C++. Volume 2: Practical Programming — читать онлайн бесплатно полную книгу (весь текст) целиком

Ниже представлен текст книги, разбитый по страницам. Система сохранения места последней прочитанной страницы, позволяет с удобством читать онлайн бесплатно книгу «Thinking In C++. Volume 2: Practical Programming», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

//{L} ../TestSuite/Test

This section will present a program to just extract all the code so that you can compile and inspect it manually. You can use this program to extract all the code in this book by saving the document file as a text file [36] Beware that some versions of Microsoft Word erroneously replace single quote characters with an extended ASCII character when you save a document as text, which of course causes a compile error. We have no idea why this happens. Just replace the character manually with an apostrophe. (let’s call it TICV2.txt) and by executing something like the following on a shell command line: .

C:> extractCode TICV2.txt /TheCode

This command reads the text file TICV2.txtand writes all the source code files in subdirectories under the top-level directory /TheCode. The directory tree will look like the following:

TheCode/

C0B/

C01/

C02/

C03/

C04/

C05/

C06/

C07/

C08/

C09/

C10/

C11/

TestSuite/

The source files containing the examples from each chapter will be in the corresponding directory .

Here’s the program:

//: C03:ExtractCode.cpp

// Extracts code from text

#include

#include

#include

#include

#include

#include

#include

using namespace std;

// Legacy non-standard C header for mkdir()

#ifdef __GNUC__

#include

#elif defined(__BORLANDC__) || defined(_MSC_VER)

#include

#else

#error Compiler not supported

#endif

// Check to see if directory exists

// by attempting to open a new file

// for output within it.

bool exists(string fname) {

size_t len = fname.length();

if(fname[len-1] != '/' && fname[len-1] != '\\')

fname.append("/");

fname.append("000.tmp");

ofstream outf(fname.c_str());

bool existFlag = outf;

if (outf) {

outf.close();

remove(fname.c_str());

}

return existFlag;

}

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

// See if input file name provided

if(argc == 1) {

cerr << "usage: extractCode file [dir]\n";

exit(EXIT_FAILURE);

}

// See if input file exists

ifstream inf(argv[1]);

if(!inf) {

cerr << "error opening file: " << argv[1] << endl;

exit(EXIT_FAILURE);

}

// Check for optional output directory

string root("./"); // current is default

if(argc == 3) {

// See if output directory exists

root = argv[2];

if(!exists(root)) {

cerr << "no such directory: " << root << endl;

exit(EXIT_FAILURE);

}

size_t rootLen = root.length();

if(root[rootLen-1] != '/' && root[rootLen-1] != '\\')

root.append("/");

}

// Read input file line by line

// checking for code delimiters

string line;

bool inCode = false;

bool printDelims = true;

ofstream outf;

while (getline(inf, line)) {

size_t findDelim = line.find("//" "/:~");

if(findDelim != string::npos) {

// Output last line and close file

if (!inCode) {

cerr << "Lines out of order\n";

exit(EXIT_FAILURE);

}

assert(outf);

if (printDelims)

outf << line << endl;

outf.close();

inCode = false;

printDelims = true;

} else {

findDelim = line.find("//" ":");

if(findDelim == 0) {

// Check for '!' directive

if(line[3] == '!') {

printDelims = false;

++findDelim; // To skip '!' for next search

}

// Extract subdirectory name, if any

size_t startOfSubdir =

line.find_first_not_of(" \t", findDelim+3);

findDelim = line.find(':', startOfSubdir);

if (findDelim == string::npos) {

cerr << "missing filename information\n" << endl;

exit(EXIT_FAILURE);

}

string subdir;

if(findDelim > startOfSubdir)

subdir = line.substr(startOfSubdir,

findDelim - startOfSubdir);

// Extract file name (better be one!)

size_t startOfFile = findDelim + 1;

size_t endOfFile =

line.find_first_of(" \t", startOfFile);

if(endOfFile == startOfFile) {

cerr << "missing filename\n";

exit(EXIT_FAILURE);

}

// We have all the pieces; build fullPath name

string fullPath(root);

if(subdir.length() > 0)

fullPath.append(subdir).append("/");

assert(fullPath[fullPath.length()-1] == '/');

if (!exists(fullPath))

#ifdef __GNUC__

mkdir(fullPath.c_str(), 0); // Create subdir

#else

mkdir(fullPath.c_str()); // Create subdir

#endif

fullPath.append(line.substr(startOfFile,

endOfFile - startOfFile));

outf.open(fullPath.c_str());

if(!outf) {

cerr << "error opening " << fullPath

<< " for output\n";

exit(EXIT_FAILURE);

}

inCode = true;

cout << "Processing " << fullPath << endl;

if(printDelims)

outf << line << endl;

}

else if(inCode) {

assert(outf);

outf << line << endl; // output middle code line

}

}

}

exit(EXIT_SUCCESS);

} ///:~

First, you’ll notice some conditional compilation directives. The mkdir( )function, which creates a directory in the file system, is defined by the POSIX [37] POSIX, an IEEE standard, stands for “Portable Operating System Interface” and is a generalization of many of the low-level system calls found in UNIX systems. standard in the header . Unfortunately, many compilers still use a different header ( ). The respective signatures for mkdir( )also differ: POSIX specifies two arguments, the older versions just one. For this reason, there is more conditional compilation later in the program to choose the right call to mkdir( ). We normally don’t use conditional compilation in the examples in this book, but this particular program is too useful not to put a little extra work into, since you can use it to extract all the code with it .

The exists( )function in ExtractCode.cpptests whether a directory exists by opening a temporary file in it. If the open fails, the directory doesn’t exist. You remove a file by sending its name as a char*to std::remove( ) .

The main program validates the command-line arguments and then reads the input file a line at a time, looking for the special source code delimiters. The Boolean flag inCodeindicates that the program is in the middle of a source file, so lines should be output. The printDelimsflag will be true if the opening token is not followed by an exclamation point; otherwise the first and last lines are not written. It is important to check for the closing delimiter first, because the start token is a subset of it, and searching for the start token first would return a successful find for both cases. If we encounter the closing token, we verify that we are in the middle of processing a source file; otherwise, something is wrong with the way the delimiters are laid out in the text file. If inCodeis true, all is well, and we (optionally) write the last line and close the file. When the opening token is found, we parse the directory and file name components and open the file. The following string-related functions were used in this example: length( ), append( ), getline( ), find( )(two versions), find_first_not_of( ), substr( ), find_first_of( ), c_str( ), and, of course, operator<<( ) .

Читать дальше
Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

Похожие книги на «Thinking In C++. Volume 2: Practical Programming»

Представляем Вашему вниманию похожие книги на «Thinking In C++. Volume 2: Practical Programming» списком для выбора. Мы отобрали схожую по названию и смыслу литературу в надежде предоставить читателям больше вариантов отыскать новые, интересные, ещё непрочитанные произведения.


Отзывы о книге «Thinking In C++. Volume 2: Practical Programming»

Обсуждение, отзывы о книге «Thinking In C++. Volume 2: Practical Programming» и просто собственные мнения читателей. Оставьте ваши комментарии, напишите, что Вы думаете о произведении, его смысле или главных героях. Укажите что конкретно понравилось, а что нет, и почему Вы так считаете.

x