/*
skriver en post til DocumentIndex
*/
void DIWrite (struct DocumentIndexFormat *DocumentIndexPost, unsigned int DocID,char subname[], char *diname) {


	FILE *file;
	
	if ((file = GetFileHandler(DocID,'w',subname, diname)) != NULL) {

		#ifdef BLACK_BOX
			unsigned int CurrentDocumentIndexVersionAsUInt = CurrentDocumentIndexVersion;

			if (fwrite(&CurrentDocumentIndexVersionAsUInt,sizeof(unsigned int),1,file) != 1) {
	                        perror("Can't write");
        	                exit(1);
                	}
		#endif
		//skriver posten
		if (fwrite(DocumentIndexPost,sizeof(struct DocumentIndexFormat),1,file) != 1) {
			perror("Can't write");
			exit(1);
		}
		#ifndef WITHOUT_DIWRITE_FSYNC
			fsync(fileno(file));
		#endif
	}
	else {
		printf("Cant get fh\n");
	}

	//hvis vi ikke har på DI_FILE_CASHE må vi lokke filen
	#ifndef DI_FILE_CASHE
		fclose(file);
	#endif
}
int DIRead_fmode (struct DocumentIndexFormat *DocumentIndexPost, int DocID,char subname[], char filemode) {

	FILE *file;
	int forReturn = 0;

	#ifdef DEBUG
		printf("DIRead_fmode(DocID=%i, subname=\"%s\")\n",DocID,subname);
	#endif

	#ifdef DISK_PROTECTOR
		dp_lock(rLotForDOCid(DocID));
	#endif

	if ((file = GetFileHandler(DocID,filemode,subname, NULL)) != NULL) {

		if (DIRead_post_fh(DocumentIndexPost,file)) {
			forReturn = 1;
		}

		//hvis vi ikke har på DI_FILE_CASHE må vi lokke filen
		#ifndef DI_FILE_CASHE
			fclose(file);
		#endif
		
        }
        else {
		printf("can't open DocumentIndexPost for DocID %u.\n",DocID);
        }


        if ((*DocumentIndexPost).htmlSize != 0) {
                (*DocumentIndexPost).htmlSize2 = (*DocumentIndexPost).htmlSize;
        }


	#ifdef DISK_PROTECTOR
		dp_unlock(rLotForDOCid(DocID));
	#endif

	return forReturn;

}
int CArchiveDir::FileSize(int handle)
{
	return GetFileHandler(handle)->FileSize();
}
bool CArchiveDir::Eof(int handle)
{
	return GetFileHandler(handle)->Eof();
}
int CArchiveDir::Peek(int handle)
{
	return GetFileHandler(handle)->Peek();
}
void CArchiveDir::Seek(int handle, int pos)
{
	GetFileHandler(handle)->Seek(pos);
}
void CArchiveDir::CloseFile(int handle)
{
	delete GetFileHandler(handle);
	fileHandles.erase(handle);
}
int CArchiveDir::ReadFile(int handle, void* buffer, int numBytes)
{
	CFileHandler* f = GetFileHandler(handle);
	f->Read(buffer, numBytes);
	return 0;
}