Skip to content
Snippets Groups Projects
Document.cpp 2.12 KiB
Newer Older
  • Learn to ignore specific revisions
  • 
    #include "Document.h"
    
    string Document::DocToString ( )
    	{
    
    vcday's avatar
    vcday committed
    	return string( docString, strlen( docString ) ) + "\n";
    
    vcday's avatar
    vcday committed
    int Document::WriteToDocMap ( )
    
    vcday's avatar
    vcday committed
    	pthread_mutex_lock( &docMap_mutex );
    
    
    	//for now just write url
    
    
    vcday's avatar
    vcday committed
    	string loc = util::GetCurrentWorkingDir( ) + filepath::DOC_MAP;
    	int file = util::getFileDescriptor( loc.c_str( ), "W" );
    
    	off_t resultPosition = 0;
    
    	try
    		{
    		//check if its available
    		if ( file == -1 )
    			{
    			throw ( "error opening docMap" );
    			}
    		else
    			{
    			//get the current size of the docMap
    
    vcday's avatar
    vcday committed
    			size_t seekPosition = util::FileSize( file );
    
    			//seek to the end of the file
    
    vcday's avatar
    vcday committed
    			resultPosition = lseek( file, seekPosition, SEEK_SET );
    
    
    			if ( resultPosition == -1 )
    				{
    				throw ( "Could not seek" );
    				}
    			cout << "Current docMap position on disk" << endl;
    			cout << resultPosition << endl;
    
    
    vcday's avatar
    vcday committed
    			size_t success = write( file, this->DocToString( ).c_str( ),
    			                        strlen( this->DocToString( ).c_str( ) ) );
    
    			if ( success == -1 )
    				{
    				throw ( "Error writing document object to document map" );
    				}
    			}
    		}
    	catch ( const char *str )
    		{
    		cerr << str << endl;
    
    vcday's avatar
    vcday committed
    		close( file );
    		pthread_mutex_unlock( &docMap_mutex );
    
    		return -1;
    		}
    
    vcday's avatar
    vcday committed
    	close( file );
    	pthread_mutex_unlock( &docMap_mutex );
    
    	return resultPosition;
    	}
    
    
    
    vcday's avatar
    vcday committed
    ParsedUrl Document::getUrl ( )
    	{
    	return this->url;
    	}
    
    
    
    vcday's avatar
    vcday committed
    void Document::PrintDocMap ( string url, int location )
    
    vcday's avatar
    vcday committed
    	pthread_mutex_lock( &docMap_mutex );
    
    
    	std::cout << url << " is " << location;
    
    
    vcday's avatar
    vcday committed
    	string loc = util::GetCurrentWorkingDir( ) + filepath::DOC_MAP;
    	int file = util::getFileDescriptor( loc.c_str( ), "R" );
    
    
    
    	//check if its available
    	if ( file )
    		{
    
    vcday's avatar
    vcday committed
    		off_t resultPosition = lseek( file, ( size_t ) location, SEEK_SET );
    
    		int bytes = 14;
    		if ( bytes > 0 )
    			{
    			char *buffer = new char[bytes];
    			ssize_t bytesRead;
    
    vcday's avatar
    vcday committed
    			if ( ( bytesRead = read( file, buffer, bytes ) ) )
    				write( 1, buffer, bytesRead );
    
    			else
    				{
    				cerr << "Could not read " << bytes << " bytes at position " <<
    
    vcday's avatar
    vcday committed
    				     resultPosition << ", error = " << errno;
    				pthread_mutex_unlock( &docMap_mutex );
    
    vcday's avatar
    vcday committed
    	pthread_mutex_unlock( &docMap_mutex );
    
    vcday's avatar
    vcday committed
    	};