DICOMDataObject*
LoadImplicitLittleEndianFile(char* filename, unsigned int iVrSizeLimit)
{
	DICOMDataObject*	pDDO;
	int			handle;
	char			Buf[128 + 4];
	PDU_Service		PDU;
	unsigned int		iLastTime, iCurTime;
	CBufferedIO*		pBufferedIO;

	PDU.AttachRTC(&VRType);

#ifdef WIN32	
	handle = sopen( filename, O_RDONLY | O_BINARY, SH_DENYNO, S_IREAD);
#else
	handle = open( filename, O_RDONLY);
#endif

	if (handle == -1)
	{
		return NULL;
	}

	pBufferedIO = new CBufferedIO(iVrSizeLimit < 0x1000 ? 0x1000 : 0x8000);
	/* Find out whether marker 'DICM' exists */
	if (pBufferedIO->read(handle, Buf, 128 + 4) != 128 + 4)
	{
		delete pBufferedIO;
		close(handle);
		return NULL;
	}
	else
	{
		if (strncmp(Buf + 128, "DICM", 4) != 0)
		{
			pBufferedIO->lseek(handle, 0, SEEK_SET);
			/* We are at beginning of the VRs */
			pDDO = new DICOMDataObject;
			LoadImplicitLittleEndian(pDDO, pBufferedIO, handle, iVrSizeLimit);
//iLastTime = timeGetTime();
			delete pBufferedIO;
			close(handle);
//iCurTime = timeGetTime();
//printf("fopen=%d\r\n", iCurTime - iLastTime);
			return pDDO;
		}
		else
		{
			/* The function 'LoadImplicitLittleEndianFile' is not intended for
			   DICM files, however don't be fuzzy about it... */
			delete pBufferedIO;
			close(handle);
			pDDO = PDU.LoadDICOMDataObject(filename);
			return pDDO;
		}
	}
}
BOOL
RegenToDatabase(Database *DB, char *basename, char *filename, const char *device)
	{
	DICOMDataObject*	pDDO;
	int			len;
	PDU_Service		PDU;
	
	PDU.AttachRTC(&VRType);

	len = strlen(filename);
	
	// The excludes some impossible short names (including path) like a.v2 which are unlikely to occur
	
	if (len < 4) return FALSE;
	
	if (stricmp(filename + len - 3, ".v2")==0 )
		{
		pDDO = LoadImplicitLittleEndianFile(filename, 0x4000);	// increased from 400 to allow load large icons
		if(!pDDO)
			{
			OperatorConsole.printf("***[Regen] %s -FAILED: Error on Load\n", filename);
			return ( FALSE );
			}
		}
	else if (stricmp(filename + len - 4, ".dcm")==0 ||
	         stricmp(filename + len - 4, ".img")==0 ||
	         strchr(filename + len - 4, '.')==NULL
		)
		{
		pDDO = PDU.LoadDICOMDataObject(filename);
		if(!pDDO)
			{
			OperatorConsole.printf("***[Regen] %s -FAILED: Error on Load\n", filename);
			return ( FALSE );
			}
		}
	else
		return FALSE;

	if(!SaveToDataBase(*DB, pDDO, basename, device, FALSE))
		{
		delete pDDO;
		OperatorConsole.printf("***[Regen] %s -FAILED: Error SQL Add\n", filename);
		return ( FALSE );
		}
	delete pDDO;
	OperatorConsole.printf("[Regen] %s -SUCCESS\n", filename);
	return ( TRUE );
	}
DICOMDataObject*
OrgLoadImplicitLittleEndianFile(char* filename, unsigned int iVrSizeLimit)
{
	DICOMDataObject*	pDDO;
	FILE*			fp;
	char			Buf[128 + 4];
	PDU_Service		PDU;

	PDU.AttachRTC(&VRType);

	fp = fopen(filename, "rb");
	if(!fp)
	{
		return NULL;
	}
	/* Find out whether marker 'DICM' exists */
	if (fread(Buf, 1, 128 + 4, fp) != 128 + 4)
	{
		fclose(fp);
		return NULL;
	}
	else
	{
		if (strncmp(Buf + 128, "DICM", 4) != 0)
		{
			fseek(fp, 0, SEEK_SET);
			/* We are at beginning of the VRs */
			pDDO = new DICOMDataObject;
			OrgLoadImplicitLittleEndian(pDDO, fp, iVrSizeLimit);
			fclose ( fp );
			return pDDO;
		}
		else
		{
			/* The function 'LoadImplicitLittleEndianFile' is not intended for
			   DICM files, however don't be fuzzy about it... */
			fclose(fp);
			pDDO = PDU.LoadDICOMDataObject(filename);
			return pDDO;
		}
	}
}