I am getting the following error:
PII.cpp:266: error: 'struct std::string' has no member named 'getfirstblock'
And I'm not sure how to fix it, since "int block" has to be equal to the firstblock of the file, how can I fix that error?
Also, my "getfirstblock" function should return the block number of the first block in string file but I am not sure how to implement it correctly, any suggestions?
Any help is appreciated, thanks in advance
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <sstream>
using namespace std;
//Pdisk Class
class Pdisk
{
public:
Pdisk(); //default constructor
Pdisk(string diskname, int numberofblocks, int blocksize);
int getblock(int blocknumber, string& buffer);
int putblock(int blocknumber, string buffer);
int getblocksize();
int getnumberofblocks();
private:
string diskname; //file name of pseudo-disk
int numberofblocks; //number of blocks on disk
int blocksize; //block size in bytes
};
#include <cmath>
#include <vector>
#include <string>
class Filesys
{
public:
Filesys(Pdisk& disk);
Filesys();
int fsclose();
int fssynch();
int newfile(string file);
int removefile(string file);
int getfirstblock(string file);
int addblock(string file, string block);
int delblock(string file, int blocknumber);
int getblock(string file, int blocknumber, string& buffer);
int putblock(string file, int blocknumber, string buffer);
int nextblock(string file, int blocknumber);
private:
bool findfile( string file, int* index = NULL );
private:
Pdisk &disk;
int rootsize;
int fatsize;
vector<string> filename;
vector<int> firstblock;
vector<int> fat;
};
//Blocking
vector<string>block(string s, int b)
{
float numberofblocks = (float)s.size( ) / b;
vector<string>blocks;
if(s.size() % b == 0)
{
numberofblocks = s.size()/b+1;
}
else
{
numberofblocks = s.size()/b+1;
}
string tempblock;
for(int i = 0; i < numberofblocks; i++)
{
tempblock = s.substr(b*i,b);
blocks.push_back(tempblock);
}
int lastblock = blocks.size() - 1;
for(int i = blocks[lastblock].size(); i < b; i++)
{
blocks[lastblock] += "#";
}
return blocks;
}
// Constructor for File System
Filesys::Filesys(Pdisk& disk)
:disk(disk),
rootsize(disk.getblocksize()/10),
fatsize(4*disk.getnumberofblocks()/disk.getblocksize()),
filename(rootsize,"XXXXX"),
firstblock(rootsize,0),
fat(disk.getnumberofblocks(),0)
{
string buffer;
disk.getblock(2, buffer);
if (buffer[0] == '#')
{
fat[0] = fatsize + 2;
for(int i = fatsize + 2; i < disk.getnumberofblocks() - 1; ++i)
{
fat[i] = i + 1;
fat[fat.size( )-1] = 0;//
}
//fat[fat.size( )-1] = 0;
}
else
{
string root;//was by string buffer
disk.getblock(1,root);
istringstream rootstream(root);
for(int i = 0; i < rootsize; ++i )
rootstream >> filename[i] >> firstblock[i];
string FAT;
for(int i = 0; i < fatsize; ++i)
{
string temp;
disk.getblock(i + 2, temp);
FAT += temp;
}
istringstream fatstream(FAT);
for(int i = 0; i < disk.getnumberofblocks( ); ++i)
fatstream >> fat[i];
}
fssynch();
}
//FsSynch
int Filesys::fssynch( )
{
// Output root directory to disk.
ostringstream rootstream;
for( int i = 0; i < rootsize; ++i )
rootstream << filename[i] << " " << firstblock[i] << " ";
string root = rootstream.str( );
for( int j = root.size( ); j < disk.getblocksize( ); ++j )
root += "#";
disk.putblock( 1, root );
// Output FAT to disk.
ostringstream fatstream;
for( int i = 0; i < disk.getnumberofblocks( ); ++i )
fatstream << fat[i] << " ";
string fat = fatstream.str( );
for( int j = fat.size( ); j < fatsize * disk.getblocksize( ); ++j )
fat += "#";
for( int k = 0, block = 2; k < fatsize; ++k )
{
string temp = fat.substr( k * disk.getblocksize( ), disk.getblocksize( ) );
disk.putblock( k + block, temp );
}
return 1;
}
//NewFile
int Filesys::newfile( string file )
{
if( findfile( file ) )
return 0; // File found.
if( file.size( ) > 5 )
{
cout << "Filename '" << file << "' too large, max size is " << 5 << " chars!" << endl;
return 0; // Filename too large.
}
for( int i = 0; i < filename.size( ); ++i )
if( filename[i] == "XXXXX" )
{
filename[i] = file;
}
}
//RemoveFile
int Filesys::removefile(string file)
{
int index;
if( findfile( file, &index ) )
if( firstblock[index] == 0 )
{
filename[index] = "XXXXX";
fssynch( );
return 1;
}
return 0; // File not empty or was not found.
}
//GetFirstBlock
int Filesys::getfirstblock(string file)
{
/*int* index = NULL;
return firstblock[*index];*/
int index;
if( findfile( file, &index ) )
return firstblock[index];
return 0; // File does not exist.
}
//AddBlock
int Filesys::addblock( string file, string buffer )
{
if( fat[0] == 0 )
return -1; // File system full.
int index;
if( !findfile( file, &index ) )
return 0; // File not found.
// Update free list.
int ablock = fat[0];
fat[0] = fat[ablock];
// Get first block.
int fblock = firstblock[index];
// If the file is empty, update first block list.
if( fblock == 0 )
{
firstblock[index] = ablock;
fat[ablock] = 0;
}
// Search through FAT for the last block in file.
else
{
while( fat[fblock] != 0 )
fblock = fat[fblock];
fat[fblock] = ablock;
fat[ablock] = 0;
}
disk.putblock( ablock, buffer );
fssynch( );
return ablock;
}
//DeleteBlock
int Filesys::delblock(string file, int blocknumber)
{
bool found = false;
int block = file.getfirstblock();
if (block == 0)
{
return 0;
}
if(block == blocknumber)
{
found = true;
for (int i = 0; i < rootsize; i++)
{
if(filename[i] == file)
firstblock[i] = fat[block];
break;
}
}
else // blocknumber is not the first block
{
int i = block;
while(fat[i] != 0)
{
if(fat[i] != 0)
{
fat[i] == fat[fat[i]];
found == true;
}
}
}
if (!found)
{
return 0;
}
fat[blocknumber] = fat[0];
fat[0] = blocknumber;
fssynch();
}
//GetBlock
int Filesys::getblock( string file, int blocknumber, string& buffer )
{
disk.getblock( blocknumber, buffer );
return 1;
}
//PutBlock
int Filesys::putblock( string file, int blocknumber, string buffer )
{
disk.putblock( blocknumber, buffer );
return 1;
}
//NextBlock
int Filesys::nextblock( string file, int blocknumber )
{
//return -1; // Block does not belong to file.
return fat[blocknumber];
//return -1; // File not found.
}
//FindFile
bool Filesys::findfile( string file, int* index )
{
for( int i = 0; i < filename.size( ); ++i )
if( filename[i] == file )
{
if( index ) *index = i;
return true;
}
return false;
}
//Pdisk - Default Constructor
Pdisk::Pdisk()
{
}
//Pdisk
Pdisk::Pdisk(string disk_name, int number_of_blocks, int block_size)
{
diskname = disk_name;
numberofblocks = number_of_blocks;
blocksize = block_size;
fstream output(diskname.c_str(),ios::out);
int i, j;
for(i = 1; i <= numberofblocks; i++)
{
for(j = 1; j <= blocksize; j++)
{
output << "#";
}
}
output.close();
}
//GetBlock
int Pdisk::getblock(int blocknumber, string& buffer)
{
fstream input;
input.open (diskname.c_str(), ios::in);
if (input.bad())
{
return 0;
}
if (input.is_open())
{
input.seekg(blocknumber * blocksize + 1, ios::beg);
char x;
for(int i = 0; i < blocksize; i++)
{
x = input.get();
buffer = buffer + x;
}
input.close();
return 1;
}
}
//PutBlock
int Pdisk::putblock(int blocknumber, string buffer)
{
fstream output;
output.open(diskname.c_str(),ios::in | ios :: out);
if(output.bad())
{
return 0;
}
output.seekp(blocknumber * blocksize + 1, ios::beg);
char x;
for(int i = 0; i < buffer.length(); i++)
{
x = buffer[i];
output.put(x);
}
output.close();
return 1;
}
//GetBlockSize
int Pdisk::getblocksize()
{
return blocksize;
}
//GetNumberOfBlocks
int Pdisk::getnumberofblocks()
{
return numberofblocks;
}
//Drivers
int main()
{
Pdisk disk1("test.txt",128,128);
Filesys fsys(disk1);
fsys.delblock("XXXXX", 6);
}