-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhistd.cpp
More file actions
122 lines (102 loc) · 3.19 KB
/
histd.cpp
File metadata and controls
122 lines (102 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* Copyright (c) 2016-2018 Hiroyuki Ichida. All rights reserved.
*
* @file histd.cpp
* @author Hiroyuki Ichida <histfd@gmail.com>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 (GPL-2.0)
* as published by the Free Software Foundation, Inc.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "histd.h"
namespace HI_NAMESPACE{
//-----------------------------------------------------------------------------
void bad_alloc_exception(const char *function){
std::cerr << ERROR_STRING << "a fatal error occurred in " << function \
<< ". Program execution aborted." << ENDL;
std::exit(EXIT_FAILURE);
}
//-----------------------------------------------------------------------------
char * FileRead(const char *file){
int fDesc = open(file, O_RDONLY);
if(0 > fDesc)
return NULL;
lseek(fDesc, 0, SEEK_SET);
long size = lseek(fDesc, 0, SEEK_END);
char *buf = new char [size+10];
std::memset(buf, '\0', size+10);
lseek(fDesc, 0, SEEK_SET);
read(fDesc, buf, size);
close(fDesc);
return buf;
}
//-----------------------------------------------------------------------------
bool split(StringArray &result, const std::string line, const char delimiter){
std::stringstream sstr(line);
std::string element;
while(std::getline(sstr, element, delimiter))
result.push_back(element);
return(true);
}
//-----------------------------------------------------------------------------
bool split(StringArray &result, const char *line, const char *delim){
size_t size = std::strlen(line);
try{
char *buf = new char [size+1];
std::memset(buf, '\0', size+1);
std::strncpy(buf, line, size);
char *saveptr;
char *element = strtok_r(buf, delim, &saveptr);
while(NULL != element){
result.push_back(element);
element = strtok_r(NULL, delim, &saveptr);
}
delete[] buf;
}
catch(std::bad_alloc){
return false;
}
return true;
}
//-----------------------------------------------------------------------------
RETVAL rmspace(char *seq){
size_t size = std::strlen(seq);
if(0 == size)
return RV_FALSE;
char *newseq = new char [size+1];
std::memset(newseq, '\0', size+1);
size_t newpos=0;
for(size_t pos=0; pos<size; pos++){
if(0 != std::isalpha(seq[pos])){
newseq[newpos] = seq[pos];
++newpos;
}
}
std::memset(seq, '\0', size);
std::strncpy(seq, newseq, newpos);
delete[] newseq;
return RV_TRUE;
}
//-----------------------------------------------------------------------------
int toupper(char *str){
size_t pos=0;
char *chr = str;
while ('\0' != *chr) {
if('a'<=*chr && 'z'>=*chr) {
++pos;
*chr -= 0x20;
}
++chr;
}
return pos;
}
//-----------------------------------------------------------------------------
} // End of namespace