Switch to unified view

a/src/utils/smallut.cpp b/src/utils/smallut.cpp
...
...
52
52
53
int stringicmp(const string & s1, const string& s2) 
53
int stringicmp(const string & s1, const string& s2) 
54
{
54
{
55
    string::const_iterator it1 = s1.begin();
55
    string::const_iterator it1 = s1.begin();
56
    string::const_iterator it2 = s2.begin();
56
    string::const_iterator it2 = s2.begin();
57
    int size1 = s1.length(), size2 = s2.length();
57
    string::size_type size1 = s1.length(), size2 = s2.length();
58
    char c1, c2;
58
    char c1, c2;
59
59
60
    if (size1 > size2) {
60
    if (size1 < size2) {
61
    while (it1 != s1.end()) { 
61
    while (it1 != s1.end()) { 
62
        c1 = ::toupper(*it1);
62
        c1 = ::toupper(*it1);
63
        c2 = ::toupper(*it2);
63
        c2 = ::toupper(*it2);
64
        if (c1 != c2) {
64
        if (c1 != c2) {
65
        return c1 > c2 ? 1 : -1;
65
        return c1 > c2 ? 1 : -1;
66
        }
66
        }
67
        ++it1; ++it2;
67
        ++it1; ++it2;
68
    }
68
    }
69
    return size1 == size2 ? 0 : 1;
69
    return size1 == size2 ? 0 : -1;
70
    } else {
70
    } else {
71
    while (it2 != s2.end()) { 
71
    while (it2 != s2.end()) { 
72
        c1 = ::toupper(*it1);
72
        c1 = ::toupper(*it1);
73
        c2 = ::toupper(*it2);
73
        c2 = ::toupper(*it2);
74
        if (c1 != c2) {
74
        if (c1 != c2) {
75
        return c1 > c2 ? 1 : -1;
75
        return c1 > c2 ? 1 : -1;
76
        }
76
        }
77
        ++it1; ++it2;
77
        ++it1; ++it2;
78
    }
78
    }
79
    return size1 == size2 ? 0 : -1;
79
    return size1 == size2 ? 0 : 1;
80
    }
80
    }
81
}
81
}
82
void stringtolower(string& io)
82
void stringtolower(string& io)
83
{
83
{
84
    string::iterator it = io.begin();
84
    string::iterator it = io.begin();
...
...
112
//  s1 is already lowercase
112
//  s1 is already lowercase
113
int stringlowercmp(const string & s1, const string& s2) 
113
int stringlowercmp(const string & s1, const string& s2) 
114
{
114
{
115
    string::const_iterator it1 = s1.begin();
115
    string::const_iterator it1 = s1.begin();
116
    string::const_iterator it2 = s2.begin();
116
    string::const_iterator it2 = s2.begin();
117
    int size1 = s1.length(), size2 = s2.length();
117
    string::size_type size1 = s1.length(), size2 = s2.length();
118
    char c2;
118
    char c2;
119
119
120
    if (size1 > size2) {
120
    if (size1 < size2) {
121
    while (it1 != s1.end()) { 
121
    while (it1 != s1.end()) { 
122
        c2 = ::tolower(*it2);
122
        c2 = ::tolower(*it2);
123
        if (*it1 != c2) {
123
        if (*it1 != c2) {
124
        return *it1 > c2 ? 1 : -1;
124
        return *it1 > c2 ? 1 : -1;
125
        }
125
        }
126
        ++it1; ++it2;
126
        ++it1; ++it2;
127
    }
127
    }
128
    return size1 == size2 ? 0 : 1;
128
    return size1 == size2 ? 0 : -1;
129
    } else {
129
    } else {
130
    while (it2 != s2.end()) { 
130
    while (it2 != s2.end()) { 
131
        c2 = ::tolower(*it2);
131
        c2 = ::tolower(*it2);
132
        if (*it1 != c2) {
132
        if (*it1 != c2) {
133
        return *it1 > c2 ? 1 : -1;
133
        return *it1 > c2 ? 1 : -1;
134
        }
134
        }
135
        ++it1; ++it2;
135
        ++it1; ++it2;
136
    }
136
    }
137
    return size1 == size2 ? 0 : -1;
137
    return size1 == size2 ? 0 : 1;
138
    }
138
    }
139
}
139
}
140
140
141
//  s1 is already uppercase
141
//  s1 is already uppercase
142
int stringuppercmp(const string & s1, const string& s2) 
142
int stringuppercmp(const string & s1, const string& s2) 
143
{
143
{
144
    string::const_iterator it1 = s1.begin();
144
    string::const_iterator it1 = s1.begin();
145
    string::const_iterator it2 = s2.begin();
145
    string::const_iterator it2 = s2.begin();
146
    int size1 = s1.length(), size2 = s2.length();
146
    string::size_type size1 = s1.length(), size2 = s2.length();
147
    char c2;
147
    char c2;
148
148
149
    if (size1 > size2) {
149
    if (size1 < size2) {
150
    while (it1 != s1.end()) { 
150
    while (it1 != s1.end()) { 
151
        c2 = ::toupper(*it2);
151
        c2 = ::toupper(*it2);
152
        if (*it1 != c2) {
152
        if (*it1 != c2) {
153
        return *it1 > c2 ? 1 : -1;
153
        return *it1 > c2 ? 1 : -1;
154
        }
154
        }
155
        ++it1; ++it2;
155
        ++it1; ++it2;
156
    }
156
    }
157
    return size1 == size2 ? 0 : 1;
157
    return size1 == size2 ? 0 : -1;
158
    } else {
158
    } else {
159
    while (it2 != s2.end()) { 
159
    while (it2 != s2.end()) { 
160
        c2 = ::toupper(*it2);
160
        c2 = ::toupper(*it2);
161
        if (*it1 != c2) {
161
        if (*it1 != c2) {
162
        return *it1 > c2 ? 1 : -1;
162
        return *it1 > c2 ? 1 : -1;
163
        }
163
        }
164
        ++it1; ++it2;
164
        ++it1; ++it2;
165
    }
165
    }
166
    return size1 == size2 ? 0 : -1;
166
    return size1 == size2 ? 0 : 1;
167
    }
167
    }
168
}
168
}
169
169
170
// Compare charset names, removing the more common spelling variations
170
// Compare charset names, removing the more common spelling variations
171
bool samecharset(const string &cs1, const string &cs2)
171
bool samecharset(const string &cs1, const string &cs2)
...
...
484
void utf8truncate(string &s, int maxlen)
484
void utf8truncate(string &s, int maxlen)
485
{
485
{
486
    if (s.size() <= string::size_type(maxlen))
486
    if (s.size() <= string::size_type(maxlen))
487
    return;
487
    return;
488
    Utf8Iter iter(s);
488
    Utf8Iter iter(s);
489
    int pos = 0;
489
    string::size_type pos = 0;
490
    while (iter++ != string::npos) 
490
    while (iter++ != string::npos) 
491
    if (iter.getBpos() < string::size_type(maxlen))
491
    if (iter.getBpos() < string::size_type(maxlen))
492
        pos = iter.getBpos();
492
        pos = iter.getBpos();
493
493
494
    s.erase(pos);
494
    s.erase(pos);
...
...
635
    roundable = double(size) / 1E6;
635
    roundable = double(size) / 1E6;
636
    } else {
636
    } else {
637
    unit = " GB ";
637
    unit = " GB ";
638
    roundable = double(size) / 1E9;
638
    roundable = double(size) / 1E9;
639
    }
639
    }
640
    size = round(roundable);
640
    size = off_t(round(roundable));
641
    sprintf(sizebuf, "%lld" "%s", (long long)size, unit);
641
    sprintf(sizebuf, "%lld" "%s", (long long)size, unit);
642
    return string(sizebuf);
642
    return string(sizebuf);
643
}
643
}
644
644
645
string breakIntoLines(const string& in, unsigned int ll, 
645
string breakIntoLines(const string& in, unsigned int ll, 
...
...
706
706
707
#ifdef __APPLE__
707
#ifdef __APPLE__
708
#undef USE_CLOCK_GETTIME
708
#undef USE_CLOCK_GETTIME
709
#endif
709
#endif
710
710
711
#ifdef WIN32
712
#include "safewindows.h"
713
// Note: struct timespec is defined by pthread.h (from pthreads-w32)
714
#ifndef CLOCK_REALTIME
715
#define CLOCK_REALTIME 0
716
#endif
717
718
LARGE_INTEGER getFILETIMEoffset()
719
{
720
  SYSTEMTIME s;
721
  FILETIME f;
722
  LARGE_INTEGER t;
723
724
  s.wYear = 1970;
725
  s.wMonth = 1;
726
  s.wDay = 1;
727
  s.wHour = 0;
728
  s.wMinute = 0;
729
  s.wSecond = 0;
730
  s.wMilliseconds = 0;
731
  SystemTimeToFileTime(&s, &f);
732
  t.QuadPart = f.dwHighDateTime;
733
  t.QuadPart <<= 32;
734
  t.QuadPart |= f.dwLowDateTime;
735
  return (t);
736
}
737
738
int clock_gettime(int X, struct timespec *tv)
739
{
740
  LARGE_INTEGER           t;
741
  FILETIME            f;
742
  double                  microseconds;
743
  static LARGE_INTEGER    offset;
744
  static double           frequencyToMicroseconds;
745
  static int              initialized = 0;
746
  static BOOL             usePerformanceCounter = 0;
747
748
  if (!initialized) {
749
      LARGE_INTEGER performanceFrequency;
750
      initialized = 1;
751
      usePerformanceCounter = QueryPerformanceFrequency(&performanceFrequency);
752
      if (usePerformanceCounter) {
753
          QueryPerformanceCounter(&offset);
754
          frequencyToMicroseconds = (double)performanceFrequency.QuadPart / 1000000.;
755
      }
756
      else {
757
          offset = getFILETIMEoffset();
758
          frequencyToMicroseconds = 10.;
759
      }
760
  }
761
  if (usePerformanceCounter) QueryPerformanceCounter(&t);
762
  else {
763
      GetSystemTimeAsFileTime(&f);
764
      t.QuadPart = f.dwHighDateTime;
765
      t.QuadPart <<= 32;
766
      t.QuadPart |= f.dwLowDateTime;
767
  }
768
769
  t.QuadPart -= offset.QuadPart;
770
  microseconds = (double)t.QuadPart / frequencyToMicroseconds;
771
  t.QuadPart = (long long)microseconds;
772
  tv->tv_sec = t.QuadPart / 1000000;
773
  tv->tv_nsec = (t.QuadPart % 1000000) * 1000;
774
  return (0);
775
}
776
#define USE_CLOCK_GETTIME
777
#else /* -> !_WIN32 */
778
711
#ifndef USE_CLOCK_GETTIME
779
#ifndef USE_CLOCK_GETTIME
712
#include <sys/time.h>
780
#include <sys/time.h>
781
#endif
782
713
#endif
783
#endif
714
784
715
static void gettime(clockid_t clk_id, struct timespec *ts)
785
static void gettime(clockid_t clk_id, struct timespec *ts)
716
{
786
{
717
#ifndef USE_CLOCK_GETTIME
787
#ifndef USE_CLOCK_GETTIME
...
...
738
{
808
{
739
  restart();
809
  restart();
740
}
810
}
741
811
742
// Reset and return value before rest in milliseconds
812
// Reset and return value before rest in milliseconds
743
long Chrono::restart()
813
time_t Chrono::restart()
744
{
814
{
745
  struct timespec tv;
815
  struct timespec tv;
746
  gettime(CLOCK_REALTIME, &tv);
816
  gettime(CLOCK_REALTIME, &tv);
747
  long ret = MILLIS(tv);
817
  time_t ret = MILLIS(tv);
748
  m_secs = tv.tv_sec;
818
  m_secs = tv.tv_sec;
749
  m_nsecs = tv.tv_nsec;
819
  m_nsecs = tv.tv_nsec;
750
  return ret;
820
  return ret;
751
}
821
}
752
822
753
// Get current timer value, milliseconds
823
// Get current timer value, milliseconds
754
long Chrono::millis(int frozen)
824
time_t Chrono::millis(int frozen)
755
{
825
{
756
    return nanos() / 1000000;
826
    return nanos() / 1000000;
757
}
827
}
758
828
759
//
829
//
760
long Chrono::micros(int frozen)
830
time_t Chrono::micros(int frozen)
761
{
831
{
762
    return nanos() / 1000;
832
    return nanos() / 1000;
763
}
833
}
764
834
765
long long Chrono::nanos(int frozen)
835
time_t Chrono::nanos(int frozen)
766
{
836
{
767
  if (frozen) {
837
  if (frozen) {
768
    return NANOS(frozen_tv);
838
    return NANOS(frozen_tv);
769
  } else {
839
  } else {
770
    struct timespec tv;
840
    struct timespec tv;
771
    gettime(CLOCK_REALTIME, &tv);
841
    gettime(CLOCK_REALTIME, &tv);
772
    return NANOS(tv);
842
    return NANOS(tv);
773
  }
843
  }
774
}
844
}
775
845
776
float Chrono::secs(int frozen)
846
double Chrono::secs(int frozen)
777
{
847
{
778
  struct timespec tv;
848
  struct timespec tv;
779
  gettime(CLOCK_REALTIME, &tv);
849
  gettime(CLOCK_REALTIME, &tv);
780
  float secs = (float)(frozen?frozen_tv.tv_sec:tv.tv_sec - m_secs);
850
  double secs = (double)(frozen?frozen_tv.tv_sec:tv.tv_sec - m_secs);
781
  float nsecs = (float)(frozen?frozen_tv.tv_nsec:tv.tv_nsec - m_nsecs); 
851
  double nsecs = (double)(frozen?frozen_tv.tv_nsec:tv.tv_nsec - m_nsecs); 
782
  return secs + nsecs * 1e-9;
852
  return secs + nsecs * 1e-9;
783
}
853
}
784
854
785
// Date is Y[-M[-D]]
855
// Date is Y[-M[-D]]
786
static bool parsedate(vector<string>::const_iterator& it, 
856
static bool parsedate(vector<string>::const_iterator& it, 
...
...
816
    if (it->length() > 2 || !it->length() || 
886
    if (it->length() > 2 || !it->length() || 
817
        it->find_first_not_of("0123456789") != string::npos) {
887
        it->find_first_not_of("0123456789") != string::npos) {
818
        return false;
888
        return false;
819
    }
889
    }
820
    if (it == end || sscanf(it++->c_str(), "%d", &dip->d1) != 1) {
890
    if (it == end || sscanf(it++->c_str(), "%d", &dip->d1) != 1) {
821
        return -1;
891
        return false;
822
    }
892
    }
823
893
824
    return true;
894
    return true;
825
}
895
}
826
896
...
...
1060
    sprintf(nbuf, "%d", _errno);
1130
    sprintf(nbuf, "%d", _errno);
1061
    reason->append(nbuf);
1131
    reason->append(nbuf);
1062
1132
1063
    reason->append(" : ");
1133
    reason->append(" : ");
1064
1134
1065
#ifdef sun
1135
#if defined(sun) || defined(_WIN32)
1066
    // Note: sun strerror is noted mt-safe ??
1136
    // Note: sun strerror is noted mt-safe ??
1067
    reason->append(strerror(_errno));
1137
    reason->append(strerror(_errno));
1068
#else
1138
#else
1069
#define ERRBUFSZ 200    
1139
#define ERRBUFSZ 200    
1070
    char errbuf[ERRBUFSZ];
1140
    char errbuf[ERRBUFSZ];
...
...
1104
    char cbuf[200];
1174
    char cbuf[200];
1105
    sprintf(cbuf, "Groups size %d grpsugidx size %d ugroups size %d",
1175
    sprintf(cbuf, "Groups size %d grpsugidx size %d ugroups size %d",
1106
        int(groups.size()), int(grpsugidx.size()), int(ugroups.size()));
1176
        int(groups.size()), int(grpsugidx.size()), int(ugroups.size()));
1107
    out.append(cbuf);
1177
    out.append(cbuf);
1108
1178
1109
    unsigned int ugidx = (unsigned int)-1;
1179
    size_t ugidx = (size_t)-1;
1110
    for (unsigned int i = 0; i < groups.size(); i++) {
1180
    for (unsigned int i = 0; i < groups.size(); i++) {
1111
    if (ugidx != grpsugidx[i]) {
1181
    if (ugidx != grpsugidx[i]) {
1112
        ugidx = grpsugidx[i];
1182
        ugidx = grpsugidx[i];
1113
        out.append("\n(");
1183
        out.append("\n(");
1114
        for (unsigned int j = 0; j < ugroups[ugidx].size(); j++) {
1184
        for (unsigned int j = 0; j < ugroups[ugidx].size(); j++) {
...
...
1133
    size_t ugsz0 = ugroups.size();
1203
    size_t ugsz0 = ugroups.size();
1134
    ugroups.insert(ugroups.end(), hl.ugroups.begin(), hl.ugroups.end());
1204
    ugroups.insert(ugroups.end(), hl.ugroups.begin(), hl.ugroups.end());
1135
1205
1136
    groups.insert(groups.end(), hl.groups.begin(), hl.groups.end());
1206
    groups.insert(groups.end(), hl.groups.begin(), hl.groups.end());
1137
    slacks.insert(slacks.end(), hl.slacks.begin(), hl.slacks.end());
1207
    slacks.insert(slacks.end(), hl.slacks.begin(), hl.slacks.end());
1138
    for (std::vector<unsigned int>::const_iterator it = hl.grpsugidx.begin(); 
1208
    for (std::vector<size_t>::const_iterator it = hl.grpsugidx.begin(); 
1139
     it != hl.grpsugidx.end(); it++) {
1209
     it != hl.grpsugidx.end(); it++) {
1140
    grpsugidx.push_back(*it + ugsz0);
1210
    grpsugidx.push_back(*it + ugsz0);
1141
    }
1211
    }
1142
}
1212
}
1143
1213
...
...
1269
1339
1270
int main(int argc, char **argv)
1340
int main(int argc, char **argv)
1271
{
1341
{
1272
    thisprog = *argv++;argc--;
1342
    thisprog = *argv++;argc--;
1273
1343
1274
#if 1
1344
#if 0
1275
    if (argc <=0 ) {
1345
    if (argc <=0 ) {
1276
        cerr << "Usage: smallut <stringtosplit>" << endl;
1346
        cerr << "Usage: smallut <stringtosplit>" << endl;
1277
        exit(1);
1347
        exit(1);
1278
    }
1348
    }
1279
    string s = *argv++;argc--;
1349
    string s = *argv++;argc--;
...
...
1374
    tokens.push_back("simple value");
1444
    tokens.push_back("simple value");
1375
    tokens.push_back("with \"quotes\"");
1445
    tokens.push_back("with \"quotes\"");
1376
    string out;
1446
    string out;
1377
    stringsToCSV(tokens, out);
1447
    stringsToCSV(tokens, out);
1378
    cout << "CSV line: [" << out << "]" << endl;
1448
    cout << "CSV line: [" << out << "]" << endl;
1449
#elif 1
1450
    string sshort("ABC");
1451
    string slong("ABCD");
1452
    string sshortsmaller("ABB");
1453
    
1454
    vector<pair<string,string> > cmps;
1455
    cmps.push_back(pair<string,string>(sshort,sshort));
1456
    cmps.push_back(pair<string,string>(sshort,slong));
1457
    cmps.push_back(pair<string,string>(slong,sshort));
1458
    cmps.push_back(pair<string,string>(sshortsmaller,sshort));
1459
    cmps.push_back(pair<string,string>(sshort, sshortsmaller));
1460
1461
    for (vector<pair<string,string> >::const_iterator it = cmps.begin();
1462
         it != cmps.end(); it++) {
1463
        cout << it->first << " " << it->second << " " << 
1464
            stringicmp(it->first, it->second) << endl;
1465
    }
1466
    cout << endl;
1467
    for (vector<pair<string,string> >::const_iterator it = cmps.begin();
1468
         it != cmps.end(); it++) {
1469
        cout << it->first << " " << it->second << " " << 
1470
            stringlowercmp(stringtolower(it->first), it->second) << endl;
1471
    }
1472
    cout << endl;
1473
    for (vector<pair<string,string> >::const_iterator it = cmps.begin();
1474
         it != cmps.end(); it++) {
1475
        cout << it->first << " " << it->second << " " << 
1476
            stringuppercmp(it->first, it->second) << endl;
1477
    }
1478
1379
#endif
1479
#endif
1380
1381
}
1480
}
1382
1481
1383
#endif
1482
#endif