OpenCores
URL https://opencores.org/ocsvn/lxp32/lxp32/trunk

Subversion Repositories lxp32

[/] [lxp32/] [trunk/] [tools/] [src/] [lxp32asm/] [utils.cpp] - Blame information for rev 9

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 9 ring0_mipt
/*
2
 * Copyright (c) 2016 by Alex I. Kuznetsov.
3
 *
4
 * Part of the LXP32 CPU IP core.
5
 *
6
 * This module implements members of the Utils namespace.
7
 */
8
 
9
#include "utils.h"
10
 
11
#include <iostream>
12
#include <fstream>
13
#include <algorithm>
14
#include <stdexcept>
15
#include <cstring>
16
 
17
std::string Utils::urlEncode(const std::string &str) {
18
        std::string res;
19
        for(std::size_t i=0;i<str.size();i++) {
20
                char ch=str[i];
21
                if(ch>='A'&&ch<='Z') res.push_back(ch);
22
                else if(ch>='a'&&ch<='z') res.push_back(ch);
23
                else if(ch>='0'&&ch<='9') res.push_back(ch);
24
                else if(ch=='-'||ch=='_'||ch=='.'||ch=='~') res.push_back(ch);
25
                else res+="%"+hex(ch);
26
        }
27
        return res;
28
}
29
 
30
std::string Utils::urlDecode(const std::string &str) {
31
        std::string res;
32
        for(std::size_t i=0;i<str.size();i++) {
33
                char ch=str[i];
34
                if(ch!='%') res.push_back(ch);
35
                else {
36
                        auto hexcode=str.substr(i+1,2);
37
                        i+=hexcode.size();
38
                        try {
39
                                if(hexcode.size()!=2) throw std::exception();
40
                                auto u=static_cast<unsigned char>(std::stoul(hexcode,nullptr,16));
41
                                res.push_back(static_cast<char>(u));
42
                        }
43
                        catch(std::exception &) {
44
                                throw std::runtime_error("Ill-formed URL-encoded string");
45
                        }
46
                }
47
        }
48
        return res;
49
}
50
 
51
std::string Utils::normalizeSeparators(const std::string &path) {
52
        std::string str(path);
53
#ifdef _WIN32
54
        std::replace(str.begin(),str.end(),'\\','/');
55
#endif
56
        return str;
57
}
58
 
59
std::string Utils::nativeSeparators(const std::string &path) {
60
        std::string str(path);
61
#ifdef _WIN32
62
        std::replace(str.begin(),str.end(),'/','\\');
63
#endif
64
        return str;
65
}
66
 
67
bool Utils::isAbsolutePath(const std::string &path) {
68
        auto native=nativeSeparators(path);
69
        if(native.empty()) return false;
70
        if(native[0]=='/') return true;
71
#ifdef _WIN32
72
        if(native.size()>1&&native[1]==':') return true;
73
#endif
74
        return false;
75
}
76
 
77
bool Utils::fileExists(const std::string &path) {
78
        std::ifstream in(nativeSeparators(path),std::ios_base::in);
79
        if(!in) return false;
80
        return true;
81
}
82
 
83
std::string Utils::relativePath(const std::string &from,const std::string &to) {
84
// Normalize directory separators
85
        auto nfrom=normalizeSeparators(from);
86
        auto nto=normalizeSeparators(to);
87
 
88
        if(nto.empty()) return std::string();
89
 
90
// If "nto" is an absolute path, just return it
91
        if(isAbsolutePath(nto)) return nativeSeparators(nto);
92
 
93
// Process relative path
94
        auto pos=nfrom.find_last_of('/');
95
        if(pos==std::string::npos) return nativeSeparators(nto);
96
        else return nativeSeparators(nfrom.substr(0,pos+1)+nto);
97
}
98
 
99
std::string Utils::dequoteString(const std::string &str) {
100
        if(str.size()<2) throw std::runtime_error("String literal expected");
101
        if(str.front()!='\"'||str.back()!='\"') throw std::runtime_error("String literal expected");
102
        return str.substr(1,str.size()-2);
103
}
104
 
105
bool Utils::ishexdigit(char ch) {
106
        static const char *digits="0123456789ABCDEFabcdef";
107
        return (std::strchr(digits,ch)!=NULL);
108
}
109
 
110
bool Utils::isoctdigit(char ch) {
111
        static const char *digits="01234567";
112
        return (std::strchr(digits,ch)!=NULL);
113
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.