获取当前目录
读取资源文件头
char *path_getcwd(char *buffer, int size) {
#ifdef WIN32
if (!GetCurrentDirectoryA(size, buffer)) {
LOG_ERROR("GetCurrentDirectoryA() failed, system error: %d", sys_get_errno());
return 0;
}
#else
if (!getcwd(buffer, size)) {
LOG_ERROR("getcwd() failed, system error: %d", sys_get_errno());
return 0;
}
#endif // WIN32
return buffer;
}
各类型转换成string
template< class T >
inline
std::string
to_string( T const & x)
{
std::ostringstream out;
out << x;
return out.str();
}
资源加载相关的函数
template<class T>
int read_from_memory(const char *&p_memory, uint &mem_size, uint read_size, T &value) {
if (!p_memory || (mem_size < (sizeof(T) * read_size) ) )
return 0;
else {
value = *((T *)p_memory);
p_memory += read_size * sizeof(T);
mem_size -= sizeof(T) * read_size;
return 1;
}
}
template<class T>
int read_from_memory_p(const char *&p_memory, uint &mem_size, uint read_size, T **value) {
if (!p_memory || !value || (mem_size < (sizeof(T) * read_size)) )
return 0;
else {
*value = (T *)p_memory;
p_memory += read_size * sizeof(T);
mem_size -= sizeof(T) * read_size;
return 1;
}
}
template<class T>
int read_copy_from_memory(const char *&p_memory, uint &mem_size, uint read_size, T *value) {
if (!p_memory || !value || (mem_size < (sizeof(T) * read_size)))
return 0;
else {
memcpy(value, p_memory, sizeof(T) * read_size);
p_memory += read_size*sizeof(T);
mem_size -= sizeof(T) * read_size;
return 1;
}
}
读取资源文件头
static int read_mem_header(const char *head_str, const char *&memory, uint &memsize,int with_line = 1) {
const char *pmem = memory;
const char *phead = head_str;
uint i = 0;
uint head_size = strlen(head_str);
if (head_size > memsize || head_size > 16 || head_size <= 0)
return 0;
while (i < head_size && *phead++ == *pmem++)
i++;
/* Not find head_str */
if (i != head_size)
return 0;
/* Get head_str */
if (with_line && *pmem == '\n')
pmem++;
memsize -= (pmem - memory);
memory = pmem;
return 1;
}