Esempio n. 1
0
bool spi_writeFlash(uint8_t *buf, uint32_t addr, uint32_t size) {
    uint32_t wrote, pageWriteSize;
    bool ok=false;
    do {
        if(!unWriteProtect())
            { printf("Write protected.\n"); break; }
        for(wrote=0; wrote<size; wrote+=pageWriteSize) {
            pageWriteSize=PAGESIZE-(addr & (PAGESIZE-1));   //bytes left in page
            if(pageWriteSize>size-wrote)
                pageWriteSize=size-wrote;
            if(!pageWrite(addr+wrote, buf+wrote, pageWriteSize))
                break;
            if((addr+wrote)%0x800==0)
                printf(".");
        }
        printf("\n");
        ok=(wrote==size);
    } while(0);
    return ok;
}
int insertEntry(string fName,int pageSize)
{
	char *buffer = new char [pageSize];
	int pageID = 1;

	ifstream fd;
	fd.open(fName.c_str(),ios::binary);
	if(!fd)
	{
		cerr<<"\n File does not exist.....";
		return -1;
	}
	fd.seekg((pageID-1)*pageSize,ifstream::beg);
	fd.read(buffer,pageSize);
	fd.close();
	//int result1 = pageRead(fName,pageID,buffer,pageSize);
	SysColumns * sysColumns = new SysColumns(buffer,pageSize);

	SysColumnsEntry newSysColumnsEntry;
	newSysColumnsEntry.setTableName("kp1");
	newSysColumnsEntry.setDBName("k1");
	newSysColumnsEntry.setColumnName("c1");
	newSysColumnsEntry.setColumnDefault("########");
	newSysColumnsEntry.setDataType("INT");
	newSysColumnsEntry.setRemarks("None");
	newSysColumnsEntry.setIsNullable('Y');
	newSysColumnsEntry.setUpdatable('Y');
	newSysColumnsEntry.setForeignKey('N');
	newSysColumnsEntry.setGeneratedAttribute(' ');
	newSysColumnsEntry.setHidden('N');
	newSysColumnsEntry.setScale(0);
	newSysColumnsEntry.setKeyPos(0);
	newSysColumnsEntry.setOrdinalPosition(2);
	newSysColumnsEntry.setLength(20);

	char *dataEntry = new char [SYSCOLUMNENTRYSIZE];

	int result = newSysColumnsEntry.fillBuffer(dataEntry);

	bool inserted = 0;

	int nextSysColumnsPage,insertedPage;
	int _pageSize = pageSize;

	memcpy(&nextSysColumnsPage,&buffer[NEXTPTR],sizeof(int));

	while(1)
	{
		if(sysColumns->checkEntryInsertion() == 1)
		{
			sysColumns->createNewSysColumnEntry(dataEntry,buffer);
			inserted = 1;
			insertedPage = sysColumns->getpageID();
			break;
		}
		if(nextSysColumnsPage==-1 && inserted != 1)
			break;
		//fd.seekg((nextSysTablePage-1)*pageSize,ifstream::beg);
		//fd.read(buffer,pageSize);
		memcpy(&nextSysColumnsPage,&buffer[NEXTPTR],sizeof(int));
	}
	if(inserted == 0)
	{
		cout<<"Entry could not be inserted.... So, continue trying to search for empty page for SysColumns"<<endl;
		delete buffer;
		delete dataEntry;
		delete sysColumns;
		return -1;
	}
	else
	{
		cout<<"Column Entry was inserted into "<<insertedPage<<" page"<<endl;
		result = pageWrite(fName,insertedPage,buffer,pageSize);
		delete buffer;
		delete dataEntry;
		delete sysColumns;
		return 1;
	}
}
int writeSysColumnBufferCreate(string fName,SysColumns newPage)
{
	ofstream fd;
	fd.open(fName.c_str(),ios::binary);
	if(!fd)
	{
		cerr<<"\n Error in creating the file";
		return -1;
	}

	/*There will be no pages to read.....
	result = pageRead(_pageID,buffer); // From Chinmay, get buffer and page from cache........
	if(result == -1)
	{
		cout<<"Page could not be read...."<<endl;
		return -1;
	}*/

	int pageSize = newPage.getPageSize();
	int _pageSize = pageSize;
	int _pageID = newPage.getpageID();
	short _pagePriority = newPage.getpagePriority();
	int _nextSysColumnsPage = newPage.getnextSysColumnsPage();
	int _curSysColumnsSize = newPage.getCurSysColumnsSize(); // Check this is not required... This can be got by METADATASIZE + noe*TABLEENTRYSIZE
	int _remSysColumnsSize = newPage.getRemSysColumnsSize();
	long _sysColumnsEntryPointer = newPage.getSysColumnsEntryPointer();
	int _noOfEntries = newPage.getNoOfEntries();

	char *buffer = new char [pageSize];
	int result;

	cout<<"Object contents....: "<<endl;
	cout<<"Remaining size: "<<_remSysColumnsSize<<endl;
	cout<<"MetaData Size: "<<SYSCOLUMNMETADATASIZE<<endl;
	cout<<"Entry Space: "<<SYSCOLUMNENTRYSPACE<<endl;
	cout<<"Page Size: "<<PAGESIZE<<endl;

	// Copying contents to buffer.....
	cout<<"PageID PTR: "<<PAGEIDPTR<<" Value: "<<_pageID<<endl;
	memcpy(&buffer[PAGEIDPTR],&_pageID,sizeof(int));
	cout<<"PagePriority PTR: "<<PAGEPRIPTR<<" Value: "<<_pagePriority<<endl;
	memcpy(&buffer[PAGEPRIPTR],&_pagePriority,sizeof(short));
	cout<<"Next PTR: "<<NEXTPTR<<" Value: "<<_nextSysColumnsPage<<endl;
	memcpy(&buffer[NEXTPTR],&_nextSysColumnsPage,sizeof(int));
	cout<<"CurrentSysColSize PTR: "<<CURSYSCOLSIZEPTR<<" Value: "<<_curSysColumnsSize<<endl;
	memcpy(&buffer[CURSYSCOLSIZEPTR],&_curSysColumnsSize,sizeof(int));
	cout<<"RemSysColSize PTR: "<<REMSYSCOLSIZEPTR<<" Value: "<<_remSysColumnsSize<<endl;
	memcpy(&buffer[REMSYSCOLSIZEPTR],&_remSysColumnsSize,sizeof(int));
	cout<<"SysColEntry PTR: "<<SYSCOLUMNENTRYPTR<<" Value: "<<_sysColumnsEntryPointer<<endl;
	memcpy(&buffer[SYSCOLUMNENTRYPTR],&_sysColumnsEntryPointer,sizeof(long));
	cout<<"NoOfEnt PTR: "<<SYSCOLNOEPTR<<" Value: "<<_noOfEntries<<endl;
	memcpy(&buffer[SYSCOLNOEPTR],&_noOfEntries,sizeof(int));

	// Copying contents to buffer.....
	/*memcpy(&buffer[PAGEIDPTR],&_pageID,sizeof(int));
	memcpy(&buffer[PAGEPRIPTR],&_pagePriority,sizeof(short));
	memcpy(&buffer[NEXTPTR],&_nextSysColumnsPage,sizeof(int));
	memcpy(&buffer[CURSYSCOLSIZEPTR],&_curSysColumnsSize,sizeof(int));
	memcpy(&buffer[REMSYSCOLSIZEPTR],&_remSysColumnsSize,sizeof(int));
	memcpy(&buffer[SYSCOLUMNENTRYPTR],&_sysColumnsEntryPointer,sizeof(long));
	memcpy(&buffer[SYSCOLNOEPTR],&_noOfEntries,sizeof(int));*/

	// WriteBuffer.... Chinmay's function call here.....
	result = pageWrite(fd,_pageID,buffer,pageSize);

	if(result == -1)
	{
		cout<<"Page could not be read...."<<endl;
		return -1;
	}

	fd.close();
	return _pageSize;
}