Compare commits

..

13 Commits

Author SHA1 Message Date
65d87a1759 Fix bitconverter code 2026-02-16 17:41:16 -06:00
3889c746fe Add signed to bitconverter and float 2026-02-15 15:29:28 -06:00
9dae77a0b9 Revamp uuid code 2026-02-15 14:55:01 -06:00
61275c0f5f Add Uuids 2026-02-15 13:28:06 -06:00
adf11bd144 Fix plucky 2026-02-04 22:13:52 -06:00
f53bacb18b Add node 2026-02-04 21:28:59 -06:00
94eb56aad6 Fix gitea 2026-02-04 21:00:34 -06:00
4efd654941 Add gitea workflows 2026-02-04 20:39:57 -06:00
34b484f633 Add gitea container 2026-02-04 18:38:55 -06:00
96ba20d65c Remove expensive builds on commit 2026-02-04 18:29:57 -06:00
f825c2616a Change version 0.0.1 2026-01-30 19:21:16 -06:00
67874eba30 Make portable json support absolute paths as an option, for android crosslang 2026-01-15 13:02:49 -06:00
8eb7e050c0 Fix array 2026-01-14 15:24:55 -06:00
27 changed files with 1389 additions and 136 deletions

44
.gitea/workflows/tag.yaml Normal file
View File

@@ -0,0 +1,44 @@
name: Build and Deploy on Tag
on:
push:
tags:
- 'v*'
env:
GITEA_AUTH: ${{ secrets.MY_GITEA_AUTH }}
jobs:
build-arch:
runs-on: arch-builder
steps:
- run: pacman --noconfirm -Sy nodejs npm
- uses: actions/checkout@v4
- run: pacman --noconfirm -Sy mbedtls curl
- run: pacman --config /opt/cross/ppc/pacman.conf --noconfirm -Sy mbedtls
- run: cp Packaging/Linux/PKGBUILD /home/build/PKGBUILD
- run: cp Packaging/Linux/build-arch.sh /home/build/build-arch.sh
- run: chmod 755 /home/build/build-arch.sh
- run: chown build:build /home/build/PKGBUILD
- run: chown build:build /home/build/build-arch.sh
- run: su build -c /home/build/build-arch.sh
build-jammy:
runs-on: deb-builder-jammy
steps:
- uses: actions/checkout@v4
- name: Build for jammy, noble
run: |
bash build-ubuntu-jammy.sh
bash push-ubuntu-jammy.sh
working-directory: ./Packaging/Linux
build-plucky:
runs-on: deb-builder-plucky
steps:
- uses: actions/checkout@v4
- name: Build for plucky, resolute
run: |
bash build-ubuntu-plucky.sh
bash push-ubuntu-plucky.sh
working-directory: ./Packaging/Linux
- uses: akkuman/gitea-release-action@v1
env:
NODE_OPTIONS: '--experimental-fetch' # if nodejs < 18

View File

@@ -1,18 +1,21 @@
version: 39 version: 43
jobs: jobs:
- name: Build for x86_64 - name: Build for x86_64
steps: steps:
- !CheckoutStep - type: CheckoutStep
name: Checkout name: Checkout
cloneCredential: !DefaultCredential {} cloneCredential:
type: DefaultCredential
withLfs: true withLfs: true
withSubmodules: false withSubmodules: false
condition: ALL_PREVIOUS_STEPS_WERE_SUCCESSFUL condition: SUCCESSFUL
- !CommandStep optional: false
- type: CommandStep
name: Execute build name: Execute build
runInContainer: true runInContainer: true
image: onedev.site.tesses.net/dependencies/dependencies:latest image: onedev.site.tesses.net/dependencies/dependencies:latest
interpreter: !DefaultInterpreter interpreter:
type: DefaultInterpreter
commands: | commands: |
mkdir build mkdir build
cd build cd build
@@ -20,72 +23,28 @@ jobs:
make -j12 make -j12
make install DESTDIR=out make install DESTDIR=out
useTTY: true useTTY: true
condition: ALL_PREVIOUS_STEPS_WERE_SUCCESSFUL condition: SUCCESSFUL
- !BuildImageStep optional: false
- type: BuildImageStep
name: Build Docker Image name: Build Docker Image
dockerfile: Dockerfile.run dockerfile: Dockerfile.run
output: !RegistryOutput output:
tags: onedev.site.tesses.net/tesses-framework/tesses-framework:latest onedev.site.tesses.net/tesses-framework/tesses-framework:@commit_hash@ type: RegistryOutput
tags: onedev.site.tesses.net/tesses-framework/tesses-framework:latest onedev.site.tesses.net/tesses-framework/tesses-framework:@commit_hash@ git.tesseslanguage.com/tesses50/tesses-framework:latest git.tesseslanguage.com/tesses50/tesses-framework:@commit_hash@
registryLogins: registryLogins:
- registryUrl: '@server_url@' - registryUrl: '@server_url@'
userName: '@job_token@' userName: '@job_token@'
passwordSecret: dockersecret passwordSecret: dockersecret
- registryUrl: git.tesseslanguage.com
userName: tesses50
passwordSecret: GITEA_AUTH
platforms: linux/amd64 platforms: linux/amd64
condition: ALL_PREVIOUS_STEPS_WERE_SUCCESSFUL condition: SUCCESSFUL
- !CommandStep optional: false
name: Build archlinux
runInContainer: true
image: git.tesseslanguage.com/tesses50/arch-builds:2025-11-11
interpreter: !DefaultInterpreter
commands: |
pacman --noconfirm -Sy mbedtls curl
pacman --config /opt/cross/ppc/pacman.conf --noconfirm -Sy mbedtls
cp Packaging/Linux/PKGBUILD /home/build/PKGBUILD
cp Packaging/Linux/build-arch.sh /home/build/build-arch.sh
chmod 755 /home/build/build-arch.sh
chown build:build /home/build/PKGBUILD
chown build:build /home/build/build-arch.sh
su build -c /home/build/build-arch.sh
envVars:
- name: GITEA_AUTH
value: '@secret:GITEA_AUTH@'
useTTY: true
condition: ALL_PREVIOUS_STEPS_WERE_SUCCESSFUL
- !CommandStep
name: Build and Publish Deb Package
runInContainer: true
image: onedev.site.tesses.net/dependencies/debbuilder/jammy:latest
interpreter: !DefaultInterpreter
commands: |
cd Packaging/Linux
bash build-ubuntu-jammy.sh
bash push-ubuntu-jammy.sh
envVars:
- name: GITEA_AUTH
value: '@secret:GITEA_AUTH@'
- name: BUILD_NO
value: '@build_number@'
useTTY: true
condition: ALL_PREVIOUS_STEPS_WERE_SUCCESSFUL
- !CommandStep
name: Build and Publish Deb Package (Plucky)
runInContainer: true
image: onedev.site.tesses.net/dependencies/debbuilder/plucky:latest
interpreter: !DefaultInterpreter
commands: |
cd Packaging/Linux
bash build-ubuntu-plucky.sh
bash push-ubuntu-plucky.sh
envVars:
- name: GITEA_AUTH
value: '@secret:GITEA_AUTH@'
- name: BUILD_NO
value: '@build_number@'
useTTY: true
condition: ALL_PREVIOUS_STEPS_WERE_SUCCESSFUL
triggers: triggers:
- !BranchUpdateTrigger - type: BranchUpdateTrigger
branches: master branches: master
userMatch: anyone
projects: tesses-framework projects: tesses-framework
retryCondition: never retryCondition: never
maxRetries: 3 maxRetries: 3

View File

@@ -1,6 +1,10 @@
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
project(TessesFramework VERSION 1.0.0) set(TESSESFRAMEWORK_MAJOR_VERSION 0)
set(TESSESFRAMEWORK_MINOR_VERSION 0)
set(TESSESFRAMEWORK_PATCH_VERSION 2)
project(TessesFramework VERSION ${TESSESFRAMEWORK_MAJOR_VERSION}.${TESSESFRAMEWORK_MINOR_VERSION}.${TESSESFRAMEWORK_PATCH_VERSION})
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
@@ -61,6 +65,7 @@ src/Crypto/MbedTLS/Crypto.cpp
src/Args.cpp src/Args.cpp
src/TF_Init.cpp src/TF_Init.cpp
src/HiddenField.cpp src/HiddenField.cpp
src/Uuid.cpp
src/BitTorrent/TorrentFile.cpp src/BitTorrent/TorrentFile.cpp
src/BitTorrent/TorrentStream.cpp src/BitTorrent/TorrentStream.cpp
) )
@@ -409,6 +414,16 @@ add_executable(trng apps/trng.cpp)
target_link_libraries(trng PUBLIC tessesframework) target_link_libraries(trng PUBLIC tessesframework)
install(TARGETS trng DESTINATION "${CMAKE_INSTALL_BINDIR}") install(TARGETS trng DESTINATION "${CMAKE_INSTALL_BINDIR}")
add_executable(twatch apps/twatch.cpp)
target_link_libraries(twatch PUBLIC tessesframework)
install(TARGETS twatch DESTINATION "${CMAKE_INSTALL_BINDIR}")
add_executable(tuuid apps/tuuid.cpp)
target_link_libraries(tuuid PUBLIC tessesframework)
install(TARGETS tuuid DESTINATION "${CMAKE_INSTALL_BINDIR}")
endif() endif()
include(InstallRequiredSystemLibraries) include(InstallRequiredSystemLibraries)

View File

@@ -11,22 +11,11 @@ find_package(Threads REQUIRED)
endif() endif()
endif() endif()
set(TESSESFRAMEWORK_ENABLE_LIBWEBCAM @TESSESFRAMEWORK_ENABLE_LIBWEBCAM@)
if(${TESSESFRAMEWORK_ENABLE_LIBWEBCAM})
find_package(libwebcam)
endif()
set(TESSESFRAMEWORK_ENABLE_SQLITE @TESSESFRAMEWORK_ENABLE_SQLITE@) set(TESSESFRAMEWORK_ENABLE_SQLITE @TESSESFRAMEWORK_ENABLE_SQLITE@)
set(TESSESFRAMEWORK_ENABLE_PLATFORMFOLDERS @TESSESFRAMEWORK_ENABLE_PLATFORMFOLDERS@) set(TESSESFRAMEWORK_ENABLE_PLATFORMFOLDERS @TESSESFRAMEWORK_ENABLE_PLATFORMFOLDERS@)
set(TESSESFRAMEWORK_ENABLE_SDL2 @TESSESFRAMEWORK_ENABLE_SDL2@)
if(${TESSESFRAMEWORK_ENABLE_SDL2})
find_package(SDL2 REQUIRED)
find_package(SDL2_ttf REQUIRED)
find_package(SDL2_image REQUIRED)
endif()
set(TESSESFRAMEWORK_ENABLE_NETWORKING @TESSESFRAMEWORK_ENABLE_NETWORKING@) set(TESSESFRAMEWORK_ENABLE_NETWORKING @TESSESFRAMEWORK_ENABLE_NETWORKING@)
set(TESSESFRAMEWORK_ENABLE_THREADING @TESSESFRAMEWORK_ENABLE_THREADING@) set(TESSESFRAMEWORK_ENABLE_THREADING @TESSESFRAMEWORK_ENABLE_THREADING@)
set(TESSESFRAMEWORK_ENABLE_PROCESS @TESSESFRAMEWORK_ENABLE_PROCESS@)
set(TESSESFRAMEWORK_ENABLE_RPATH @TESSESFRAMEWORK_ENABLE_RPATH@)

View File

@@ -1,6 +1,6 @@
# Maintainer: Mike Nolan <tesses@tesses.net> # Maintainer: Mike Nolan <tesses@tesses.net>
pkgname=tesses-framework # '-bzr', '-git', '-hg' or '-svn' pkgname=tesses-framework # '-bzr', '-git', '-hg' or '-svn'
pkgver=1.0.0 pkgver=0.0.2
pkgrel=1 pkgrel=1
pkgdesc="" pkgdesc=""
arch=('x86_64' 'powerpc') arch=('x86_64' 'powerpc')
@@ -18,21 +18,7 @@ options=(!strip)
else else
options=(!buildflags !strip) options=(!buildflags !strip)
fi fi
# Please refer to the 'USING VCS SOURCES' section of the PKGBUILD man page for
# a description of each element in the source array.
pkgver() {
cd "$srcdir/${pkgname}"
# The examples below are not absolute and need to be adapted to each repo. The
# primary goal is to generate version numbers that will increase according to
# pacman's version comparisons with later commits to the repo. The format
# VERSION='VER_NUM.rREV_NUM.HASH', or a relevant subset in case VER_NUM or HASH
# are not available, is recommended.
# Git, no tags available
printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}
prepare() { prepare() {
cd "$srcdir/${pkgname}" cd "$srcdir/${pkgname}"

View File

@@ -1,2 +1 @@
export BUILD=$(($BUILD_NO-163)) export DEB_VERSION=0.0.2
export DEB_VERSION=1.0.0-$BUILD

View File

@@ -1,6 +1,11 @@
#pragma once #pragma once
#define TESSES_FRAMEWORK_FLAG_OFF 0 #define TESSES_FRAMEWORK_FLAG_OFF 0
#define TESSES_FRAMEWORK_FLAG_ON 1 #define TESSES_FRAMEWORK_FLAG_ON 1
#define TESSES_FRAMEWORK_MAJOR @TESSESFRAMEWORK_MAJOR_VERSION@
#define TESSES_FRAMEWORK_MINOR @TESSESFRAMEWORK_MINOR_VERSION@
#define TESSES_FRAMEWORK_PATCH @TESSESFRAMEWORK_PATCH_VERSION@
#if defined(TESSES_FRAMEWORK_FLAG_@TESSESFRAMEWORK_ENABLE_SQLITE@) && !defined(TESSESFRAMEWORK_ENABLE_SQLITE) #if defined(TESSES_FRAMEWORK_FLAG_@TESSESFRAMEWORK_ENABLE_SQLITE@) && !defined(TESSESFRAMEWORK_ENABLE_SQLITE)
#define TESSESFRAMEWORK_ENABLE_SQLITE #define TESSESFRAMEWORK_ENABLE_SQLITE
#endif #endif

24
apps/tuuid.cpp Normal file
View File

@@ -0,0 +1,24 @@
#include "TessesFramework/Uuid.hpp"
#include <iostream>
int main(int argc, char** argv)
{
//e794499c
using namespace Tesses::Framework;
Uuid uuid = Uuid::Generate();
/*uuid.time_low = 0xe794499c;
uuid.time_mid = 0x823c;
uuid.time_hi_and_version = 0x304c;
uuid.clock_seq_hi_and_reserved = 0xa4;
uuid.clock_seq_low = 0x59;
uuid.node[0] = 0x8f;
uuid.node[1] = 0xdc;
uuid.node[2] = 0xd9;
uuid.node[3] = 0x60;
uuid.node[4] = 0xb4;
uuid.node[5] = 0xac;*/
std::cout << uuid.ToString(Tesses::Framework::UuidStringifyConfig::LowercaseNoCurly) << std::endl;
}

24
apps/twatch.cpp Normal file
View File

@@ -0,0 +1,24 @@
#include <TessesFramework/TessesFramework.hpp>
using namespace Tesses::Framework;
using namespace Tesses::Framework::Filesystem;
using namespace Tesses::Framework::Filesystem::Literals;
int main(int argc, char** argv)
{
TF_Init();
if(argc<2)
{
std::cout << "USAGE " << argv[0] << " <file|dir>" << std::endl;
return 1;
}
auto watcher=FSWatcher::Create(LocalFS,VFSPath{argv[1]});
watcher->events = FSWatcherEventType::All;
watcher->event = [](FSWatcherEvent& evt)->void{
std::cout << evt.ToString() << std::endl;
};
watcher->SetEnabled(true);
TF_RunEventLoop();
return 0;
}

8
changelog.md Normal file
View File

@@ -0,0 +1,8 @@
# Changelog
## 0.0.2
Add UUIDs
## 0.0.1
Start versioning

View File

@@ -10,6 +10,7 @@
#include <functional> #include <functional>
#include "Threading/Mutex.hpp" #include "Threading/Mutex.hpp"
#include <optional> #include <optional>
#include <atomic>
namespace Tesses::Framework namespace Tesses::Framework
{ {

View File

@@ -42,6 +42,9 @@ namespace Tesses::Framework::Filesystem
void Lock(VFSPath path); void Lock(VFSPath path);
void Unlock(VFSPath path); void Unlock(VFSPath path);
protected:
std::shared_ptr<FSWatcher> CreateWatcher(std::shared_ptr<VFS> vfs, VFSPath path);
}; };
extern std::shared_ptr<LocalFilesystem> LocalFS; extern std::shared_ptr<LocalFilesystem> LocalFS;
} }

View File

@@ -8,6 +8,7 @@
namespace Tesses::Framework::Filesystem namespace Tesses::Framework::Filesystem
{ {
class StatVFSData { class StatVFSData {
public: public:
uint64_t BlockSize; uint64_t BlockSize;
@@ -29,7 +30,8 @@ namespace Tesses::Framework::Filesystem
static std::vector<std::string> SplitPath(std::string path); static std::vector<std::string> SplitPath(std::string path);
std::vector<std::string> path; std::vector<std::string> path;
VFSPath(); VFSPath();
explicit VFSPath(const char* path) : VFSPath(std::string(path))
{}
VFSPath(std::vector<std::string> path); VFSPath(std::vector<std::string> path);
VFSPath(std::string path); VFSPath(std::string path);
VFSPath(VFSPath p, std::string subent); VFSPath(VFSPath p, std::string subent);
@@ -118,7 +120,74 @@ namespace Tesses::Framework::Filesystem
VFSPathEnumeratorItterator end(); VFSPathEnumeratorItterator end();
}; };
enum class FSWatcherEventType {
None = 0,
//IN_ACCESS
Accessed=1,
//IN_ATTRIB
AttributeChanged =2,
//IN_CLOSE_WRITE
Writen = 4,
//IN_CLOSE_NOWRITE
Read = 8,
//IN_CREATE
Created = 16,
//IN_DELETE
Deleted = 32,
//IN_DELETE_SELF
WatchEntryDeleted = 64,
//IN_MODIFY
Modified = 128,
//IN_MOVE_SELF
WatchEntryMoved = 256,
//IN_MOVED_FROM
MoveOld = 512,
//IN_MOVED_TO
MoveNew = 1024,
//IN_OPEN
Opened = 2048,
//IN_CLOSE
Closed = Writen | Read,
//IN_MOVE
Moved = MoveOld | MoveNew,
//IN_ALL_EVENTS
All = Accessed | AttributeChanged | Created | Deleted | WatchEntryDeleted | Modified | WatchEntryMoved | Opened | Closed | Moved
};
struct FSWatcherEvent {
//the file or source on move
VFSPath src;
//the dest when moving
VFSPath dest;
FSWatcherEventType type;
bool isDir;
bool IsEvent(FSWatcherEventType e);
std::string ToString();
};
class VFS;
class FSWatcher {
private:
std::shared_ptr<VFS> vfs;
VFSPath path;
protected:
std::atomic<bool> enabled=false;
virtual void SetEnabledImpl(bool enabled);
public:
FSWatcher(std::shared_ptr<VFS> vfs, VFSPath path);
std::function<void(FSWatcherEvent&)> event;
FSWatcherEventType events = FSWatcherEventType::All;
bool GetEnabled();
void SetEnabled(bool val);
std::shared_ptr<VFS> GetFilesystem();
const VFSPath& GetPath();
virtual ~FSWatcher() = default;
static std::shared_ptr<FSWatcher> Create(std::shared_ptr<VFS> vfs, VFSPath path);
};
class VFS { class VFS {
public: public:
@@ -157,8 +226,24 @@ namespace Tesses::Framework::Filesystem
virtual void Lock(VFSPath path); virtual void Lock(VFSPath path);
virtual void Unlock(VFSPath path); virtual void Unlock(VFSPath path);
virtual ~VFS(); virtual ~VFS();
virtual void Close(); virtual void Close();
protected:
virtual std::shared_ptr<FSWatcher> CreateWatcher(std::shared_ptr<VFS> vfs, VFSPath path);
friend class FSWatcher;
}; };
namespace Literals
{
inline VFSPath operator""_tpath(const char* path)
{
return VFSPath(path);
}
}
} }

View File

@@ -143,10 +143,14 @@ struct CaseInsensitiveLess {
class HttpUtils class HttpUtils
{ {
public: public:
static char NibbleToHex(uint8_t b, bool isUppercase);
static char NibbleToHex(uint8_t nibble); static char NibbleToHex(uint8_t nibble);
static uint8_t HexToNibble(char c); static uint8_t HexToNibble(char c);
static std::string BytesToHex(const std::vector<uint8_t>& data); static std::string BytesToHex(const std::vector<uint8_t>& data);
static void BytesToHex(std::string& text,const std::vector<uint8_t>& data); static void BytesToHex(std::string& text,const std::vector<uint8_t>& data);
static std::string BytesToHex(const std::vector<uint8_t>& data,bool isUppercase);
static void BytesToHex(std::string& text,const std::vector<uint8_t>& data, bool isUppercase);
static std::vector<uint8_t> HexToBytes(const std::string& text); static std::vector<uint8_t> HexToBytes(const std::string& text);
static void HexToBytes(std::vector<uint8_t>& data,const std::string& text); static void HexToBytes(std::vector<uint8_t>& data,const std::string& text);
static std::string MimeType(std::filesystem::path p); static std::string MimeType(std::filesystem::path p);

View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include "../Common.hpp" #include "../Common.hpp"
#include "../Uuid.hpp"
namespace Tesses::Framework::Serialization namespace Tesses::Framework::Serialization
{ {
@@ -9,40 +10,73 @@ namespace Tesses::Framework::Serialization
*/ */
class BitConverter { class BitConverter {
public: public:
/**
* @brief Get the bits of a double from a int64_t
*
* @param v a int64_t with double's bits
* @return double the double
*/
static double ToDoubleBits(uint64_t v); static double ToDoubleBits(uint64_t v);
/**
* @brief Get the bits of a int64_t from a double
*
* @param v a double with int64_t's bits
* @return uint64_t the int64_t
*/
static uint64_t ToUintBits(double v); static uint64_t ToUintBits(double v);
/** static float ToFloatBits(uint32_t v);
* @brief Get big endian double from uint8_t reference of first element of 8 byte array
* static uint32_t ToUint32Bits(float v);
* @param b a reference to the first byte of an array
* @return double the double
*/
static double ToDoubleBE(uint8_t& b); static double ToDoubleBE(uint8_t& b);
/** static float ToFloatBE(uint8_t& b);
* @brief Get big endian uint64_t from uint8_t reference of first element of 8 byte array
*
* @param b a reference to the first byte of an array
* @return uint64_t the uint64_t
*/
static uint64_t ToUint64BE(uint8_t& b); static uint64_t ToUint64BE(uint8_t& b);
static uint32_t ToUint32BE(uint8_t& b); static uint32_t ToUint32BE(uint8_t& b);
static uint16_t ToUint16BE(uint8_t& b); static uint16_t ToUint16BE(uint8_t& b);
static double ToDoubleLE(uint8_t& b);
static float ToFloatLE(uint8_t& b);
static uint64_t ToUint64LE(uint8_t& b);
static uint32_t ToUint32LE(uint8_t& b);
static uint16_t ToUint16LE(uint8_t& b);
static int64_t ToSint64BE(uint8_t& b);
static int32_t ToSint32BE(uint8_t& b);
static int16_t ToSint16BE(uint8_t& b);
static int64_t ToSint64LE(uint8_t& b);
static int32_t ToSint32LE(uint8_t& b);
static int16_t ToSint16LE(uint8_t& b);
static Uuid ToUuid(uint8_t& b);
static void ToUuid(uint8_t& b, Uuid& uuid);
static void FromDoubleBE(uint8_t& b, double v); static void FromDoubleBE(uint8_t& b, double v);
static void FromFloatBE(uint8_t& b, float v);
static void FromUint64BE(uint8_t& b, uint64_t v); static void FromUint64BE(uint8_t& b, uint64_t v);
static void FromUint32BE(uint8_t& b, uint32_t v); static void FromUint32BE(uint8_t& b, uint32_t v);
static void FromUint16BE(uint8_t& b, uint16_t v); static void FromUint16BE(uint8_t& b, uint16_t v);
static void FromDoubleLE(uint8_t& b, double v);
static void FromFloatLE(uint8_t& b, float v);
static void FromUint64LE(uint8_t& b, uint64_t v);
static void FromUint32LE(uint8_t& b, uint32_t v);
static void FromUint16LE(uint8_t& b, uint16_t v);
static void FromSint64BE(uint8_t& b, int64_t v);
static void FromSint32BE(uint8_t& b, int32_t v);
static void FromSint16BE(uint8_t& b, int16_t v);
static void FromSint64LE(uint8_t& b, int64_t v);
static void FromSint32LE(uint8_t& b, int32_t v);
static void FromSint16LE(uint8_t& b, int16_t v);
static void FromUuid(uint8_t& b, const Uuid& uuid);
static inline bool IsLittleEndian()
{
uint8_t a[2];
a[0] = 0x01;
a[1] = 0xA4;
uint16_t num=0;
memcpy(&num,&a, 2);
return num != 420;
}
}; };
} }

View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include "Stream.hpp" #include "Stream.hpp"
#include "../Uuid.hpp"
namespace Tesses::Framework::Streams namespace Tesses::Framework::Streams
{ {
@@ -26,5 +27,7 @@ namespace Tesses::Framework::Streams
float ReadF32LE(); float ReadF32LE();
double ReadF64BE(); double ReadF64BE();
double ReadF64LE(); double ReadF64LE();
Uuid ReadUuid();
void ReadUuid(Uuid& uuid);
}; };
} }

View File

@@ -1,6 +1,6 @@
#pragma once #pragma once
#include "Stream.hpp" #include "Stream.hpp"
#include "../Uuid.hpp"
namespace Tesses::Framework::Streams namespace Tesses::Framework::Streams
{ {
class ByteWriter { class ByteWriter {
@@ -27,5 +27,6 @@ namespace Tesses::Framework::Streams
void WriteF32LE(float v); void WriteF32LE(float v);
void WriteF64BE(double v); void WriteF64BE(double v);
void WriteF64LE(double v); void WriteF64LE(double v);
void WriteUuid(const Uuid& uuid);
}; };
} }

View File

@@ -0,0 +1,37 @@
#pragma once
#include "Common.hpp"
namespace Tesses::Framework {
enum class UuidStringifyConfig {
IsUppercase=0b001,
HasCurly=0b010,
HasDashes=0b100,
UppercaseCompact = IsUppercase,
LowercaseCompact = 0,
UppercaseNoCurly = IsUppercase | HasDashes,
LowercaseNoCurly = HasDashes,
UppercaseCurly = IsUppercase | HasDashes | HasCurly,
LowercaseCurly = HasDashes | HasCurly
};
struct Uuid {
Uuid() = default;
uint32_t time_low = 0;
uint16_t time_mid = 0;
uint16_t time_hi_and_version = 0;
uint8_t clock_seq_hi_and_reserved = 0;
uint8_t clock_seq_low = 0;
uint8_t node[6] = {0,0,0,0,0,0};
static Uuid Generate();
static void Generate(Uuid& uuid);
static bool TryParse(std::string text, Uuid& uuid);
std::string ToString(UuidStringifyConfig cfg = UuidStringifyConfig::UppercaseCurly) const;
bool IsEmpty() const;
};
bool operator==(const Uuid& left, const Uuid& right);
bool operator!=(const Uuid& left, const Uuid& right);
}

View File

@@ -10,6 +10,13 @@
#include <utime.h> #include <utime.h>
#include <sys/statvfs.h> #include <sys/statvfs.h>
#endif #endif
#include "TessesFramework/Threading/Thread.hpp"
#if defined(__linux__)
#include <poll.h>
#include <sys/inotify.h>
#include <unistd.h>
#endif
namespace Tesses::Framework::Filesystem namespace Tesses::Framework::Filesystem
{ {
#if defined(_WIN32) #if defined(_WIN32)
@@ -249,8 +256,184 @@ namespace Tesses::Framework::Filesystem
std::error_code error; std::error_code error;
std::filesystem::remove(VFSPathToSystem(path),error); std::filesystem::remove(VFSPathToSystem(path),error);
} }
#if defined(__linux__)
class INotifyWatcher : public FSWatcher {
std::shared_ptr<Threading::Thread> thrd;
static uint32_t to_linux_mask(FSWatcherEventType flags)
{
uint32_t lflags = 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::Accessed) != 0) ? IN_ACCESS : 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::AttributeChanged) != 0) ? IN_ATTRIB : 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::Writen) != 0) ? IN_CLOSE_WRITE : 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::Read) != 0) ? IN_CLOSE_NOWRITE : 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::Created) != 0) ? IN_CREATE : 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::Deleted) != 0) ? IN_DELETE : 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::WatchEntryDeleted) != 0) ? IN_DELETE_SELF : 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::Modified) != 0) ? IN_MODIFY : 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::WatchEntryMoved) != 0) ? IN_MOVE_SELF : 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::MoveOld) != 0) ? IN_MOVED_FROM : 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::MoveNew) != 0) ? IN_MOVED_TO : 0;
lflags |= (((uint32_t)flags & (uint32_t)FSWatcherEventType::Opened) != 0) ? IN_OPEN : 0;
return lflags;
}
static FSWatcherEventType from_linux_mask(uint32_t lflags)
{
uint32_t flags = 0;
flags |= ((lflags & IN_ACCESS) != 0) ? (uint32_t)FSWatcherEventType::Accessed : 0;
flags |= ((lflags & IN_ATTRIB) != 0) ? (uint32_t)FSWatcherEventType::AttributeChanged : 0;
flags |= ((lflags & IN_CLOSE_WRITE) != 0) ? (uint32_t)FSWatcherEventType::Writen : 0;
flags |= ((lflags & IN_CLOSE_NOWRITE) != 0) ? (uint32_t)FSWatcherEventType::Read : 0;
flags |= ((lflags & IN_CREATE) != 0) ? (uint32_t)FSWatcherEventType::Created : 0;
flags |= ((lflags & IN_DELETE) != 0) ? (uint32_t)FSWatcherEventType::Deleted : 0;
flags |= ((lflags & IN_DELETE_SELF) != 0) ? (uint32_t)FSWatcherEventType::WatchEntryDeleted : 0;
flags |= ((lflags & IN_MODIFY) != 0) ? (uint32_t)FSWatcherEventType::Modified : 0;
flags |= ((lflags & IN_MOVE_SELF) != 0) ? (uint32_t)FSWatcherEventType::WatchEntryMoved : 0;
flags |= ((lflags & IN_MOVED_FROM) != 0) ? (uint32_t)FSWatcherEventType::MoveOld : 0;
flags |= ((lflags & IN_MOVED_TO) != 0) ? (uint32_t)FSWatcherEventType::MoveNew : 0;
flags |= ((lflags & IN_OPEN) != 0) ? (uint32_t)FSWatcherEventType::Opened : 0;
return (FSWatcherEventType)flags;
}
public:
INotifyWatcher(std::shared_ptr<VFS> vfs, VFSPath path) : FSWatcher(vfs,path)
{
}
protected:
void SetEnabledImpl(bool enabled)
{
if(enabled)
{
int fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
if (fd == -1)
{
throw std::runtime_error("Cannot init inotify");
}
auto str = this->GetFilesystem()->VFSPathToSystem(this->GetPath());
int watch = inotify_add_watch(fd, str.c_str(),to_linux_mask(this->events));
thrd = std::make_shared<Threading::Thread>([this,watch,fd]()-> void {
int cnt = 0;
struct pollfd pfd = {.fd = fd, .events = POLLIN};
std::vector<std::pair<VFSPath,uint32_t>> mvFroms;
char buf[4096]
__attribute__ ((aligned(__alignof__(struct inotify_event))));
const struct inotify_event *event;
ssize_t size;
bool fail=false;
FSWatcherEvent evt;
evt.dest = this->GetPath();
while(!fail && this->enabled)
{
cnt = poll(&pfd,1,-1);
if(cnt == -1) break;
if(cnt > 0)
{
if(pfd.revents & POLLIN)
{
for (;;) {
size = read(fd, buf, sizeof(buf));
if (size == -1 && errno != EAGAIN) {
fail=true;
break;
}
if (size <= 0)
break;
for (char *ptr = buf; ptr < buf + size;
ptr += sizeof(struct inotify_event) + event->len) {
event = (const struct inotify_event *) ptr;
VFSPath path = this->GetPath();
if(event->len)
path = path / std::string(event->name, (size_t)event->len);
if(((uint32_t)this->events & (uint32_t)FSWatcherEventType::Moved) == (uint32_t)FSWatcherEventType::Moved && event->mask & IN_MOVED_FROM)
{
mvFroms.emplace_back(path,event->cookie);
}
else if(((uint32_t)this->events & (uint32_t)FSWatcherEventType::Moved) == (uint32_t)FSWatcherEventType::Moved && event->mask & IN_MOVED_TO)
{
for(auto ittr = mvFroms.begin(); ittr != mvFroms.end(); ittr++)
{
if(ittr->second == event->cookie)
{
evt.src = ittr->first;
mvFroms.erase(ittr);
break;
}
}
evt.isDir = (event->mask & IN_ISDIR);
evt.dest = path;
evt.type = FSWatcherEventType::Moved;
if(this->event)
this->event(evt);
}
else {
evt.isDir = (event->mask & IN_ISDIR);
evt.src = path;
evt.type = from_linux_mask(event->mask);;
if(this->event)
this->event(evt);
}
if(event->mask & IN_MOVE_SELF)
{
close(fd);
return;
}
if(event->mask & IN_DELETE_SELF)
{
close(fd);
return;
}
}
}
}
}
}
close(fd);
});
}
else
{
thrd = nullptr;
}
}
public:
~INotifyWatcher()
{
this->enabled = false;
}
};
#endif
std::shared_ptr<FSWatcher> LocalFilesystem::CreateWatcher(std::shared_ptr<VFS> vfs, VFSPath path)
{
#if defined(__linux__)
return std::make_shared<INotifyWatcher>(vfs, path);
#endif
return VFS::CreateWatcher(vfs,path);
}
std::shared_ptr<LocalFilesystem> LocalFS = std::make_shared<LocalFilesystem>(); std::shared_ptr<LocalFilesystem> LocalFS = std::make_shared<LocalFilesystem>();
} }
// C:/Users/Jim/Joel // C:/Users/Jim/Joel

View File

@@ -232,7 +232,8 @@ namespace Tesses::Framework::Filesystem
if(i == this->path.size()-1 && i == toMakeRelativeTo.path.size()-1) if(i == this->path.size()-1 && i == toMakeRelativeTo.path.size()-1)
{ {
VFSPath path({this->path[this->path.size()-1]}); std::vector<std::string> paths{this->path[this->path.size()-1]};
VFSPath path(paths);
path.relative = true; path.relative = true;
return path; return path;
} }
@@ -387,7 +388,8 @@ namespace Tesses::Framework::Filesystem
if(ext.empty()) return; if(ext.empty()) return;
if(ext[0] != '.') if(ext[0] != '.')
{ {
str += '.' + ext; str += '.';
str += ext;
} }
else else
{ {
@@ -560,4 +562,113 @@ namespace Tesses::Framework::Filesystem
{ {
} }
std::shared_ptr<FSWatcher> VFS::CreateWatcher(std::shared_ptr<VFS> vfs,VFSPath path)
{
return std::make_shared<FSWatcher>(vfs,path);
}
void FSWatcher::SetEnabled(bool enabled)
{
if(this->enabled == enabled) return;
this->enabled = enabled;
this->SetEnabledImpl(enabled);
}
bool FSWatcher::GetEnabled()
{
return this->enabled;
}
void FSWatcher::SetEnabledImpl(bool enabled)
{
}
std::shared_ptr<VFS> FSWatcher::GetFilesystem()
{
return this->vfs;
}
const VFSPath& FSWatcher::GetPath()
{
return this->path;
}
FSWatcher::FSWatcher(std::shared_ptr<VFS> vfs, VFSPath path): vfs(vfs), path(path)
{
}
std::shared_ptr<FSWatcher> FSWatcher::Create(std::shared_ptr<VFS> vfs, VFSPath path)
{
return vfs->CreateWatcher(vfs,path);
}
bool FSWatcherEvent::IsEvent(FSWatcherEventType e)
{
if(e == FSWatcherEventType::All) return this->type != FSWatcherEventType::None;
if(e == FSWatcherEventType::Moved) return ((uint32_t)this->type & (uint32_t)FSWatcherEventType::Moved) == (uint32_t)FSWatcherEventType::Moved;
if(e == FSWatcherEventType::Closed) return ((uint32_t)this->type & (uint32_t)FSWatcherEventType::Closed) != 0;
return (uint32_t)this->type & (uint32_t)e;
}
std::string FSWatcherEvent::ToString()
{
if(IsEvent(FSWatcherEventType::Moved))
{
return (this->isDir ? "Moved directory " : "Moved file ") + this->src.ToString() + " -> " + this->dest.ToString();
}
else if(IsEvent(FSWatcherEventType::MoveOld))
{
return (this->isDir ? "Move source directory " : "Move source file ") + this->src.ToString();
}
else if(IsEvent(FSWatcherEventType::MoveNew))
{
return (this->isDir ? "Move destination directory " : "Move destination file ") + this->src.ToString();
}
else if(IsEvent(FSWatcherEventType::Accessed))
{
return (this->isDir ? "Accessed directory " : "Accessed file ") + this->src.ToString();
}
else if(IsEvent(FSWatcherEventType::AttributeChanged))
{
return (this->isDir ? "Changed attr on directory " : "Changed attr on file ") + this->src.ToString();
}
else if(IsEvent(FSWatcherEventType::Writen))
{
return (this->isDir ? "Finished changing directory " : "Finished writing to file ") + this->src.ToString();
}
else if(IsEvent(FSWatcherEventType::Read))
{
return (this->isDir ? "Finished reading directory " : "Finished reading from file ") + this->src.ToString();
}
else if(IsEvent(FSWatcherEventType::Created))
{
return (this->isDir ? "Created directory " : "Created file ") + this->src.ToString();
}
else if(IsEvent(FSWatcherEventType::Deleted))
{
return (this->isDir ? "Deleted directory " : "Deleted file ") + this->src.ToString();
}
else if(IsEvent(FSWatcherEventType::WatchEntryDeleted))
{
return (this->isDir ? "Deleted watched directory " : "Deleted watched file ") + this->src.ToString();
}
else if(IsEvent(FSWatcherEventType::Modified))
{
return (this->isDir ? "Modified directory " : "Modified file ") + this->src.ToString();
}
else if(IsEvent(FSWatcherEventType::WatchEntryMoved))
{
return (this->isDir ? "Moved watched directory " : "Moved watched file ") + this->src.ToString();
}
else if(IsEvent(FSWatcherEventType::Opened))
{
return (this->isDir ? "Opened directory " : "Opened file ") + this->src.ToString();
}
return "";
}
} }

View File

@@ -389,11 +389,8 @@ namespace Tesses::Framework::Http
static bool parseUntillBoundaryEnd(std::shared_ptr<Tesses::Framework::Streams::Stream> src, std::shared_ptr<Tesses::Framework::Streams::Stream> dest, std::string boundary) static bool parseUntillBoundaryEnd(std::shared_ptr<Tesses::Framework::Streams::Stream> src, std::shared_ptr<Tesses::Framework::Streams::Stream> dest, std::string boundary)
{ {
bool hasMore=true; bool hasMore=true;
#if defined(_WIN32)
uint8_t* checkBuffer = new uint8_t[boundary.size()]; uint8_t* checkBuffer = new uint8_t[boundary.size()];
#else
uint8_t checkBuffer[boundary.size()];
#endif
int b; int b;
size_t i = 0; size_t i = 0;
size_t i2 = 0; size_t i2 = 0;
@@ -457,9 +454,8 @@ namespace Tesses::Framework::Http
{ {
dest->Write(buffer,offsetInMem); dest->Write(buffer,offsetInMem);
} }
#if defined(_WIN32) delete[] checkBuffer;
delete checkBuffer;
#endif
return hasMore; return hasMore;
} }

View File

@@ -198,6 +198,19 @@ namespace Tesses::Framework::Http {
strm << std::setfill(c) << std::setw(count) << text; strm << std::setfill(c) << std::setw(count) << text;
return strm.str(); return strm.str();
} }
char HttpUtils::NibbleToHex(uint8_t b, bool isUppercase)
{
if(isUppercase)
{
b %= 16;
if(b >= 0 && b <= 9)
return b + '0';
if(b >= 10 && b <= 15)
return b + ('A' - 10);
return 0;
}
return NibbleToHex(b);
}
char HttpUtils::NibbleToHex(uint8_t b) char HttpUtils::NibbleToHex(uint8_t b)
{ {
b %= 16; b %= 16;
@@ -971,6 +984,12 @@ namespace Tesses::Framework::Http {
BytesToHex(text,data); BytesToHex(text,data);
return text; return text;
} }
std::string HttpUtils::BytesToHex(const std::vector<uint8_t>& data, bool isUpper)
{
std::string text;
BytesToHex(text,data, isUpper);
return text;
}
void HttpUtils::BytesToHex(std::string& text,const std::vector<uint8_t>& data) void HttpUtils::BytesToHex(std::string& text,const std::vector<uint8_t>& data)
{ {
if(data.empty()) { if(data.empty()) {
@@ -984,6 +1003,20 @@ namespace Tesses::Framework::Http {
text[i*2+1] += NibbleToHex(data[i]); text[i*2+1] += NibbleToHex(data[i]);
} }
} }
void HttpUtils::BytesToHex(std::string& text,const std::vector<uint8_t>& data, bool isUpper)
{
if(data.empty()) {
text.clear();
return;
}
text.resize(data.size()*2);
for(size_t i = 0; i < data.size(); i++)
{
text[i*2] = NibbleToHex(data[i] >> 4, isUpper);
text[i*2+1] += NibbleToHex(data[i], isUpper);
}
}
std::vector<uint8_t> HttpUtils::HexToBytes(const std::string& text) std::vector<uint8_t> HttpUtils::HexToBytes(const std::string& text)
{ {
std::vector<uint8_t> data; std::vector<uint8_t> data;

View File

@@ -4,16 +4,40 @@ namespace Tesses::Framework::Serialization
{ {
double BitConverter::ToDoubleBits(uint64_t v) double BitConverter::ToDoubleBits(uint64_t v)
{ {
return *(double*)&v; static_assert(sizeof(double) == sizeof(uint64_t), "double is not the same size as uint64_t");
double dest=0;
memcpy(&dest,&v, sizeof(uint64_t));
return dest;
} }
uint64_t BitConverter::ToUintBits(double v) uint64_t BitConverter::ToUintBits(double v)
{ {
return *(uint64_t*)&v; //as static_assert is compile time we don't need it here
uint64_t dest = 0;
memcpy(&dest,&v, sizeof(uint64_t));
return dest;
}
float BitConverter::ToFloatBits(uint32_t v)
{
static_assert(sizeof(float) == sizeof(uint32_t), "float is not the same size as uint32_t");
float dest=0;
memcpy(&dest,&v, sizeof(uint32_t));
return dest;
}
uint32_t BitConverter::ToUint32Bits(float v)
{
//as static_assert is compile time we don't need it here
uint32_t dest = 0;
memcpy(&dest,&v, sizeof(uint32_t));
return dest;
} }
double BitConverter::ToDoubleBE(uint8_t& b) double BitConverter::ToDoubleBE(uint8_t& b)
{ {
return ToDoubleBits(ToUint64BE(b)); return ToDoubleBits(ToUint64BE(b));
} }
float BitConverter::ToFloatBE(uint8_t& b)
{
return ToFloatBits(ToUint32BE(b));
}
uint64_t BitConverter::ToUint64BE(uint8_t& b) uint64_t BitConverter::ToUint64BE(uint8_t& b)
{ {
uint8_t* b2 = &b; uint8_t* b2 = &b;
@@ -49,6 +73,50 @@ namespace Tesses::Framework::Serialization
v |= (uint16_t)b2[1]; v |= (uint16_t)b2[1];
return v; return v;
} }
double BitConverter::ToDoubleLE(uint8_t& b)
{
return ToDoubleBits(ToUint64LE(b));
}
float BitConverter::ToFloatLE(uint8_t& b)
{
return ToFloatBits(ToUint32LE(b));
}
uint64_t BitConverter::ToUint64LE(uint8_t& b)
{
uint8_t* b2 = &b;
uint64_t v = 0;
v |= (uint64_t)b2[0];
v |= ((uint64_t)b2[1] << 8);
v |= ((uint64_t)b2[2] << 16);
v |= ((uint64_t)b2[3] << 24);
v |= ((uint64_t)b2[4] << 32);
v |= ((uint64_t)b2[5] << 40);
v |= ((uint64_t)b2[6] << 48);
v |= ((uint64_t)b2[7] << 56);
return v;
}
uint32_t BitConverter::ToUint32LE(uint8_t& b)
{
uint8_t* b2 = &b;
uint32_t v = 0;
v |= (uint32_t)b2[0];
v |= ((uint32_t)b2[1] << 8);
v |= ((uint32_t)b2[2] << 16);
v |= ((uint32_t)b2[3] << 24);
return v;
}
uint16_t BitConverter::ToUint16LE(uint8_t& b)
{
uint8_t* b2 = &b;
uint16_t v = 0;
v |= (uint16_t)b2[0];
v |= ((uint16_t)b2[1] << 8);
return v;
}
void BitConverter::FromDoubleBE(uint8_t& b, double v) void BitConverter::FromDoubleBE(uint8_t& b, double v)
{ {
FromUint64BE(b,ToUintBits(v)); FromUint64BE(b,ToUintBits(v));
@@ -82,4 +150,170 @@ namespace Tesses::Framework::Serialization
b2[1] = (uint8_t)v; b2[1] = (uint8_t)v;
} }
}; void BitConverter::FromDoubleLE(uint8_t& b, double v)
{
FromUint64BE(b,ToUintBits(v));
}
void BitConverter::FromFloatLE(uint8_t& b, float v)
{
FromUint32LE(b,ToUint32Bits(v));
}
void BitConverter::FromFloatBE(uint8_t& b, float v)
{
FromUint32BE(b,ToUint32Bits(v));
}
void BitConverter::FromUint64LE(uint8_t& b, uint64_t v)
{
uint8_t* b2 = &b;
b2[0] = (uint8_t)v;
b2[1] = (uint8_t)(v >> 8);
b2[2] = (uint8_t)(v >> 16);
b2[3] = (uint8_t)(v >> 24);
b2[4] = (uint8_t)(v >> 32);
b2[5] = (uint8_t)(v >> 40);
b2[6] = (uint8_t)(v >> 48);
b2[7] = (uint8_t)(v >> 56);
}
void BitConverter::FromUint32LE(uint8_t& b, uint32_t v)
{
uint8_t* b2 = &b;
b2[0] = (uint8_t)v;
b2[1] = (uint8_t)(v >> 8);
b2[2] = (uint8_t)(v >> 16);
b2[3] = (uint8_t)(v >> 24);
}
void BitConverter::FromUint16LE(uint8_t& b, uint16_t v)
{
uint8_t* b2 = &b;
b2[0] = (uint8_t)v;
b2[1] = (uint8_t)(v >> 8);
}
void BitConverter::FromUuid(uint8_t& b, const Uuid& uuid)
{
uint8_t* b2 = &b;
FromUint32BE(b2[0], uuid.time_low);
FromUint16BE(b2[4], uuid.time_mid);
FromUint16BE(b2[6], uuid.time_hi_and_version);
b2[8] = uuid.clock_seq_hi_and_reserved;
b2[9] = uuid.clock_seq_low;
for(size_t i = 0; i < 6; i++)
b2[i+10] = uuid.node[i];
}
Uuid BitConverter::ToUuid(uint8_t& b)
{
Uuid uuid;
BitConverter::ToUuid(b,uuid);
return uuid;
}
void BitConverter::ToUuid(uint8_t& b, Uuid& uuid)
{
uint8_t* b2 = &b;
uuid.time_low = ToUint32BE(b2[0]);
uuid.time_mid = ToUint16BE(b2[4]);
uuid.time_hi_and_version = ToUint16BE(b2[6]);
uuid.clock_seq_hi_and_reserved = b2[8];
uuid.clock_seq_low = b2[9];
for(size_t i = 0; i < 6; i++)
uuid.node[i]= b2[i+10];
}
int64_t BitConverter::ToSint64BE(uint8_t& b)
{
uint64_t src = ToUint64BE(b);
int64_t dest = 0;
memcpy(&dest,&src,sizeof(uint64_t));
return dest;
}
int64_t BitConverter::ToSint64LE(uint8_t& b)
{
uint64_t src = ToUint64LE(b);
int64_t dest = 0;
memcpy(&dest,&src,sizeof(uint64_t));
return dest;
}
int32_t BitConverter::ToSint32BE(uint8_t& b)
{
uint32_t src = ToUint32BE(b);
int32_t dest = 0;
memcpy(&dest,&src,sizeof(uint32_t));
return dest;
}
int32_t BitConverter::ToSint32LE(uint8_t& b)
{
uint32_t src = ToUint32LE(b);
int32_t dest = 0;
memcpy(&dest,&src,sizeof(uint32_t));
return dest;
}
int16_t BitConverter::ToSint16BE(uint8_t& b)
{
uint16_t src = ToUint16BE(b);
int16_t dest = 0;
memcpy(&dest,&src,sizeof(uint16_t));
return dest;
}
int16_t BitConverter::ToSint16LE(uint8_t& b)
{
uint16_t src = ToUint16LE(b);
int16_t dest = 0;
memcpy(&dest,&src,sizeof(uint16_t));
return dest;
}
void BitConverter::FromSint64BE(uint8_t& b, int64_t v)
{
uint64_t dest = 0;
memcpy(&dest,&v,sizeof(uint64_t));
FromUint64BE(b, dest);
}
void BitConverter::FromSint32BE(uint8_t& b, int32_t v)
{
uint32_t dest = 0;
memcpy(&dest,&v,sizeof(uint32_t));
FromUint32BE(b, dest);
}
void BitConverter::FromSint16BE(uint8_t& b, int16_t v)
{
uint16_t dest = 0;
memcpy(&dest,&v,sizeof(uint16_t));
FromUint16BE(b, dest);
}
void BitConverter::FromSint64LE(uint8_t& b, int64_t v)
{
uint64_t dest = 0;
memcpy(&dest,&v,sizeof(uint64_t));
FromUint64LE(b, dest);
}
void BitConverter::FromSint32LE(uint8_t& b, int32_t v)
{
uint32_t dest = 0;
memcpy(&dest,&v,sizeof(uint32_t));
FromUint32LE(b, dest);
}
void BitConverter::FromSint16LE(uint8_t& b, int16_t v)
{
uint16_t dest = 0;
memcpy(&dest,&v,sizeof(uint16_t));
FromUint16LE(b, dest);
}
}

View File

@@ -1,4 +1,5 @@
#include "TessesFramework/Streams/ByteReader.hpp" #include "TessesFramework/Streams/ByteReader.hpp"
#include "TessesFramework/Serialization/BitConverter.hpp"
namespace Tesses::Framework::Streams namespace Tesses::Framework::Streams
{ {
std::shared_ptr<Stream> ByteReader::GetStream() std::shared_ptr<Stream> ByteReader::GetStream()
@@ -150,4 +151,19 @@ namespace Tesses::Framework::Streams
return *(double*)&v; return *(double*)&v;
} }
Uuid ByteReader::ReadUuid()
{
uint8_t data[16];
if(this->strm->ReadBlock(data, 16) != 16)
throw std::runtime_error("End of file");
return Serialization::BitConverter::ToUuid(data[0]);
}
void ByteReader::ReadUuid(Uuid& uuid)
{
uint8_t data[16];
if(this->strm->ReadBlock(data, 16) != 16)
throw std::runtime_error("End of file");
Serialization::BitConverter::ToUuid(data[0],uuid);
}
} }

View File

@@ -1,4 +1,5 @@
#include "TessesFramework/Streams/ByteWriter.hpp" #include "TessesFramework/Streams/ByteWriter.hpp"
#include "TessesFramework/Serialization/BitConverter.hpp"
namespace Tesses::Framework::Streams namespace Tesses::Framework::Streams
{ {
std::shared_ptr<Stream> ByteWriter::GetStream() std::shared_ptr<Stream> ByteWriter::GetStream()
@@ -129,5 +130,11 @@ namespace Tesses::Framework::Streams
uint64_t data = *(uint64_t*)&v; uint64_t data = *(uint64_t*)&v;
WriteU64LE(data); WriteU64LE(data);
} }
void ByteWriter::WriteUuid(const Uuid& uuid)
{
uint8_t data[16];
Serialization::BitConverter::FromUuid(data[0], uuid);
this->strm->WriteBlock(data, 16);
}
} }

View File

@@ -712,6 +712,244 @@ if (iResult != 0) {
}
else if(portable_str == "absolute")
{
if(dict2.TryGetValueAsType("user",portable_str))
{
if(portable_str != "system")
{
VFSPath userDir = portable_str;
portable_config.user = userDir.CollapseRelativeParents();
}
}
if(dict2.TryGetValueAsType("documents", portable_str))
{
if(portable_str != "system")
{
if(portable_str == "default")
{
if(portable_config.user)
{
portable_config.documents = *(portable_config.user) / "Documents";
}
}
else
{
VFSPath userDir = portable_str;
portable_config.documents = userDir.CollapseRelativeParents();
}
}
}
if(dict2.TryGetValueAsType("downloads", portable_str))
{
if(portable_str != "system")
{
if(portable_str == "default")
{
if(portable_config.user)
{
portable_config.downloads = *(portable_config.user) / "Downloads";
}
}
else
{
VFSPath userDir = portable_str;
portable_config.downloads = userDir.CollapseRelativeParents();
}
}
}
if(dict2.TryGetValueAsType("desktop", portable_str))
{
if(portable_str != "system")
{
if(portable_str == "default")
{
if(portable_config.user)
{
portable_config.desktop = *(portable_config.user) / "Desktop";
}
}
else
{
VFSPath userDir = portable_str;
portable_config.desktop = userDir.CollapseRelativeParents();
}
}
}
if(dict2.TryGetValueAsType("pictures", portable_str))
{
if(portable_str != "system")
{
if(portable_str == "default")
{
if(portable_config.user)
{
portable_config.pictures = *(portable_config.user) / "Pictures";
}
}
else
{
VFSPath userDir = portable_str;
portable_config.pictures = userDir.CollapseRelativeParents();
}
}
}
if(dict2.TryGetValueAsType("videos", portable_str))
{
if(portable_str != "system")
{
if(portable_str == "default")
{
if(portable_config.user)
{
portable_config.videos = *(portable_config.user) / "Videos";
}
}
else
{
VFSPath userDir = portable_str;
portable_config.videos = userDir.CollapseRelativeParents();
}
}
}
if(dict2.TryGetValueAsType("music", portable_str))
{
if(portable_str != "system")
{
if(portable_str == "default")
{
if(portable_config.user)
{
portable_config.music = *(portable_config.user) / "Music";
}
}
else
{
VFSPath userDir = portable_str;
portable_config.music = userDir.CollapseRelativeParents();
}
}
}
if(dict2.TryGetValueAsType("config", portable_str))
{
if(portable_str != "system")
{
if(portable_str == "default")
{
if(portable_config.user)
{
portable_config.config = *(portable_config.user) / "Config";
}
}
else
{
VFSPath userDir = portable_str;
portable_config.config = userDir.CollapseRelativeParents();
}
}
}
if(dict2.TryGetValueAsType("cache", portable_str))
{
if(portable_str != "system")
{
if(portable_str == "default")
{
if(portable_config.user)
{
portable_config.cache = *(portable_config.user) / "Cache";
}
}
else
{
VFSPath userDir = portable_str;
portable_config.cache = userDir.CollapseRelativeParents();
}
}
}
if(dict2.TryGetValueAsType("data", portable_str))
{
if(portable_str != "system")
{
if(portable_str == "default")
{
if(portable_config.user)
{
portable_config.data = *(portable_config.user) / "Data";
}
}
else
{
VFSPath userDir = portable_str;
portable_config.data = userDir.CollapseRelativeParents();
}
}
}
if(dict2.TryGetValueAsType("state", portable_str))
{
if(portable_str != "system")
{
if(portable_str == "default")
{
if(portable_config.user)
{
portable_config.state = *(portable_config.user) / "State";
}
}
else
{
VFSPath userDir = portable_str;
portable_config.state = userDir.CollapseRelativeParents();
}
}
}
if(dict2.TryGetValueAsType("temp", portable_str))
{
if(portable_str != "system")
{
if(portable_str == "default")
{
if(portable_config.user)
{
portable_config.temp = *(portable_config.user) / "Temp";
}
}
else
{
VFSPath userDir = portable_str;
portable_config.temp = userDir.CollapseRelativeParents();
}
}
}
} }
} }
} }

214
src/Uuid.cpp Normal file
View File

@@ -0,0 +1,214 @@
#include "TessesFramework/Uuid.hpp"
#include "TessesFramework/Http/HttpUtils.hpp"
#include "TessesFramework/Crypto/Crypto.hpp"
namespace Tesses::Framework {
Uuid Uuid::Generate()
{
//xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
Uuid uuid;
Uuid::Generate(uuid);
return uuid;
}
void Uuid::Generate(Uuid& uuid)
{
std::vector<uint8_t> bytes(16);
Crypto::RandomBytes(bytes, "TF_UUID");
uuid.time_low = (uint32_t)bytes[0];
uuid.time_low |= (uint32_t)bytes[1] << 8;
uuid.time_low |= (uint32_t)bytes[2] << 16;
uuid.time_low |= (uint32_t)bytes[3] << 24;
uuid.time_mid = (uint16_t)bytes[4];
uuid.time_mid |= (uint16_t)bytes[5] << 8;
uuid.time_hi_and_version = (uint16_t)bytes[6];
uuid.time_hi_and_version |= (uint16_t)bytes[7] << 8;
uuid.clock_seq_hi_and_reserved = bytes[8];
uuid.clock_seq_low = bytes[9];
for(size_t i = 0; i < 6; i++)
{
uuid.node[i] = bytes[i+10];
}
uuid.time_hi_and_version &= ~0x00F0;
uuid.time_hi_and_version |= 0x0040;
uuid.clock_seq_hi_and_reserved &= ~0b11000000;
uuid.clock_seq_hi_and_reserved |= 0b10000000;
}
bool Uuid::TryParse(std::string text, Uuid& uuid)
{
std::array<uint8_t,32> hex_digits;
size_t hex_offset = 0;
size_t text_offset = 0;
for(; text_offset < text.size(); text_offset++)
{
if(text[text_offset] == '{' && (text_offset != 0 || hex_offset != 0))
return false;
if(text[text_offset] == '}' && hex_offset < 32)
return false;
if(text[text_offset] == '-' && hex_offset != 8 && hex_offset != 12 && hex_offset != 16 && hex_offset != 20)
return false;
if((text[text_offset] >= 'A' && text[text_offset] <= 'F') || (text[text_offset] >= 'a' && text[text_offset] <= 'f') || text[text_offset] >= '0' && text[text_offset] <= '9')
{
if(hex_offset >= 32) return false;
hex_digits[hex_offset] = Http::HttpUtils::HexToNibble(text[text_offset]);
hex_offset++;
}
else return false;
}
uint8_t b = hex_digits[0] << 4 | hex_digits[1];
uuid.time_low = (uint32_t)b;
b = hex_digits[2] << 4 | hex_digits[3];
uuid.time_low |= (uint32_t)b << 8;
b = hex_digits[4] << 4 | hex_digits[5];
uuid.time_low |= (uint32_t)b << 16;
b = hex_digits[6] << 4 | hex_digits[7];
uuid.time_low |= (uint32_t)b << 24;
b = hex_digits[8] << 4 | hex_digits[9];
uuid.time_mid = (uint16_t)b;
b = hex_digits[10] << 4 | hex_digits[11];
uuid.time_mid |= (uint16_t)b << 8;
b = hex_digits[12] << 4 | hex_digits[13];
uuid.time_hi_and_version = (uint16_t)b;
b = hex_digits[14] << 4 | hex_digits[15];
uuid.time_hi_and_version |= (uint16_t)b << 8;
uuid.clock_seq_hi_and_reserved = hex_digits[16] << 4 | hex_digits[17];
uuid.clock_seq_low = hex_digits[18] << 4 | hex_digits[19];
for(size_t i = 0; i < 6; i++)
{
uuid.node[i] = hex_digits[20+(i*2)] << 4 | hex_digits[21+(i*2)];
}
return true;
}
//9c4994e7-3c82-4c30-a459-8fdcd960b4ac
std::string Uuid::ToString(UuidStringifyConfig cfg) const
{
bool hasCurly = ((int)cfg & (int)UuidStringifyConfig::HasCurly) != 0;
bool isUppercase = ((int)cfg & (int)UuidStringifyConfig::IsUppercase) != 0;
bool hasDash = ((int)cfg & (int)UuidStringifyConfig::HasDashes) != 0;
std::string uuid_str = "";
if(hasCurly)
uuid_str += "{";
uint8_t byte = (uint8_t)(this->time_low & 0xFF);
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
byte = (uint8_t)((this->time_low >> 8) & 0xFF);
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
byte = (uint8_t)((this->time_low >> 16) & 0xFF);
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
byte = (uint8_t)((this->time_low >> 24) & 0xFF);
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
if(hasDash)
uuid_str += "-";
byte = (uint8_t)(this->time_mid & 0xFF);
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
byte = (uint8_t)((this->time_mid >> 8) & 0xFF);
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
if(hasDash)
uuid_str += "-";
byte = (uint8_t)(this->time_hi_and_version & 0xFF);
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
byte = (uint8_t)((this->time_hi_and_version >> 8) & 0xFF);
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
if(hasDash)
uuid_str += "-";
uuid_str += Http::HttpUtils::NibbleToHex(this->clock_seq_hi_and_reserved>>4,isUppercase);
uuid_str += Http::HttpUtils::NibbleToHex(this->clock_seq_hi_and_reserved,isUppercase);
uuid_str += Http::HttpUtils::NibbleToHex(this->clock_seq_low>>4,isUppercase);
uuid_str += Http::HttpUtils::NibbleToHex(this->clock_seq_low,isUppercase);
if(hasDash)
uuid_str += "-";
for(size_t i = 0; i < 6; i++)
{
byte = this->node[i];
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
}
if(hasCurly)
uuid_str += "}";
return uuid_str;
}
bool Uuid::IsEmpty() const
{
return this->time_low == 0 &&
this->time_mid == 0 &&
this->time_hi_and_version == 0 &&
this->clock_seq_hi_and_reserved == 0 &&
this->clock_seq_low == 0 &&
this->node[0] == 0 &&
this->node[1] == 0 &&
this->node[2] == 0 &&
this->node[3] == 0 &&
this->node[4] == 0 &&
this->node[5] == 0;
}
bool operator==(const Uuid& left, const Uuid& right)
{
return left.time_low == right.time_low &&
left.time_mid == right.time_mid &&
left.time_hi_and_version == right.time_hi_and_version &&
left.clock_seq_hi_and_reserved == right.clock_seq_hi_and_reserved &&
left.clock_seq_low == right.clock_seq_low &&
left.node[0] == right.node[0] &&
left.node[1] == right.node[1] &&
left.node[2] == right.node[2] &&
left.node[3] == right.node[3] &&
left.node[4] == right.node[4] &&
left.node[5] == right.node[5];
}
bool operator!=(const Uuid& left, const Uuid& right)
{
return left.time_low != right.time_low &&
left.time_mid != right.time_mid &&
left.time_hi_and_version != right.time_hi_and_version &&
left.clock_seq_hi_and_reserved != right.clock_seq_hi_and_reserved &&
left.clock_seq_low != right.clock_seq_low &&
left.node[0] != right.node[0] &&
left.node[1] != right.node[1] &&
left.node[2] != right.node[2] &&
left.node[3] != right.node[3] &&
left.node[4] != right.node[4] &&
left.node[5] != right.node[5];
}
}