Exemple #1
0
//------------------------------------------------------------
static
void
create_node_dict( hashTable<CC_String,BTCollectable> &dict,
		  BookCaseDB& db )
{
  DBTable *nodeMeta = db.table(BookCaseDB::NodeMeta, DB::READ);
  DBCursor cursor(*nodeMeta);

  const char *bookLocator;
  const char *nodeLocator;
  const char *filename;
  int line_num;
  const char *title;
  const char *stitle;
  const char *style;

  int throw_exception = 0;
  while(cursor.next(STRING_CODE, &bookLocator,
		    STRING_CODE, &nodeLocator,
		    STRING_CODE, &filename,
		    INTEGER_CODE, &line_num,
		    SKIP_CODE, /* TOC num */
		    STRING_CODE, &title,
		    STRING_CODE, &stitle,
		    STRING_CODE, &style,
		    NULL)){
    CC_String *key = new CC_String ( nodeLocator );
    BTCollectable       *value = new BTCollectable ( filename,line_num,bookLocator );

    BTCollectable    *val = (BTCollectable *)dict.findValue( key );
    if ( !val ) { 
      dict.insertKeyAndValue( key, value );
    }
    else {
      
      delete key;
      delete value;

      throw_exception = 1;
      cerr << "(ERROR) Duplicate section ID = " << (const char *)*key << endl
	   << "            found in file    = " << filename << endl
	   << "                  at line    = " << line_num << endl
	   << "          is in conflict with  " << endl
	   << "                  section ID = " << (const char *)*key << endl
	   << "            found in file    = " << val->filename() << endl
	   << "                  at line    = " << val->linenum() << "\n\n";

    }
  }

  if ( throw_exception ) {
    throw(Unexpected("Duplicate section IDs are found"));
  }



}
Exemple #2
0
//------------------------------------------------------------
static
void
checkTocLink( const char *toc_file_name, 
	      hashTable<CC_String,int> &linkTab, 
	      hashTable<CC_String,BTCollectable> &nodeTab )
{
  int flag = 0;

  char *ToCFileName = strdup ( toc_file_name );
  
  hashTableIterator<CC_String,BTCollectable> nodeit( nodeTab );
  while ( ++nodeit ) {

    CC_String *key = (CC_String *)nodeit.key();
    BTCollectable       *value = (BTCollectable *)nodeit.value();

    if ( !linkTab.contains( key ) ) {
      flag = 1;

      const char *file_name = value->filename();
      int line_num = value->linenum();

      cerr << "(ERROR) Section ID = " << (const char *)*key << endl
	   << "        of file    = " << file_name << endl
	   << "        at line    = " << line_num << endl
	   << "        does not have a corresponding link with the value  = " << (const char *)*key << endl
           << "        from the ToC file = " << ToCFileName << "\n\n";

    }
  }

  hashTableIterator<CC_String,int> linkit( linkTab );

  while ( ++linkit ) {
    CC_String *link = linkit.key();
    int *line_num = linkit.value();

    if ( !nodeTab.contains( link ) ) {
      flag = 1;
      cerr << "(ERROR) IDREF       = " << (const char *)*link << endl
	   << "        of ToC file = " << ToCFileName << endl
	   << "        at line     = " << *line_num << endl
           << "        is pointing to an unavailable node\n\n";

    }
  }
  
  delete ToCFileName;

  if ( flag ) {
    throw(Unexpected("TOC validation failed\n"));
  }
}
Exemple #3
0
void modify(char * input, hashTable& table){
	stock stock;
	table.retrieve(input, stock);

	float newNetAssetValue;
	char newDate[MAX_LEN];
	float newDateReturn;

	newNetAssetValue = getFloat("\tNew value: ");
	getString("\tNew date: ", newDate);
	newDateReturn = getFloat("\tNew return: ");

	table.modify(input, newNetAssetValue, newDate, newDateReturn);
}
Exemple #4
0
void makeData(hashTable & table)
{
    data aData;
    char ticker[100];
    char name[100];
    char date[100];
    float value, ytd;
    cin.ignore(100, '\n');
    cout << "Please enter the stock ticker: ";
    cin.get(ticker, 100, '\n');
    cin.ignore(100, '\n');
    cout << "Please enter the stock asset name: ";
    cin.get(name, 100, '\n');
    cin.ignore(100, '\n');
    cout << "Please etner the net asset value: ";
    cin >> value;
    cin.ignore(100, '\n');
    cout << "Please enter the date of that value: ";
    cin.get(date, 100, '\n');
    cin.ignore(100, '\n');
    cout << "Please enter the year-to-date return: ";
    cin >> ytd;
    cin.ignore(100, '\n');
    
    aData.setTicker(ticker);
    aData.setName(name);
    aData.setValue(value);
    aData.setDate(date);
    aData.setYtd(ytd);
    
    table.insert(aData);

}
Exemple #5
0
void loadDict() {
	string filename;
	cout << "Enter name of dictionary: ";
	cin >> filename;

	clock_t t1 = clock();
	ifstream inputDict(filename.c_str());
	if (!inputDict) {
		cerr << "Error: could not open " << filename << endl;
		exit(1);
	}

	string line;
	while (getline(inputDict, line)) {
		line = toLowerCaseSTD(line);
		if (line.find_first_not_of(validCharacters) != string::npos
				|| line.length() > 20) {
			continue;
		}
		dict.insert(line);
	}
	inputDict.close();

	clock_t t2 = clock();
	double timeDiff = ((double) (t2 - t1)) / CLOCKS_PER_SEC;
	cout << "Total time (in seconds) to load dictionary: " << timeDiff << endl;
}
Exemple #6
0
void spell_check(){
    int newline, start_index, end_index, line_number = 1;
    string line, word;
    while (getline(infile, line)){
        newline = line.find_last_not_of("\n");
        if (newline != string::npos){
            line.erase(newline+1);
        }
        start_index = line.find_first_of(valid_chars);
        while (start_index != string::npos){
            end_index = line.find_first_not_of(valid_chars, start_index);
            word = toLowercase(line.substr(start_index, end_index-start_index));
            start_index = line.find_first_of(valid_chars, end_index);
            if (word.find_first_of(digits) != string::npos){
                continue;
            }
            else if (word.length() > 20){
                outfile << "Long word at line " << line_number << ", starts: " << word.substr(0, 20) << endl;
            }
            else if (!dictionary.contains(word)){
                outfile << "Unknown word at line " << line_number << ": " << word << endl;
            }
            if (end_index == string::npos){
                break;
            }
        }
        line_number++;
    }
}
Exemple #7
0
void fileWrite(hashTable& table)
{
  ofstream outFile;
  //open file
  outFile.open (OUTFILENAME.c_str());
  //Write to file
  table.outputData(outFile, LISTSIZE);
  outFile.close();
}
Exemple #8
0
void menuoptions(char decider, hashTable & table)
{
    data aData;
    char key[100];
    switch (decider)
    {
        case 'd':
            table.display();
            break;
        case 'a':
            makeData(table);
            break;
        case 'm':
            table.monitor();
            break;
        case 'e':
            cout << "Enter the stock ticker you'd like to edit: ";
            cin.ignore(100, '\n');
            cin.get(key, 100, '\n');
            table.modify(key);
            break;
            
        case 'r':
            cout << "Enter the stock ticker you'd like to remove: ";
            cin.ignore(100, '\n');
            cin.get(key, 100, '\n');
            table.remove(key);
            break;
            
        case 'f':
            cout << "Enter the stock ticker you'd like to find: ";
            cin.ignore(100, '\n');
            cin.get(key, 100, '\n');
            table.retrieve(key, aData);
            aData.print();
            break;
            
        case 'q':
            cout << "------Program Terminated------";
            break;
            
    }
}
Exemple #9
0
void executeCmd(char command, hashTable& table)
{
	stock stock;
	char key[MAX_LEN];

	switch(command)
	{
	case 'a': getStock(stock);
			  table.add(stock);
			  cout << endl << "the stock has been saved in the database. " << endl;
		break;

	case 'r': getString("\nPlease enter the ticker symbol of the stock you want to remove: ", key);
		      table.remove(key);
			  cout << endl << key << " has been removed from the database. " << endl;
		break;

	case 'm': getString("\nPlease enter the ticker symbole of the stock you want to modify: ", key);
				modify(key, table);
				cout << endl << key << " has been modified. " << endl;
		break;

	case 's': getString("\nPlease enter the name of the stock you want to search: ", key);
			  table.retrieve (key, stock);
			  cout << endl << "Information about " << key << ": " << endl << '\t' << stock << endl;
		break;

	case 'l': display(table);
		break;

	case 'c': table.monitor();
		break;

	default: cout << "illegal command!" << endl;
		break;
	}
}
Exemple #10
0
void spellCheck() {
	string filenameIn, filenameOut, line, word;
	unsigned int startIndex, stopIndex;
	cout << "Enter name of input file: ";
	cin >> filenameIn;
	ifstream input(filenameIn.c_str());
	if (!input) {
		cerr << "Error: could not open " << filenameIn << endl;
		exit(1);
	}
	cout << "Enter name of output file: ";
	cin >> filenameOut;
	ofstream output(filenameOut.c_str());
	if (!output) {
		cerr << "Error: could not open " << filenameOut << endl;
		exit(1);
	}
	unsigned int lineNum = 1;
	while (getline(input, line)) {
		line = toLowerCaseSTD(line);
		int startIndex = line.find_first_of(validCharacters);
		int stopIndex = line.find_first_not_of(validCharacters, startIndex);
		while (startIndex != string::npos) {
			if (stopIndex == string::npos) {
				stopIndex = line.length();
			}
			word = line.substr(startIndex, stopIndex - startIndex);
			if (word.length() > 20) {
				output << "Long word at line " << lineNum << ", starts: "
						<< word.substr(0, 20) << endl;
			}else if (word.find_first_of(ignoreCharacters) != string::npos){

			}else if (!dict.contains(word)){
				output << "Unknown word at line " << lineNum << ": "<< word << endl;
			}

			if (stopIndex != line.length()) {
				line = line.substr(stopIndex + 1);
			} else {
				break;
			}
			startIndex = line.find_first_of(validCharacters);
			stopIndex = line.find_first_not_of(validCharacters, startIndex);
		}
		lineNum++;
	}
	input.close();
	output.close();
}
Exemple #11
0
bool fileRead(hashTable& table)
{
  int count = 0;
  string data;
  ifstream inFile;
  //Open file
  inFile.open(INFILENAME.c_str());
  //Read from file
  while (inFile >> data) {
	table.insert(data);
	count++;
  }
  inFile.close();
  return (count > 0);
}
Exemple #12
0
void loadDictionary(){
    string line;
    int newline;
    while (getline(dictfile, line)){ // Read in line from dictionary file
        newline = line.find_last_not_of("\n");
        if (newline != string::npos){
            line.erase(newline+1);
        }
        if (line.length() > 20 || line.find_first_not_of(valid_chars) != string::npos){
            continue;
        }
        dictionary.insert(toLowercase(line));
    }
    dictfile.close();
}
Exemple #13
0
//------------------------------------------------------------
static
void
node_table( DBCursor *node_meta_cursor,
	    const char *this_book,
	    hashTable<CC_String, BTCollectable> &this_node_table )
{
  assert( node_meta_cursor != NULL );
  assert( this_book != NULL );
 
  const char *bookLocator;
  const char *nodeLocator;
  const char *file_name;
  int line_num;
  const char *title;
  const char *stitle;
  const char *style;

  while ( node_meta_cursor->next(STRING_CODE, &bookLocator,
				 STRING_CODE, &nodeLocator,
				 STRING_CODE, &file_name,
				 INTEGER_CODE, &line_num,
				 SKIP_CODE, /* TOC num */
				 STRING_CODE, &title,
				 STRING_CODE, &stitle,
				 STRING_CODE, &style,
				 NULL)){
    /* this book has changed */
    if ( strcmp( bookLocator, this_book ) != 0 ) {
      node_meta_cursor->undoNext();
      break;
    }

    CC_String *key = new CC_String( nodeLocator );
    BTCollectable *value = new BTCollectable( file_name,line_num,bookLocator );
    this_node_table.insertKeyAndValue( key, value );

  }

}
Exemple #14
0
/**
 * fileHandle(char*, hashTable&), reads in a file and passes each word of the file
 * onto a tokenize function and inserts the word into a hashtable
**/
void fileHandle ( char* fileName, hashTable &h )
{
    ifstream in;
    in.open ( fileName, ios::in );

    if ( !in )
    {
        cout << "Unable to open " << fileName << ". Exiting program.\n";
        return;
    }

    string x;
    auto c1 = clock();

    while ( in >> x )
    {
        x = tokenize ( x );

        // we would add into a hash table here..
        // should only need to add in, in the insert function for
        // our hash table it should be able to handle everything else
        if ( x != "" && !h.insert ( x ) )
        {
            cout << "Unable to insert " << x << " into hashtable. Exiting program\n";
            return;
        }
    }

    cout << "Finished generating a hash table of size " << h.getSize() << ".\n";
    cout << "Read " << h.getNumWords() << " words from the file " << fileName << ".\n";
    cout << "Inserted " << h.getNumDistinct() << " distinct words into the hash table.\n";
    cout << "Compacting and sorting the hash table ... ";
    h.sort();
    cout << "finished!\n";

    auto c2 = clock();
    auto totalTime = c2 - c1;
    cout << fixed << showpoint;
    cout << "Elapsed time = " << setprecision ( 1 ) << totalTime / ( CLOCKS_PER_SEC / 1000.0 )  <<  " msec.\n";

    h.printStats ( fileName );
}
Exemple #15
0
//------------------------------------------------------------
static
void
locator_table( BookCaseDB& db,
	       hashTable<CC_String, BTCollectable> &hd)
{

  
  DBTable *locTable = db.table(BookCaseDB::Locator, DB::READ);
  
  DBCursor cursor(*locTable);

  const char *locator;
  const char *nodeloc;
  const char *reflabel;
  const char *filename;
  int line_num;

  int dup_count = 0;
  
  while(cursor.next(STRING_CODE, &locator,
		    STRING_CODE, &nodeloc,
		    STRING_CODE, &reflabel,
		    STRING_CODE, &filename,
		    INTEGER_CODE, &line_num,
		    NULL)){

  char *buf = new char[strlen(nodeloc) + strlen(reflabel) + 2];
  sprintf(buf, "%s\t%s", nodeloc, reflabel);
    
    CC_String *loc_collect = new CC_String( locator );
    BTCollectable *node_collect = new BTCollectable( filename, 
						     line_num, 
						     buf );

  delete[] buf;

    BTCollectable *val = hd.findValue( loc_collect );
    if ( !val ) {
      hd.insertKeyAndValue( loc_collect, node_collect );
    }
    else {

      delete loc_collect;
      delete node_collect;

      dup_count++;

      
      cerr << "(ERROR)   Duplicate ID  = " << locator << endl
           << "          found in file = " << filename << endl
	   << "                at line = " << line_num << endl
	   << "    is in conflict with   " << endl
	   << "                    ID  = " << locator << endl
	   << "          found in file = " << val->filename() << endl
	   << "                at line = " << val->linenum()  << "\n\n";
      

    }
      
  }

  if ( dup_count ) {
    throw(Unexpected(form("Number of duplicate IDs found = %d\n", 
			  dup_count)));
  }
}  
Exemple #16
0
void display(hashTable& table)
{
	table.display();
}