2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2022-04-25 19:15:46 +08:00
|
|
|
Copyright (c) 2020 C4games Ltd
|
2023-01-01 18:41:48 +08:00
|
|
|
Copyright (c) 2021-2023 Bytedance Inc.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2022-07-16 10:43:05 +08:00
|
|
|
#ifndef __AX_UTILS_H__
|
|
|
|
#define __AX_UTILS_H__
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "2d/Node.h"
|
|
|
|
#include "base/Macros.h"
|
|
|
|
#include "base/RefPtr.h"
|
|
|
|
#include "base/Data.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "renderer/backend/Types.h"
|
|
|
|
#include "math/Mat4.h"
|
2023-09-02 19:56:50 +08:00
|
|
|
#include "math/Mat3.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** @file ccUtils.h
|
|
|
|
Misc free functions
|
|
|
|
*/
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
/*
|
|
|
|
ccNextPOT function is licensed under the same license that is used in Texture2D.m.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Returns the Next Power of Two value.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
- If "value" is 15, it will return 16.
|
|
|
|
- If "value" is 16, it will return 16.
|
|
|
|
- If "value" is 17, it will return 32.
|
|
|
|
@param value The value to get next power of two.
|
|
|
|
@return Returns the next power of two value.
|
|
|
|
@since v0.99.5
|
|
|
|
*/
|
|
|
|
|
|
|
|
int ccNextPOT(int value);
|
|
|
|
|
|
|
|
class Sprite;
|
|
|
|
class Image;
|
|
|
|
|
|
|
|
namespace utils
|
|
|
|
{
|
2021-12-06 13:00:44 +08:00
|
|
|
/** Capture the entire screen.
|
|
|
|
* To ensure the snapshot is applied after everything is updated and rendered in the current frame,
|
|
|
|
* we need to wrap the operation with a custom command which is then inserted into the tail of the render queue.
|
|
|
|
* @param afterCaptured specify the callback function which will be invoked after the snapshot is done.
|
|
|
|
* @param filename specify a filename where the snapshot is stored. This parameter can be either an absolute path or a
|
|
|
|
* simple base filename ("hello.png" etc.), don't use a relative path containing directory names.("mydir/hello.png"
|
|
|
|
* etc.).
|
2022-10-18 19:13:40 +08:00
|
|
|
* @since v4.0 with axmol
|
2021-12-06 13:00:44 +08:00
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL void captureScreen(std::function<void(RefPtr<Image>)> imageCallback);
|
2021-12-06 13:00:44 +08:00
|
|
|
|
|
|
|
/** Capture a specific Node.
|
2022-08-08 18:02:17 +08:00
|
|
|
* @param startNode specify the snapshot Node. It should be ax::Scene
|
2021-12-06 13:00:44 +08:00
|
|
|
* @param scale
|
|
|
|
* @returns: return a Image, then can call saveToFile to save the image as "xxx.png or xxx.jpg".
|
2022-10-18 19:13:40 +08:00
|
|
|
* @since v4.0 with axmol
|
2021-12-06 13:00:44 +08:00
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL void captureNode(Node* startNode, std::function<void(RefPtr<Image>)> imageCallback, float scale = 1.0f);
|
2021-12-06 13:00:44 +08:00
|
|
|
|
|
|
|
/** Capture the entire screen. V4-copmatiable only, [DEPRECATED]
|
|
|
|
* To ensure the snapshot is applied after everything is updated and rendered in the current frame,
|
|
|
|
* we need to wrap the operation with a custom command which is then inserted into the tail of the render queue.
|
|
|
|
* @param afterCaptured specify the callback function which will be invoked after the snapshot is done.
|
|
|
|
* @param filename specify a filename where the snapshot is stored. This parameter can be either an absolute path or a
|
|
|
|
* simple base filename ("hello.png" etc.), don't use a relative path containing directory names.("mydir/hello.png"
|
|
|
|
* etc.).
|
|
|
|
* @since v4.0
|
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL void captureScreen(std::function<void(bool, std::string_view)> afterCap, std::string_view filename);
|
2021-12-06 13:00:44 +08:00
|
|
|
|
|
|
|
/** Find children by name, it will return all child that has the same name.
|
|
|
|
* It supports c++ 11 regular expression. It is a helper function of `Node::enumerateChildren()`.
|
|
|
|
* You can refer to `Node::enumerateChildren()` for detail information.
|
|
|
|
*
|
|
|
|
* @param node The node to find
|
|
|
|
* @param name The name to search for, it supports c++ 11 expression
|
|
|
|
* @return Array of Nodes that matches the name
|
|
|
|
* @since v3.2
|
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL std::vector<Node*> findChildren(const Node& node, std::string_view name);
|
2021-12-06 13:00:44 +08:00
|
|
|
|
|
|
|
/** Same to ::atof, but strip the string, remain 7 numbers after '.' before call atof.
|
|
|
|
* Why we need this? Because in android c++_static, atof ( and std::atof ) is unsupported for numbers have long decimal
|
|
|
|
* part and contain several numbers can approximate to 1 ( like 90.099998474121094 ), it will return inf. This function
|
|
|
|
* is used to fix this bug.
|
|
|
|
* @param str The string be to converted to double.
|
|
|
|
* @return Returns converted value of a string.
|
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL double atof(const char* str);
|
2021-12-06 13:00:44 +08:00
|
|
|
|
|
|
|
/** Get current exact time, accurate to nanoseconds.
|
|
|
|
* @return Returns the time in seconds since the Epoch.
|
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL double gettime();
|
2021-12-06 13:00:44 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get current time in milliseconds, accurate to nanoseconds
|
|
|
|
*
|
|
|
|
* @return Returns the time in milliseconds since the Epoch.
|
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL long long getTimeInMilliseconds();
|
2021-12-06 13:00:44 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate unionof bounding box of a node and its children.
|
|
|
|
* @return Returns unionof bounding box of a node and its children.
|
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL Rect getCascadeBoundingBox(Node* node);
|
2021-12-06 13:00:44 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a sprite instance from base64 encoded image and adds the texture to the Texture Cache.
|
|
|
|
|
|
|
|
* @return Returns an instance of sprite
|
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL Sprite* createSpriteFromBase64Cached(const char* base64String, const char* key);
|
2021-12-06 13:00:44 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a sprite instance from base64 encoded image.
|
|
|
|
|
|
|
|
* @return Returns an instance of sprite
|
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL Sprite* createSpriteFromBase64(const char* base64String);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
/**
|
|
|
|
* Find a child by name recursively
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
* @return Returns found node or nullptr
|
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL Node* findChild(Node* levelRoot, std::string_view name);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
/**
|
|
|
|
* Find a child by tag recursively
|
2020-09-04 11:28:16 +08:00
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
* @return Returns found node or nullptr
|
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL Node* findChild(Node* levelRoot, int tag);
|
2020-09-04 11:28:16 +08:00
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
/**
|
|
|
|
* Find a child by name recursively
|
2020-09-04 11:28:16 +08:00
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
* @return Returns found node or nullptr with specified type 'T'
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2021-12-31 12:12:40 +08:00
|
|
|
inline T findChild(Node* levelRoot, std::string_view name)
|
2021-12-06 13:00:44 +08:00
|
|
|
{
|
|
|
|
return dynamic_cast<T>(findChild(levelRoot, name));
|
|
|
|
}
|
2020-09-04 11:28:16 +08:00
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
/**
|
|
|
|
* Find a child by tag recursively
|
2020-09-04 11:28:16 +08:00
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
* @return Returns found node or nullptr with specified type 'T'
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
inline T findChild(Node* levelRoot, int tag)
|
|
|
|
{
|
|
|
|
return dynamic_cast<T>(findChild(levelRoot, tag));
|
|
|
|
}
|
2020-09-04 11:28:16 +08:00
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
/**
|
|
|
|
* Gets the md5 hash for the given file.
|
|
|
|
* @param filename The file to calculate md5 hash.
|
|
|
|
* @return The md5 hash for the file
|
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL std::string getFileMD5Hash(std::string_view filename);
|
2021-12-06 13:00:44 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the md5 hash for the given buffer.
|
|
|
|
* @param data The buffer to calculate md5 hash.
|
|
|
|
* @return The md5 hash for the data
|
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL std::string getDataMD5Hash(const Data& data);
|
2021-12-06 13:00:44 +08:00
|
|
|
|
2021-12-16 11:14:39 +08:00
|
|
|
/**
|
|
|
|
* Gets the hash for the given buffer with specific algorithm.
|
|
|
|
* @param data The buffer to calculate md5 hash.
|
|
|
|
* @param algorithm The hash algorithm, support "md5", "sha1", "sha256", "sha512" and more
|
|
|
|
* @return The hash for the data
|
|
|
|
*/
|
2023-01-01 18:07:18 +08:00
|
|
|
AX_DLL std::string computeDigest(std::string_view data, std::string_view algorithm, bool toHex = true);
|
2021-12-16 11:14:39 +08:00
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
/**
|
|
|
|
@brief Converts language iso 639-1 code to LanguageType enum.
|
|
|
|
@return LanguageType enum.
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL LanguageType getLanguageTypeByISO2(const char* code);
|
2020-09-04 11:28:16 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL backend::BlendFactor toBackendBlendFactor(int factor);
|
2020-09-04 11:28:16 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL int toGLBlendFactor(backend::BlendFactor blendFactor);
|
2020-09-04 11:28:16 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL backend::SamplerFilter toBackendSamplerFilter(int mode);
|
2020-10-04 23:43:57 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL backend::SamplerAddressMode toBackendAddressMode(int mode);
|
2020-10-04 23:43:57 +08:00
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
// Adjust matrix for metal.
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL const Mat4& getAdjustMatrix();
|
2021-12-06 13:00:44 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Get the Normal Matrix of matrixMV
|
|
|
|
*/
|
2023-09-02 19:56:50 +08:00
|
|
|
AX_DLL Mat3 getNormalMat3OfMat4(const Mat4& mat);
|
2021-12-06 13:00:44 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
@brief Parses a list of space-separated integers.
|
|
|
|
@return Vector of ints.
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL std::vector<int> parseIntegerList(std::string_view intsString);
|
2021-12-06 13:00:44 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
@brief translate charstring/binarystream to hexstring.
|
|
|
|
@return hexstring.
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL std::string bin2hex(std::string_view binary /*charstring also regard as binary in C/C++*/,
|
2021-12-06 13:00:44 +08:00
|
|
|
int delim = -1,
|
|
|
|
bool prefix = false);
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief killCurrentProcess immidiately, any object's destructor never call
|
|
|
|
@return NA.
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL void killCurrentProcess();
|
2021-12-06 13:00:44 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a Game Object, like CREATE_FUNC, but more powerful
|
|
|
|
|
|
|
|
* @return Returns a RefPtr<T> game object
|
2022-08-08 18:02:17 +08:00
|
|
|
* @remark Auto manage ax::Ref reference count, use std::unique_ptr
|
2021-12-06 13:00:44 +08:00
|
|
|
* @limition The init function finit must be public
|
|
|
|
*/
|
|
|
|
template <typename T, typename F, typename... Ts>
|
|
|
|
static RefPtr<T> makeInstance(F&& memf, Ts&&... args)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
RefPtr<T> pRet(ReferencedObject<T>{new T()});
|
2021-12-06 13:00:44 +08:00
|
|
|
if (pRet && std::mem_fn(memf)(pRet.get(), std::forward<Ts>(args)...))
|
|
|
|
return pRet;
|
2021-10-05 09:50:01 +08:00
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
pRet.reset();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a Game Object with 'bool T::init()', like CREATE_FUNC, but more powerful
|
|
|
|
|
|
|
|
* @return Returns a autorelease game object
|
2022-08-08 18:02:17 +08:00
|
|
|
* @remark Auto manage ax::Ref reference count, use std::unique_ptr
|
2021-12-06 13:00:44 +08:00
|
|
|
* @limition The init function finit must be public
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
inline static RefPtr<T> makeInstance()
|
|
|
|
{
|
|
|
|
return makeInstance<T>(&T::init);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a Game Object, like CREATE_FUNC, but more powerful
|
|
|
|
|
|
|
|
* @return Returns a autorelease game object
|
2022-08-08 18:02:17 +08:00
|
|
|
* @remark Auto manage ax::Ref reference count, use AutoReleasePool
|
2021-12-06 13:00:44 +08:00
|
|
|
* @limition The init function finit must be public
|
|
|
|
*/
|
|
|
|
template <typename T, typename F, typename... Ts>
|
|
|
|
inline T* createInstance(F&& finit, Ts&&... args)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
T* pRet = new T();
|
|
|
|
if (std::mem_fn(finit)(pRet, std::forward<Ts>(args)...))
|
2021-12-06 13:00:44 +08:00
|
|
|
pRet->autorelease();
|
|
|
|
else
|
2021-10-05 09:50:01 +08:00
|
|
|
{
|
2021-12-06 13:00:44 +08:00
|
|
|
delete pRet;
|
|
|
|
pRet = nullptr;
|
2021-10-05 09:50:01 +08:00
|
|
|
}
|
2021-12-08 00:11:53 +08:00
|
|
|
return pRet;
|
2021-12-06 13:00:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a Game Object with 'bool T::init()', like CREATE_FUNC, but more powerful
|
|
|
|
|
|
|
|
* @return Returns a autorelease game object
|
2022-08-08 18:02:17 +08:00
|
|
|
* @remark Auto manage ax::Ref reference count, use AutoReleasePool
|
2021-12-06 13:00:44 +08:00
|
|
|
* @limition The init function finit must be public
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
inline T* createInstance()
|
|
|
|
{
|
2022-08-08 18:02:17 +08:00
|
|
|
return ::ax::utils::createInstance<T>(&T::init);
|
2021-12-06 13:00:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a Game Object with 'bool T::init()', like CREATE_FUNC, but more powerful
|
2021-10-05 09:50:01 +08:00
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
* @return Returns a game object
|
|
|
|
* @remark You need call release after you don't want use it manually
|
|
|
|
* @limition The init function finit must be public
|
|
|
|
*/
|
|
|
|
template <typename T, typename F, typename... Ts>
|
|
|
|
static T* newInstance(F&& memf, Ts&&... args)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
T* pRet = new T();
|
|
|
|
if (std::mem_fn(memf)(pRet, std::forward<Ts>(args)...))
|
2021-12-06 13:00:44 +08:00
|
|
|
return pRet;
|
|
|
|
else
|
2021-10-05 09:50:01 +08:00
|
|
|
{
|
2021-12-06 13:00:44 +08:00
|
|
|
delete pRet;
|
|
|
|
pRet = nullptr;
|
|
|
|
return nullptr;
|
2021-10-05 09:50:01 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
/**
|
|
|
|
* Create a Game Object with 'bool T::init()', like CREATE_FUNC, but more powerful
|
|
|
|
|
|
|
|
* @return Returns a game object
|
|
|
|
* @remark You need call release after you don't want use it manually
|
|
|
|
* @limition The init function finit must be public
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
inline static T* newInstance()
|
|
|
|
{
|
|
|
|
return newInstance<T>(&T::init);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check a number is power of two.
|
|
|
|
inline bool isPOT(int number)
|
|
|
|
{
|
|
|
|
return ((number > 0) && (number & (number - 1)) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert ASCII hex digit to a nibble (four bits, 0 - 15).
|
|
|
|
//
|
|
|
|
// Use unsigned to avoid signed overflow UB.
|
2021-12-06 15:02:53 +08:00
|
|
|
inline unsigned char hex2nibble(unsigned char c)
|
|
|
|
{
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
{
|
|
|
|
return c - '0';
|
|
|
|
}
|
|
|
|
else if (c >= 'a' && c <= 'f')
|
|
|
|
{
|
|
|
|
return 10 + (c - 'a');
|
|
|
|
}
|
|
|
|
else if (c >= 'A' && c <= 'F')
|
|
|
|
{
|
|
|
|
return 10 + (c - 'A');
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert a nibble ASCII hex digit
|
|
|
|
inline unsigned char nibble2hex(unsigned char c, unsigned char a = 'a')
|
|
|
|
{
|
|
|
|
return ((c) < 0xa ? ((c) + '0') : ((c) + a - 10));
|
|
|
|
}
|
2021-12-06 13:00:44 +08:00
|
|
|
|
|
|
|
// Convert ASCII hex string (two characters) to byte.
|
|
|
|
//
|
|
|
|
// E.g., "0B" => 0x0B, "af" => 0xAF.
|
|
|
|
inline char hex2char(const char* p)
|
|
|
|
{
|
2021-12-06 15:02:53 +08:00
|
|
|
return hex2nibble((uint8_t)p[0]) << 4 | hex2nibble(p[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert byte to ASCII hex string (two characters).
|
|
|
|
inline char* char2hex(char* p, unsigned char c, unsigned char a = 'a')
|
|
|
|
{
|
|
|
|
p[0] = nibble2hex(c >> 4, a);
|
|
|
|
p[1] = nibble2hex(c & (uint8_t)0xf, a);
|
|
|
|
return p;
|
2021-12-06 13:00:44 +08:00
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL std::string urlEncode(std::string_view s);
|
2021-12-06 13:00:44 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL std::string urlDecode(std::string_view st);
|
2022-06-17 22:16:55 +08:00
|
|
|
|
2023-01-01 18:07:18 +08:00
|
|
|
/**
|
|
|
|
* Encodes bytes into a 64base buffer
|
|
|
|
* @returns base64 encoded string
|
|
|
|
*
|
|
|
|
@since axmol-1.0.0
|
|
|
|
*/
|
|
|
|
AX_DLL std::string base64Encode(std::string_view s);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decodes a 64base encoded buffer
|
|
|
|
* @returns palintext
|
|
|
|
*
|
|
|
|
@since axmol-1.0.0
|
|
|
|
*/
|
|
|
|
AX_DLL std::string base64Decode(std::string_view s);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encodes bytes into a 64base encoded memory with terminating '\0' character.
|
|
|
|
* The encoded memory is expected to be freed by the caller by calling `free()`
|
|
|
|
*
|
|
|
|
* @returns the length of the out buffer
|
|
|
|
*
|
|
|
|
@since v2.1.4, axmol-1.0.0 move from namespace ax to ax::utils
|
|
|
|
*/
|
|
|
|
AX_DLL int base64Encode(const unsigned char* in, unsigned int inLength, char** out);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decodes a 64base encoded memory. The decoded memory is
|
|
|
|
* expected to be freed by the caller by calling `free()`
|
|
|
|
*
|
|
|
|
* @returns the length of the out buffer
|
|
|
|
*
|
|
|
|
@since v0.8.1, axmol-1.0.0 move from namespace ax to ax::utils
|
|
|
|
*/
|
|
|
|
AX_DLL int base64Decode(const unsigned char* in, unsigned int inLength, unsigned char** out);
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DLL uint32_t fourccValue(std::string_view str);
|
2023-01-01 18:07:18 +08:00
|
|
|
|
2021-12-06 13:00:44 +08:00
|
|
|
} // namespace utils
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#endif // __SUPPORT_AX_UTILS_H__
|