#include <cstring>
#include <cstdio>
#include <cstdlib>
#include "rar.h"

void* open_rar(char *filename) {
	struct RAROpenArchiveData road;
	road.ArcName = filename;
	road.OpenMode = RAR_OM_EXTRACT;
	road.CmtBuf = 0;
	HANDLE rar_file = RAROpenArchive(&road);
	if(road.OpenResult != 0) return 0;
	return rar_file;
}

int get_fileinfo_from_rar(void *rfile, char *name, unsigned int *flags) {
	if(!rfile) return ERAR_BAD_DATA;
	RARHeaderData hd;
	memset(&hd,0,sizeof(hd));
	int status = RARReadHeader(rfile,&hd);
	if(status) return status;
	if(name) {
		name[259] = 0;
		strncpy(name,hd.FileName,260);
	}
	if(flags) *flags = hd.Flags;
	return 0;
}

int extract_file_from_rar(void *rfile, char *path, char *name) {
	return RARProcessFile(rfile,RAR_EXTRACT,path,name);
}

int close_rar(void *rfile) {
	return RARCloseArchive(rfile);
}
