1
0
mirror of https://github.com/stefanocasazza/ULib.git synced 2025-09-28 19:05:55 +08:00
ULib/include/ulib/magic/magic.h
2015-01-23 17:24:36 +01:00

123 lines
2.4 KiB
C++

// =================================================================================
//
// = LIBRARY
// ULib - c++ library
//
// = FILENAME
// magic.h - interface to the libmagic library (Magic Number Recognition Library)
//
// = AUTHOR
// Stefano Casazza
//
// =================================================================================
#ifndef U_MAGIC_H
#define U_MAGIC_H 1
#include <ulib/string.h>
#include <magic.h>
class UHttpClient_Base;
class UMimeMultipartMsg;
// identify a file's format by scanning binary data for patterns
class U_EXPORT UMagic {
public:
// Check for memory error
U_MEMORY_TEST
// Allocator e Deallocator
U_MEMORY_ALLOCATOR
U_MEMORY_DEALLOCATOR
// COSTRUTTORI
UMagic(int flags)
{
U_TRACE_REGISTER_OBJECT(0, UMagic, "%d", flags)
if (magic == 0) (void) init();
U_INTERNAL_ASSERT_POINTER(magic)
(void) setFlags(flags);
}
/**
* Deletes this object
*/
~UMagic()
{
U_TRACE_UNREGISTER_OBJECT(0, UMagic)
}
// VARIE
static void clear()
{
U_TRACE(1, "UMagic::clear()")
if (magic)
{
U_SYSCALL_VOID(magic_close, "%p", magic);
magic = 0;
}
}
static const char* getError()
{
U_TRACE(1, "UMagic::getError()")
U_INTERNAL_ASSERT_POINTER(magic)
const char* result = (const char*) U_SYSCALL(magic_error, "%p", magic);
U_RETURN(result);
}
static bool setFlags(int flags = MAGIC_NONE)
{
U_TRACE(1, "UMagic::setFlags(%d)", flags)
U_INTERNAL_ASSERT_POINTER(magic)
bool result = (U_SYSCALL(magic_setflags, "%p,%d", magic, flags) != -1);
U_RETURN(result);
}
static bool init(int flags = MAGIC_MIME);
static UString getType(const char* buffer, uint32_t buffer_len);
static UString getType(const UString& buffer) { return getType(U_STRING_TO_PARAM(buffer)); }
// DEBUG
#if defined(U_STDCPP_ENABLE) && defined(DEBUG)
const char* dump(bool reset) const;
#endif
protected:
static magic_t magic; /* pointer to magic :-) */
private:
#ifdef U_COMPILER_DELETE_MEMBERS
UMagic(const UMagic&) = delete;
UMagic& operator=(const UMagic&) = delete;
#else
UMagic(const UMagic&) {}
UMagic& operator=(const UMagic&) { return *this; }
#endif
friend class UHttpClient_Base;
friend class UMimeMultipartMsg;
};
#endif