Читаем Thinking In C++. Volume 2: Practical Programming полностью

The Bin effector relies on the fact that shifting an unsigned number to the right shifts zeros into the high bits. We use numeric_limits::max( ) (the largest unsigned long value, from the standard header ) to produce a value with the high bit set, and this value is moved across the number in question (by shifting it to the right), masking each bit in turn. We’ve juxtaposed string literals in the code for readability; the separate strings are of course concatenated into one by the compiler.

Historically, the problem with this technique was that once you created a class called Fixw for char* or Bin for unsigned long, no one else could create a different Fixw or Bin class for their type. However, with namespaces, this problem is eliminated.

<p>Iostream examples</p>

In this section you’ll see some examples of what you can do with all the information you’ve learned in this chapter. Although many tools exist to manipulate bytes (stream editors such as sed and awk from UNIX are perhaps the most well known, but a text editor also fits this category), they generally have some limitations. Both sed and awk can be slow and can only handle lines in a forward sequence, and text editors usually require human interaction, or at least learning a proprietary macro language. The programs you write with iostreams have none of these limitations: they’re fast, portable, and flexible.

<p>Maintaining class library source code</p>

Generally, when you create a class, you think in library terms: you make a header file Name.h for the class declaration, and you create a file in which the member functions are implemented, called Name.cpp. These files have certain requirements: a particular coding standard (the program shown here uses the coding format for this book), and in the header file the declarations are generally surrounded by some preprocessor statements to prevent multiple declarations of classes. (Multiple declarations confuse the compiler—it doesn’t know which one you want to use. They could be different, so it throws up its hands and gives an error message.).

This example allows you to create a new header/implementation pair of files or to modify an existing pair. If the files already exist, it checks and potentially modifies the files, but if they don’t exist, it creates them using the proper format.

//: C04:Cppcheck.cpp

// Configures .h & .cpp files to conform to style

// standard. Tests existing files for conformance.

#include

#include

#include

#include "../require.h"

using namespace std;

bool startsWith(const string& base, const string& key) {

  return base.compare(0, key.size(), key) == 0;

}

void cppCheck(string fileName) {

  enum bufs { BASE, HEADER, IMPLEMENT,

    HLINE1, GUARD1, GUARD2, GUARD3,

    CPPLINE1, INCLUDE, BUFNUM };

  string part[BUFNUM];

  part[BASE] = fileName;

  // Find any '.' in the string:

  size_t loc = part[BASE].find('.');

  if(loc != string::npos)

    part[BASE].erase(loc); // Strip extension

  // Force to upper case:

  for(size_t i = 0; i < part[BASE].size(); i++)

    part[BASE][i] = toupper(part[BASE][i]);

  // Create file names and internal lines:

  part[HEADER] = part[BASE] + ".h";

  part[IMPLEMENT] = part[BASE] + ".cpp";

  part[HLINE1] = "//" ": " + part[HEADER];

  part[GUARD1] = "#ifndef " + part[BASE] + "_H";

  part[GUARD2] = "#define " + part[BASE] + "_H";

  part[GUARD3] = "#endif // " + part[BASE] +"_H";

  part[CPPLINE1] = string("//") + ": "

    + part[IMPLEMENT];

  part[INCLUDE] = "#include \"" + part[HEADER] + "\"";

  // First, try to open existing files:

  ifstream existh(part[HEADER].c_str()),

           existcpp(part[IMPLEMENT].c_str());

  if(!existh) { // Doesn't exist; create it

    ofstream newheader(part[HEADER].c_str());

    assure(newheader, part[HEADER].c_str());

    newheader << part[HLINE1] << endl

      << part[GUARD1] << endl

      << part[GUARD2] << endl << endl

      << part[GUARD3] << endl;

  } else { // Already exists; verify it

    stringstream hfile; // Write & read

    ostringstream newheader; // Write

    hfile << existh.rdbuf();

    // Check that first three lines conform:

    bool changed = false;

    string s;

    hfile.seekg(0);

    getline(hfile, s);

    bool lineUsed = false;

    // The call to good() is for Microsoft (later too)

    for (int line = HLINE1; hfile.good() && line <= GUARD2;

         ++line) {

      if(startsWith(s, part[line])) {

        newheader << s << endl;

        lineUsed = true;

        if (getline(hfile, s))

          lineUsed = false;

      } else {

        newheader << part[line] << endl;

        changed = true;

        lineUsed = false;

      }

    }

    // Copy rest of file

    if (!lineUsed)

      newheader << s << endl;

    newheader << hfile.rdbuf();

    // Check for GUARD3

    string head = hfile.str();

    if(head.find(part[GUARD3]) == string::npos) {

      newheader << part[GUARD3] << endl;

      changed = true;

    }

    // If there were changes, overwrite file:

    if(changed) {

      existh.close();

      ofstream newH(part[HEADER].c_str());

      assure(newH, part[HEADER].c_str());

      newH << "//@//\n"  // Change marker

        << newheader.str();

    }

  }

  if(!existcpp) { // Create cpp file

    ofstream newcpp(part[IMPLEMENT].c_str());

    assure(newcpp, part[IMPLEMENT].c_str());

    newcpp << part[CPPLINE1] << endl

Перейти на страницу:

Похожие книги

1С: Бухгалтерия 8 с нуля
1С: Бухгалтерия 8 с нуля

Книга содержит полное описание приемов и методов работы с программой 1С:Бухгалтерия 8. Рассматривается автоматизация всех основных участков бухгалтерии: учет наличных и безналичных денежных средств, основных средств и НМА, прихода и расхода товарно-материальных ценностей, зарплаты, производства. Описано, как вводить исходные данные, заполнять справочники и каталоги, работать с первичными документами, проводить их по учету, формировать разнообразные отчеты, выводить данные на печать, настраивать программу и использовать ее сервисные функции. Каждый урок содержит подробное описание рассматриваемой темы с детальным разбором и иллюстрированием всех этапов.Для широкого круга пользователей.

Алексей Анатольевич Гладкий

Программирование, программы, базы данных / Программное обеспечение / Бухучет и аудит / Финансы и бизнес / Книги по IT / Словари и Энциклопедии
1С: Управление торговлей 8.2
1С: Управление торговлей 8.2

Современные торговые предприятия предлагают своим клиентам широчайший ассортимент товаров, который исчисляется тысячами и десятками тысяч наименований. Причем многие позиции могут реализовываться на разных условиях: предоплата, отсрочка платежи, скидка, наценка, объем партии, и т.д. Клиенты зачастую делятся на категории – VIP-клиент, обычный клиент, постоянный клиент, мелкооптовый клиент, и т.д. Товарные позиции могут комплектоваться и разукомплектовываться, многие товары подлежат обязательной сертификации и гигиеническим исследованиям, некондиционные позиции необходимо списывать, на складах периодически должна проводиться инвентаризация, каждая компания должна иметь свою маркетинговую политику и т.д., вообщем – современное торговое предприятие представляет живой организм, находящийся в постоянном движении.Очевидно, что вся эта кипучая деятельность требует автоматизации. Для решения этой задачи существуют специальные программные средства, и в этой книге мы познакомим вам с самым популярным продуктом, предназначенным для автоматизации деятельности торгового предприятия – «1С Управление торговлей», которое реализовано на новейшей технологической платформе версии 1С 8.2.

Алексей Анатольевич Гладкий

Финансы / Программирование, программы, базы данных