示例#1
0
文件: book.cpp 项目: jokerlee/college
RecordList
PhoneBook::searchByName(const string & name)
{
    Iter i = _records.begin(), j;
    RecordList ret;
    while ((j=searchName(name, i++)) != _records.end())
        ret.push_back(*j);
    return ret;
}
void pa5functions::ReadInputFile( const char* inputFile,
                                  RecordList& recordList ){
    // File validation
    std::fstream fin( inputFile );
    if( !fin ){
        throw pa5_exception( "Invalid input file." );
    }

    // Parse the file line by line and tokenize
    std::string currentLine;
    std::list<std::string> currentTokens;
    std::list<std::string>::iterator tokenIter;
    while( !fin.eof() ){
        currentLine.clear();
        currentTokens.clear();

        getline( fin, currentLine );
        // Handle poorly formed, but not invalid input input
        if( currentLine.size() == 0 ){
            continue;
        }
        TokenizeString( currentLine, currentTokens, ',' );
        if( currentTokens.size() != 3 ){
            throw pa5_exception( "Invalid input file - format or data error." );
        }

        tokenIter = currentTokens.begin();
        StudentRecord record;
        record.name      = *(tokenIter);
        record.ssn       = *(++tokenIter);
        record.testScore = atoi((*(++tokenIter)).c_str());

        // Normalize string length.
        record.name = record.name.substr(0,12);
        record.ssn = record.ssn.substr(0,9);

        recordList.push_back( record );
    }
    fin.close();
}
示例#3
0
//---------------------------------------------------------------------------
void BinexReadWriteTest::process()
   throw()
{
   if (verboseLevel > 0)
   {
      cout << "Creating BINEX records . . ." << endl;
   }
   
   for (short recNum = 0; recNum < 10; recNum++)
   {
      BinexData record(recNum);
      TestDataList      recordData;
      size_t            offset = 0;
      
      for (short dataNum = 0; dataNum < 80; dataNum++)
      {
         TestDataType whichType = (TestDataType)(rand() % (eMGFZI + 1) );
         void         *value = NULL;
         
         switch (whichType)
         {
            case eChar:
            {
               char c = (char)(rand() % 0x100);
               record.updateMessageData(offset, c, sizeof(c) );
               value  = new char(c);
               break;
            }   
            case eShort:
            {
               short s = (short)(rand() % 10000);
               record.updateMessageData(offset, s, sizeof(s) );
               value  = new short(s);
               break;
            }  
            case eLong:
            {
               long l = (long)(lrand48() );
               record.updateMessageData(offset, l, sizeof(l) );
               value  = new long(l);
               break;
            }   
            case eUBNXI:
            {
               BinexData::UBNXI u( (unsigned long)(abs(lrand48() ) ) % BinexData::UBNXI::MAX_VALUE);
               record.updateMessageData(offset, u);
               value = new BinexData::UBNXI(u);
               break;
            }   
            case eMGFZI:
            {
               BinexData::MGFZI m( (long long)(lrand48() ) );
               record.updateMessageData(offset, m);
               value = new BinexData::MGFZI(m);
               break;
            }   
            default:
                  // Internal error
               exit(1);
         }
         if (value != NULL)
         {
            recordData.push_back(TestData(whichType, value) );
         }
      }
      testData.push_back(recordData);      
      testRecords.push_back(record);
   }

   if (verboseLevel > 0)
   {
      cout << "Verifying BINEX records . . ." << endl;
   }
   TestDataListList::iterator dataListIter = testData.begin();
   RecordList::iterator       recordIter   = testRecords.begin();
   bool more = true;
   while (  (dataListIter != testData.end() )
         && (recordIter   != testRecords.end() ) )
   {
      TestDataList      dataList = *dataListIter;
      BinexData record   = *recordIter;
      try
      {
         size_t offset = 0;
         TestDataList::iterator dataIter = (*dataListIter).begin();
         while (dataIter != (*dataListIter).end() )
         {
            switch ( (*dataIter).first)
            {
               case eChar:
               {
                  string desc = "Comparing character record message data";
                  char   c;
                  record.extractMessageData(offset, c, sizeof(c) );
                  if (memcmp( (void*)&c, (*dataIter).second, sizeof(c) ) )
                  {
                     report(desc, false);
                     cout << "  Actual:   " << c << endl;
                     cout << "  Expected: " << *( (char*)(*dataIter).second) << endl;
                  }
                  else
                  {
                     report(desc, true);
                  }
                  break;
               }   
               case eShort:
               {
                  string desc = "Comparing short record message data";
                  short  s;
                  record.extractMessageData(offset, s, sizeof(s) );
                  if (memcmp( (void*)&s, (*dataIter).second, sizeof(s) ) )
                  {
                     report(desc, false);
                     cout << "  Actual:   " << s << endl;
                     cout << "  Expected: " << *( (char*)(*dataIter).second) << endl;
                  }
                  else
                  {
                     report(desc, true);
                  }
                  break;
               }  
               case eLong:
               {
                  string desc = "Comparing long record message data";
                  long   l;
                  record.extractMessageData(offset, l, sizeof(l) );
                  if (memcmp( (void*)&l, (*dataIter).second, sizeof(l) ) )
                  {
                     report(desc, false);
                     cout << "  Actual:   " << l << endl;
                     cout << "  Expected: " << *( (char*)(*dataIter).second) << endl;
                  }
                  else
                  {
                     report(desc, true);
                  }
                  break;
               }   
               case eUBNXI:
               {
                  string           desc = "Comparing UBNXI record message data";
                  BinexData::UBNXI u;
                  record.extractMessageData(offset, u);
                  if (u == *( (BinexData::UBNXI*)(*dataIter).second) )
                  {
                     report(desc, true);
                  }
                  else
                  {
                     report(desc, false);
                     cout << "  Actual:   " << (unsigned long)u << endl;
                     cout << "  Expected: " << (unsigned long)*( (BinexData::UBNXI*)(*dataIter).second) << endl;
                  }
                  break;
               }   
               case eMGFZI:
               {
                  string           desc = "Comparing MGFZI record message data";
                  BinexData::MGFZI m;
                  record.extractMessageData(offset, m);
                  if (m == *( (BinexData::MGFZI*)(*dataIter).second) )
                  {
                     report(desc, true);
                  }
                  else
                  {
                     report(desc, false);
                     cout << "  Actual:   " << (long long)m << endl;
                     cout << "  Expected: " << (long long)*( (BinexData::MGFZI*)(*dataIter).second) << endl;
                  }
                  break;
               }   
               default:
                     // Internal error
                  exit(1);
            }
            dataIter++;
         }
      }
      catch (FFStreamError e)
      {
         cout << "  FFStreamError reading record." << endl;
      }
      catch (...)
      {
         cout << "  Unknown error reading record." << endl;
      }
      dataListIter++;
      recordIter++;
      
   }

   if (verboseLevel > 0)
   {
      cout << "Writing BINEX file . . ." << endl;
   }
   BinexStream outStream("test.out", std::ios::out | std::ios::binary);
   outStream.exceptions(ios_base::failbit | ios_base::badbit);
   recordIter = testRecords.begin();
   while (recordIter != testRecords.end() )
   {
      try
      {
         (*recordIter).putRecord(outStream);
      }
      catch(...)
      {
         cout << "  Error writing record." << endl;
      }
      recordIter++;
   }
   outStream.close();
   
   if (verboseLevel > 0)
   {
      cout << "Reading BINEX file . . ." << endl;
   }
   BinexStream inStream("test.out", std::ios::in | std::ios::binary);   
   inStream.exceptions(ios_base::failbit);
   recordIter = testRecords.begin();
   while (inStream.good() && (EOF != inStream.peek() ) )
   {
      if (recordIter == testRecords.end() )
      {
         cout << "Stored records exhausted before file records - exiting." << endl;
         break;
      }
      BinexData record;
      try
      {
         record.getRecord(inStream);
         if (record == *recordIter)
         {
            report("Reading and comparing BINEX record", true);
         }
         else
         {
            report("Reading and comparing BINEX record", false);
            cout << "Actual record:" << endl;
            (*recordIter).dump(cout);
            cout << "Expected record:" << endl;
            record.dump(cout);
         }
      }
      catch (FFStreamError e)
      {
         cout << e << endl;
      }
      catch (...)
      {
         cout << "  Unknown error reading record." << endl;
      }
      recordIter++;      
   }
   inStream.close();

}