Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
Utilities.cpp
1/*
2Copyright (C) 2016-2019,2025 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16#include <algorithm>
17#include <iostream>
18#include <cstring>
20#include "aeongames/Utilities.hpp"
21namespace AeonGames
22{
23 const std::string GetFileExtension ( const std::string& aFilePath )
24 {
26 std::size_t extpos = aFilePath.find_last_of ( "/\\" );
27 extpos = ( extpos == std::string::npos ) ? 0 : extpos;
28 extpos = aFilePath.find_first_of ( '.', extpos );
29 if ( extpos != std::string::npos )
30 {
31 std::string extension = aFilePath.substr ( extpos );
32 std::transform ( extension.begin(), extension.end(), extension.begin(), tolower );
33 return extension;
34 }
35 return std::string();
36 }
37
38 bool FileExists ( const std::string& aFilePath )
39 {
40 struct stat stat_buffer {};
41 return ( ::stat ( aFilePath.c_str(), &stat_buffer ) == 0 ) ? true : false;
42 }
43
44 OptionHandler::OptionHandler ( const char aShortOption, const char* aLongOption, void ( *aHandler ) ( const char*, void* ), void* aUserData ) :
45 mHandler{aHandler},
46 mShortOption{aShortOption},
47 mLongOption{aLongOption},
48 mUserData{aUserData} {};
49 OptionHandler::~OptionHandler() = default;
51 {
52 return mShortOption;
53 }
54 const char* OptionHandler::GetLongOption() const
55 {
56 return mLongOption;
57 }
59 {
60 return mUserData;
61 }
62 void OptionHandler::operator() ( const char* aArgument, void* aUserData ) const
63 {
64 mHandler ( aArgument, aUserData );
65 }
66
67 void ProcessOpts ( int argc, char *argv[], const OptionHandler* aOptionHandler, size_t aOptionHandlerCount )
68 {
69 for ( int i = 0; i < argc; ++i )
70 {
71 if ( argv[i][0] == '-' )
72 {
73 const OptionHandler* handler = nullptr;
74 if ( argv[i][1] == '-' )
75 {
76 // Long Option
77 handler = std::find_if ( aOptionHandler, aOptionHandler + aOptionHandlerCount,
78 [argv, i] ( const OptionHandler & handler )
79 {
80 return strcmp ( &argv[i][2], handler.GetLongOption() ) == 0;
81 } );
82 }
83 else
84 {
85 // Short Option
86 handler = std::find_if ( aOptionHandler, aOptionHandler + aOptionHandlerCount,
87 [argv, i] ( const OptionHandler & handler )
88 {
89 return argv[i][1] == handler.GetShortOption();
90 } );
91 }
92 if ( handler && handler != ( aOptionHandler + aOptionHandlerCount ) )
93 {
94 char* argument = ( ( i + 1 ) < argc ) ? ( argv[i + 1][0] != '-' ) ? argv[i + 1] : nullptr : nullptr;
95 ( *handler ) ( argument, handler->GetUserData() );
96 }
97 }
98 }
99 }
100}
Platform-specific macros, includes, and DLL export/import definitions.
Command-line option handler.
DLL void * GetUserData() const
Get the user data pointer.
Definition Utilities.cpp:58
DLL const char * GetLongOption() const
Get the long option string.
Definition Utilities.cpp:54
DLL const char GetShortOption() const
Get the short option character.
Definition Utilities.cpp:50
DLL void operator()(const char *aArgument, void *aUserData) const
Invoke the handler callback.
Definition Utilities.cpp:62
DLL OptionHandler(const char aShortOption, const char *aLongOption, void(*aHandler)(const char *, void *), void *aUserData=nullptr)
Construct an OptionHandler.
Definition Utilities.cpp:44
<- This is here just for the literals
Definition AABB.hpp:31
DLL void ProcessOpts(int argc, char *argv[], const OptionHandler *aOptionHandler, size_t aOptionHandlerCount)
Process command-line options.
Definition Utilities.cpp:67
DLL bool FileExists(const std::string &aFilePath)
Check whether a file exists at the given path.
Definition Utilities.cpp:38
DLL const std::string GetFileExtension(const std::string &aFilePath)
Extract the file extension from a file path.
Definition Utilities.cpp:23