packrat.c
// Read: Main function - chooses format automatically.
// Returns length of file on success, or a negative error code on failure
Main function - chooses format automatically.
Returns length of file on success, or a negative error code on failure
Main function - chooses format automatically . Returns length of file on success or a negative error code on failure
int packrat_read(const char * const pathPri, const char * const pathPrd, const int id, char ** const data) {
	if (pathPri == NULL || pathPrd == NULL || id < 0) return -1;
	const int pri = open(pathPri, O_RDONLY);
	if (pri == -1) return PACKRAT_ERROR_OPEN;
	char header[5];
	const ssize_t bytesRead = read(pri, header, 5);
	if (bytesRead != 5) return PACKRAT_ERROR_READWRITE;
	if (header[0] != 'P' || header[1] != 'r') return PACKRAT_ERROR_FILESIG;
	const int bitsPos = header[3];
	const int bitsLen = header[4];
	int ret = -1;
	if (header[2] == '0') ret = packrat_read_zero(pri, bitsPos, bitsLen, pathPrd, id, data);
	if (header[2] == 'C') ret = packrat_read_compact(pri, bitsPos, pathPrd, id, data);
	close(pri);
	return ret;
}
int packrat_read ( const char * const pathPri , const char * const pathPrd , const int id , char * * const data ) { if ( pathPri == NULL || pathPrd == NULL || id < 0 ) return -1 ; const int pri = open ( pathPri , O_RDONLY ) ; if ( pri == -1 ) return PACKRAT_ERROR_OPEN ; char header [ 5 ] ; const ssize_t bytesRead = read ( pri , header , 5 ) ; if ( bytesRead != 5 ) return PACKRAT_ERROR_READWRITE ; if ( header [ 0 ] != ' ' || header [ 1 ] != ' ' ) return PACKRAT_ERROR_FILESIG ; const int bitsPos = header [ 3 ] ; const int bitsLen = header [ 4 ] ; int ret = -1 ; if ( header [ 2 ] == ' ' ) ret = packrat_read_zero ( pri , bitsPos , bitsLen , pathPrd , id , data ) ; if ( header [ 2 ] == ' ' ) ret = packrat_read_compact ( pri , bitsPos , pathPrd , id , data ) ; close ( pri ) ; return ret ; }
Read: Main function - chooses format automatically.
Read : Main function - chooses format automatically .