This repository has been archived by the owner on Apr 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Path utilities to easily deal with paths
- Loading branch information
1 parent
9d9d400
commit 5dbf6df
Showing
7 changed files
with
102 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,7 @@ These are the current features. | |
- Image | ||
- Input system | ||
- Logs | ||
- Path | ||
- Shader | ||
- Sprite | ||
- Str | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include "engine/path.h" | ||
|
||
#include "engine/assertions.h" | ||
|
||
#include <stdarg.h> | ||
#include <string.h> | ||
|
||
Path makePath(int _count, ...) | ||
{ | ||
Path path; | ||
memset(path.str, '\0', _PATH_MAX_SIXE); | ||
|
||
va_list ap; | ||
va_start(ap, _count); | ||
|
||
for (int k = 0; k < _count; ++k) { | ||
appendPath(&path, va_arg(ap, char*)); | ||
} | ||
|
||
va_end(ap); | ||
|
||
return path; | ||
} | ||
|
||
void appendPath(Path* _dest, const char* _append) | ||
{ | ||
// ASSERT_MSG(_dest != NULL, "[Path] Invalid parameter"); | ||
// ASSERT_MSG(_append != NULL, "[Path] Invalid parameter"); | ||
ASSERT_MSG(_dest->str != _append, "[Path] Append string should not be the dest string"); | ||
|
||
if (_dest == NULL || _append == NULL || _dest->str == _append) { | ||
return; | ||
} | ||
|
||
int sizeDest = strlen(_dest->str); | ||
|
||
// Exception case where _dest is empty | ||
if (sizeDest == 0) { | ||
int size = strlen(_append); | ||
size = size < _PATH_MAX_SIXE ? size : _PATH_MAX_SIXE - 1; | ||
strncat(_dest->str, _append, size); | ||
return; | ||
} | ||
|
||
// Exception case where _dest is already full | ||
if (sizeDest == _PATH_MAX_SIXE - 1) { | ||
return; | ||
} | ||
|
||
// Append / to the end of dest if not there | ||
if (_dest->str[sizeDest - 1] != '/') { | ||
strncat(_dest->str, "/", 2); | ||
sizeDest += 1; | ||
} | ||
|
||
// Bypass the / in _append because we already added it in _dest | ||
int offset = 0; | ||
if (_append[0] == '/') { | ||
offset = 1; | ||
} | ||
|
||
int sizeAppend = strlen(_append) - offset; | ||
sizeAppend = ((sizeDest + sizeAppend) < _PATH_MAX_SIXE) ? sizeAppend : (_PATH_MAX_SIXE - 1 - sizeDest); | ||
int finalSize = sizeDest + sizeAppend; | ||
sizeAppend = sizeAppend >= 0 ? sizeAppend : 0; | ||
|
||
strncat(_dest->str, _append + offset, sizeAppend); | ||
|
||
_dest->str[finalSize] = '\0'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#pragma once | ||
|
||
#define _PATH_MAX_SIXE 256 // Includes '\0' | ||
|
||
/// Path is a fixed size string with / between each words. | ||
typedef struct | ||
{ | ||
char str[_PATH_MAX_SIXE]; | ||
} Path; | ||
|
||
Path makePath(int _count, ...); | ||
void appendPath(Path* _dest, const char* _append); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters