Some more error handling

This commit is contained in:
Abs62 2014-04-24 22:50:47 +04:00
parent f8dd01e0a2
commit fbcaf7c2cb
7 changed files with 123 additions and 28 deletions

View file

@ -141,6 +141,9 @@ bool Babylon::readBlock( bgl_block &block )
if( block.length )
{
block.data = (char *)malloc( block.length );
if( !block.data )
return false;
unsigned res = gzread( file, block.data, block.length );
if( block.length != res )
{
@ -800,6 +803,11 @@ void Babylon::convertToUtf8( std::string &s, unsigned int type )
char *inbuf;
inbuf = (char *)s.data();
outbuf = (char*)malloc( outbufbytes + 1 );
if( !outbuf )
{
qFatal( "Memory allocation error in Babylon::convertToUtf8()" );
exit(1);
}
memset( outbuf, '\0', outbufbytes + 1 );
defbuf = outbuf;
while (inbufbytes) {

View file

@ -46,6 +46,7 @@ DEF_EX_STR( exCantReadFile, "Can't read file", Dictionary::Ex )
DEF_EX( exFailedToReadLineFromIndex, "Failed to read line from index file", Dictionary::Ex )
DEF_EX( exMalformedIndexFileLine, "Malformed index file line encountered", Dictionary::Ex )
DEF_EX( exInvalidBase64, "Invalid base64 sequence encountered", Dictionary::Ex )
DEF_EX_STR( exDictzipError, "DICTZIP error", Dictionary::Ex )
enum
{
@ -157,10 +158,12 @@ DictdDictionary::DictdDictionary( string const & id,
// Open the .dict file
dz = dict_data_open( dictionaryFiles[ 1 ].c_str(), 0 );
DZ_ERRORS error;
dz = dict_data_open( dictionaryFiles[ 1 ].c_str(), &error, 0 );
if ( !dz )
throw exCantReadFile( dictionaryFiles[ 1 ] );
throw exDictzipError( string( dz_error_str( error ) )
+ "(" + getDictionaryFilenames()[ 1 ] + ")" );
// Initialize the index
@ -638,7 +641,9 @@ vector< sptr< Dictionary::Class > > makeDictionaries(
// After tab1 should be article offset, after tab2 -- article size
uint32_t articleOffset = decodeBase64( string( tab1 + 1, tab2 - tab1 - 1 ) );
uint32_t articleSize = decodeBase64( tab2 + 1 );
dictData * dz = dict_data_open( dictFiles[ 1 ].c_str(), 0 );
DZ_ERRORS error;
dictData * dz = dict_data_open( dictFiles[ 1 ].c_str(), &error, 0 );
if ( dz )
{
@ -661,6 +666,9 @@ vector< sptr< Dictionary::Class > > makeDictionaries(
}
dict_data_close( dz );
}
else
throw exDictzipError( string( dz_error_str( error ) )
+ "(" + dictFiles[ 1 ] + ")" );
}
}
else

View file

@ -257,8 +257,9 @@ static void err_internal( const char *routine, const char *format, ... )
# endif
#endif
static int dict_read_header( const char *filename,
dictData *header, int computeCRC )
static enum DZ_ERRORS dict_read_header( const char *filename,
dictData *header,
int computeCRC )
{
FILE *str;
int id1, id2, si1, si2;
@ -276,7 +277,7 @@ static int dict_read_header( const char *filename,
{
err_fatal_errno( __func__,
"Cannot open data file \"%s\" for read\n", filename );
return 1;
return DZ_ERR_OPENFILE;
}
header->filename = NULL;//str_find( filename );
@ -302,7 +303,7 @@ static int dict_read_header( const char *filename,
}
header->crc = crc;
fclose( str );
return 0;
return DZ_NOERROR;
}
header->type = DICT_GZIP;
@ -334,7 +335,7 @@ static int dict_read_header( const char *filename,
"dzip header version %d not supported\n",
header->version );
fclose( str );
return 1;
return DZ_ERR_UNSUPPORTED_FORMAT;
}
header->chunkLength = getc( str ) << 0;
@ -344,10 +345,14 @@ static int dict_read_header( const char *filename,
if (header->chunkCount <= 0) {
fclose( str );
return 5;
return DZ_ERR_INVALID_FORMAT;
}
header->chunks = xmalloc( sizeof( header->chunks[0] )
* header->chunkCount );
if( header->chunks == 0 ) {
return DZ_ERR_NOMEMORY;
}
for (i = 0; i < header->chunkCount; i++) {
header->chunks[i] = getc( str ) << 0;
header->chunks[i] |= getc( str ) << 8;
@ -368,7 +373,9 @@ static int dict_read_header( const char *filename,
__func__,
"too long FNAME field in dzip file \"%s\"\n", filename);
fclose( str );
return 1;
if( header->chunks )
free( header->chunks );
return DZ_ERR_INVALID_FORMAT;
}
}
@ -389,7 +396,9 @@ static int dict_read_header( const char *filename,
__func__,
"too long COMMENT field in dzip file \"%s\"\n", filename);
fclose( str );
return 1;
if( header->chunks )
free( header->chunks );
return DZ_ERR_INVALID_FORMAT;
}
}
@ -412,7 +421,9 @@ static int dict_read_header( const char *filename,
"File position (%lu) != header length + 1 (%d)\n",
ftell( str ), header->headerLength + 1 );
fclose( str );
return 1;
if( header->chunks )
free( header->chunks );
return DZ_ERR_INVALID_FORMAT;
}
fseek( str, -8, SEEK_END );
@ -429,6 +440,12 @@ static int dict_read_header( const char *filename,
/* Compute offsets */
header->offsets = xmalloc( sizeof( header->offsets[0] )
* header->chunkCount );
if( header->offsets == 0 ) {
if( header->chunks )
free( header->chunks );
return DZ_ERR_NOMEMORY;
}
for (offset = header->headerLength + 1, i = 0;
i < header->chunkCount;
i++)
@ -438,19 +455,29 @@ static int dict_read_header( const char *filename,
}
fclose( str );
return 0;
return DZ_NOERROR;
}
dictData *dict_data_open( const char *filename, int computeCRC )
dictData *dict_data_open( const char *filename,
enum DZ_ERRORS * error,
int computeCRC )
{
dictData *h = NULL;
// struct stat sb;
int j;
if (!filename)
return NULL;
{
*error = DZ_ERR_OPENFILE;
return NULL;
}
h = xmalloc( sizeof( struct dictData ) );
if( h == 0 )
{
*error = DZ_ERR_NOMEMORY;
return 0;
}
memset( h, 0, sizeof( struct dictData ) );
#ifdef __WIN32
@ -463,7 +490,8 @@ dictData *dict_data_open( const char *filename, int computeCRC )
#ifdef __WIN32
wchar_t wname[16384];
#endif
if (dict_read_header( filename, h, computeCRC )) {
*error = dict_read_header( filename, h, computeCRC );
if ( *error != DZ_NOERROR ) {
break; /*
err_fatal( __func__,
"\"%s\" not in text or dzip format\n", filename );*/
@ -471,12 +499,18 @@ dictData *dict_data_open( const char *filename, int computeCRC )
#ifdef __WIN32
if( MultiByteToWideChar( CP_UTF8, 0, filename, -1, wname, 16384 ) == 0 )
{
*error = DZ_ERR_OPENFILE;
break;
}
h->fd = CreateFileW( wname, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0);
if( h->fd == INVALID_HANDLE_VALUE )
{
*error = DZ_ERR_OPENFILE;
break;
}
h->size = GetFileSize( h->fd, 0 );
#else
@ -484,6 +518,7 @@ dictData *dict_data_open( const char *filename, int computeCRC )
if ( !h->fd )
{
*error = DZ_ERR_OPENFILE;
break;
/*err_fatal_errno( __func__,
"Cannot open data file \"%s\"\n", filename );*/
@ -501,6 +536,7 @@ dictData *dict_data_open( const char *filename, int computeCRC )
h->cache[j].count = 0;
}
*error = DZ_NOERROR;
return h;
}
dict_data_close( h );
@ -563,7 +599,7 @@ char *dict_data_read_ (
buffer = xmalloc( size + 1 );
if( !buffer )
{
strcpy( h->errorString, "Cannot allocate memory" );
strcpy( h->errorString, dz_error_str( DZ_ERR_NOMEMORY ) );
return 0;
}
@ -603,7 +639,7 @@ char *dict_data_read_ (
fread( buffer, size, 1, h->fd ) != 1 )
#endif
{
strcpy( h->errorString, "Cannot read file" );
strcpy( h->errorString, dz_error_str( DZ_ERR_READFILE ) );
xfree( buffer );
return 0;
}
@ -681,6 +717,12 @@ char *dict_data_read_ (
if (!h->cache[target].inBuffer)
h->cache[target].inBuffer = xmalloc( h->chunkLength );
inBuffer = h->cache[target].inBuffer;
if( !inBuffer )
{
sprintf( h->errorString, dz_error_str( DZ_ERR_NOMEMORY ) );
xfree( buffer );
return 0;
}
if (h->chunks[i] >= OUT_BUFFER_SIZE ) {
/*
@ -705,6 +747,7 @@ char *dict_data_read_ (
fread( outBuffer, h->chunks[ i ], 1, h->fd ) != 1 )
#endif
{
sprintf( h->errorString, dz_error_str( DZ_ERR_READFILE ) );
xfree( buffer );
return 0;
}
@ -780,7 +823,7 @@ char *dict_data_read_ (
xfree( buffer );
return 0;
}
h->errorString[ 0 ] = 0;
return buffer;
}
@ -788,3 +831,18 @@ char *dict_error_str( dictData *data )
{
return data->errorString;
}
const char * dz_error_str( enum DZ_ERRORS error )
{
switch( error )
{
case DZ_NOERROR: return "No error";
case DZ_ERR_OPENFILE: return "Open file error";
case DZ_ERR_READFILE: return "Read file error";
case DZ_ERR_INVALID_FORMAT: return "Invalid file format";
case DZ_ERR_UNSUPPORTED_FORMAT: return "Unsupported file format";
case DZ_ERR_NOMEMORY: return "Memory allocation error";
case DZ_ERR_INTERNAL: return "Internal error";
}
return "Unknown error";
}

View file

@ -48,6 +48,16 @@ typedef struct dictCache {
int count;
} dictCache;
enum DZ_ERRORS {
DZ_NOERROR = 0,
DZ_ERR_INTERNAL,
DZ_ERR_OPENFILE,
DZ_ERR_READFILE,
DZ_ERR_UNSUPPORTED_FORMAT,
DZ_ERR_INVALID_FORMAT,
DZ_ERR_NOMEMORY
};
typedef struct dictData {
#ifdef __WIN32
HANDLE fd; /* file handle */
@ -86,7 +96,7 @@ typedef struct dictData {
/* initialize .data file */
extern dictData *dict_data_open (
const char *filename, int computeCRC);
const char *filename, enum DZ_ERRORS * error, int computeCRC);
/* */
extern void dict_data_close (
dictData *data);
@ -99,6 +109,8 @@ extern char *dict_data_read_ (
extern char *dict_error_str( dictData *data );
extern const char *dz_error_str( enum DZ_ERRORS error );
extern int mmap_mode;
#ifdef __cplusplus

7
dsl.cc
View file

@ -77,6 +77,7 @@ namespace {
DEF_EX_STR( exCantReadFile, "Can't read file", Dictionary::Ex )
DEF_EX( exUserAbort, "User abort", Dictionary::Ex )
DEF_EX_STR( exDictzipError, "DICTZIP error", Dictionary::Ex )
enum
{
@ -394,10 +395,12 @@ void DslDictionary::doDeferredInit()
// Open the .dsl file
dz = dict_data_open( getDictionaryFilenames()[ 0 ].c_str(), 0 );
DZ_ERRORS error;
dz = dict_data_open( getDictionaryFilenames()[ 0 ].c_str(), &error, 0 );
if ( !dz )
throw exCantReadFile( getDictionaryFilenames()[ 0 ] );
throw exDictzipError( string( dz_error_str( error ) )
+ "(" + getDictionaryFilenames()[ 0 ] + ")" );
// Read the abrv, if any

View file

@ -68,6 +68,7 @@ DEF_EX( exDicttypeNotSupported, "Dictionaries with dicttypes are not supported,
DEF_EX_STR( exCantReadFile, "Can't read file", Dictionary::Ex )
DEF_EX_STR( exWordIsTooLarge, "Enountered a word that is too large:", Dictionary::Ex )
DEF_EX_STR( exSuddenEndOfFile, "Sudden end of file", Dictionary::Ex )
DEF_EX_STR( exDictzipError, "DICTZIP error", Dictionary::Ex )
DEF_EX_STR( exIncorrectOffset, "Incorrect offset encountered in file", Dictionary::Ex )
@ -228,10 +229,12 @@ StardictDictionary::StardictDictionary( string const & id,
{
// Open the .dict file
dz = dict_data_open( dictionaryFiles[ 2 ].c_str(), 0 );
DZ_ERRORS error;
dz = dict_data_open( dictionaryFiles[ 2 ].c_str(), &error, 0 );
if ( !dz )
throw exCantReadFile( dictionaryFiles[ 2 ] );
throw exDictzipError( string( dz_error_str( error ) )
+ "(" + dictionaryFiles[ 2 ] + ")" );
// Initialize the index

11
xdxf.cc
View file

@ -63,6 +63,7 @@ namespace {
DEF_EX_STR( exCantReadFile, "Can't read file", Dictionary::Ex )
DEF_EX_STR( exNotXdxfFile, "The file is not an XDXF file:", Dictionary::Ex )
DEF_EX( exCorruptedIndex, "The index file is corrupted", Dictionary::Ex )
DEF_EX_STR( exDictzipError, "DICTZIP error", Dictionary::Ex )
enum
{
@ -221,10 +222,12 @@ XdxfDictionary::XdxfDictionary( string const & id,
// Open the file
dz = dict_data_open( dictionaryFiles[ 0 ].c_str(), 0 );
DZ_ERRORS error;
dz = dict_data_open( dictionaryFiles[ 0 ].c_str(), &error, 0 );
if ( !dz )
throw exCantReadFile( dictionaryFiles[ 0 ] );
throw exDictzipError( string( dz_error_str( error ) )
+ "(" + dictionaryFiles[ 0 ] + ")" );
// Read the abrv, if any
@ -714,8 +717,8 @@ GzippedFile::GzippedFile( char const * fileName ) throw( exCantReadFile )
if ( !gz )
throw exCantReadFile( fileName );
dz = dict_data_open( fileName, 0 );
DZ_ERRORS error;
dz = dict_data_open( fileName, &error, 0 );
}
GzippedFile::~GzippedFile()