常用函数

获取当前目录
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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值