OpenGothic
Open source reimplementation of Gothic I and II
Loading...
Searching...
No Matches
fileext.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <cstring>
5#include <cctype>
6
7namespace FileExt {
8 inline bool hasExt(std::string_view s,const char* extIn) {
9 if(extIn==nullptr)
10 return std::string::npos==s.find('.');
11 const size_t l = std::strlen(extIn);
12 if(l+1>s.size())
13 return false;
14 const size_t off = s.size()-l;
15 if(s[off-1]!='.')
16 return false;
17 for(size_t i=0;i<l;++i) {
18 char a = char(std::tolower(s[off+i]));
19 char b = char(std::tolower(extIn[i]));
20 if(a!=b)
21 return false;
22 }
23 return true;
24 }
25
26 inline bool hasExt(const char* s,const char* extIn) {
27 const size_t ssize = std::strlen(s);
28 const size_t l = std::strlen(extIn);
29 if(l+1>ssize)
30 return false;
31 const size_t off = ssize-l;
32 if(s[off-1]!='.')
33 return false;
34 for(size_t i=0;i<l;++i) {
35 char a = char(std::tolower(s[off+i]));
36 char b = char(std::tolower(extIn[i]));
37 if(a!=b)
38 return false;
39 }
40 return true;
41 }
42
43 inline bool exchangeExt(std::string& s,const char* extIn,const char* extOut) {
44 if(!hasExt(s,extIn))
45 return false;
46 if(extIn==nullptr) {
47 s.push_back('.');
48 s += extOut;
49 return true;
50 }
51 const size_t l1 = std::strlen(extIn);
52 const size_t l2 = std::strlen(extOut);
53 if(l1<l2)
54 s.resize(s.size()+l2-l1); else
55 if(l1>l2)
56 s.resize(s.size()+l1-l2);
57
58 const size_t off = s.size()-l2;
59 for(size_t i=0;i<l2;++i)
60 s[off+i] = extOut[i];
61 return true;
62 }
63
64 inline void assignExt(std::string& s,const char* extOut){
65 size_t cut = s.rfind('.');
66 if(cut==std::string::npos) {
67 s.push_back('.');
68 s += extOut;
69 return;
70 }
71 const size_t l1 = s.size()-cut-1;
72 const size_t l2 = std::strlen(extOut);
73 if(l1<l2)
74 s.resize(s.size()+l2-l1); else
75 if(l1>l2)
76 s.resize(s.size()+l1-l2);
77 const size_t off = cut+1;
78 for(size_t i=0;i<l2;++i)
79 s[off+i] = extOut[i];
80 }
81
82 inline std::string addExt(const std::string& s,const char* ext){
83 if(s.size()>0 && s.back()=='.')
84 return s+&ext[1];
85 return s+ext;
86 }
87 }
bool exchangeExt(std::string &s, const char *extIn, const char *extOut)
Definition fileext.h:43
bool hasExt(std::string_view s, const char *extIn)
Definition fileext.h:8
std::string addExt(const std::string &s, const char *ext)
Definition fileext.h:82
void assignExt(std::string &s, const char *extOut)
Definition fileext.h:64