-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGeneral.cpp
119 lines (100 loc) · 2.69 KB
/
General.cpp
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
/*
* Argus Open Source
* Software to apply Statistical Disclosure Control techniques
*
* Copyright 2014 Statistics Netherlands
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the European Union Public Licence
* (EUPL) version 1.1, as published by the European Commission.
*
* You can find the text of the EUPL v1.1 on
* https://joinup.ec.europa.eu/software/page/eupl/licence-eupl
*
* This software is distributed on an "AS IS" basis without
* warranties or conditions of any kind, either express or implied.
*/
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include "General.h"
using namespace std;
void ArgusTrace(const char* Format, ...)
{
}
int RemoveStringInPlace(char* subject, char ch) {
int occurences = 0;
char *pos = subject;
while ((pos = strchr(pos, ch)) != NULL) {
memmove(pos, pos + 1, strlen(pos));
occurences++;
}
return occurences;
}
int RemoveStringInPlace(string& subject, char ch) {
int occurences = 0;
size_t pos = 0;
while((pos = subject.find(ch, pos)) != string::npos) {
subject.erase(pos, 1);
occurences++;
}
return occurences;
}
void ReplaceStringInPlace(string& subject, const char search, const char replace)
{
size_t pos = 0;
while((pos = subject.find(search, pos)) != string::npos) {
subject[pos] = replace;
pos++;
}
}
// sort strings
void QuickSortStringArray(vector<string> &s)
{
sort(s.begin(), s.end());
}
// binary search to see if an string is in an array of strings
// taking in to account weather to look at missings or not.
// returns the position if the string is found -1 if not.
// IsMissing on true if x = Missing1 or x = Missing2
int BinSearchStringArray(vector<string> &s, const string &x, int nMissing, bool &IsMissing)
{
ASSERT((int) s.size() > nMissing);
IsMissing = false;
vector<string>::iterator it = lower_bound(s.begin(), s.end() - nMissing, x);
if (it != s.end() - nMissing) {
if (*it == x) {
return it - s.begin();
}
}
// equal to missing1 or -2? // code missing not always the highest
for (int mis = s.size() - nMissing; mis < (int) (s.size()); mis++) {
if (x == s[mis]) {
IsMissing = true;
return mis;
}
}
return -1;
}
// add spaces before to make the string the right length
void AddSpacesBefore(char *str, int len)
{
int lstr = strlen(str);
if (lstr < len) {
char *dest = str + len - lstr;
memmove(dest, str, lstr);
while (dest-- != str) {
*dest = ' ';
}
str[len] = '\0';
}
}
void AddSpacesBefore(string& str, int len)
{
int width = str.length();
if (width < len)
{
str.insert((size_t)0, (size_t)(len - width), ' ');
}
}