tools.cpp 12.7 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
/*
 * Copyright 2002-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      CC/LICENSE
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <errno.h>
#include <limits> 

#include "tools.h"
#include "db_pool.h"

int32_t ivr_tools_t::get_abs_timespec(struct timespec* ts, int32_t milliSeconds) {
    struct timeval tp;
    int32_t rv;

    if (NULL == ts) {
        return -1;
    }

    rv = gettimeofday(&tp, NULL);

    if (0 != rv) {
        return -1;
    }

    ts->tv_sec = tp.tv_sec;
    ts->tv_nsec = tp.tv_usec * 1000UL;

    ts->tv_sec += milliSeconds / 1000UL;
    ts->tv_nsec += milliSeconds % 1000UL * 1000000UL;

    ts->tv_sec += ts->tv_nsec / (1000UL * 1000UL * 1000UL);
    ts->tv_nsec %= (1000UL * 1000UL * 1000UL);

    return 0;
}

uint64_t ivr_tools_t::get_timestamp_ms() {
    timeval t;
    gettimeofday(&t, NULL);
    return t.tv_sec * 1000000 + t.tv_usec;
}

bool ivr_tools_t:: check_port(const char* src, uint32_t& result) {
    result = 0;

    if (src == NULL) {
        return false;
    }

    const char* s = src;

    while (*s == ' ') {
        ++s;
    }

    while (*s >= '0' && *s <= '9') {
        result = result * 10 + ((*s) - '0');

        if (result > 0xffff) {
            result = 0;
            return false;
        }

        ++s;
    }

    while (*s == ' ') {
        ++s;
    }

    if (*s != '\0') {
        return false;
    }

    return true;
}

bool ivr_tools_t::check_ip(const char* src, uint32_t& result) {
    result = 0;

    if (src == NULL) {
        return false;
    }

    if (strcmp(src, "localhost") == 0) {
        result = (127 << 24) | 0x1;
        return true;
    }

    const char* s = src;
    uint32_t count = 0;
    char subip[5];
    uint32_t idx = 0;

    while (*s == ' ') {
        ++s;
    }

    while (1) {
        if (*s == '.' || *s == '\0' || *s == ' ') {
            ++count;
            uint32_t tmp = 0;

            for (uint32_t i = 0; i < idx; ++i) {
                tmp = (subip[i] - '0') + tmp * 10;
            }

            idx = 0;

            if (tmp > 255) {
                return false;
            }

            result = ((result << 8) & 0xffffff00) | (tmp & 0xff);
        } else if (*s >= '0' && *s <= '9') {
            subip[idx] = *s;
            idx++;
        } else {
            return false;
        }

        if (*s == '\0' || *s == ' ' || count > 4) {
            break;
        }

        ++s;
    }

    while (*s == ' ') {
        ++s;
    }

    if (count == 4 && *s == '\0') {
        return true;
    } else {
        result = 0;
        return false;
    }
}

bool ivr_tools_t::is_positive_integer(std::string& str) {
    if (str.size() == 0 || str.at(0) == '0') {
        return false;
    }

    for (uint32_t i = 0; i < str.size(); i++) {
        if (!isdigit(str.at(i))) {
            return false;
        }
    }

    uint32_t len = str.length();

    if (len > 10) {
        return false;
    } else if (10 == len && str > "2147483647") {
        return false;
    }

    return true;

}

bool ivr_tools_t::is_nonnegative_integer(std::string& str) {
    return ("0" == str) || is_positive_integer(str);
}

bool ivr_tools_t::is_integer(std::string& str) {
    if ("0" == str) {
        return true;
    } else if (str.empty()) {
        return false;
    } else if ('-' == str.at(0)) {
        std::string str2 = str.substr(1);
        return is_positive_integer(str2);
    }

    return is_positive_integer(str);
}

bool ivr_tools_t::is_all_digit(std::string& str) {
    if (str.size() == 0) {
        return false;
    }

    for (uint32_t i = 0; i < str.size(); i++) {
        if (!isdigit(str.at(i))) {
            return false;
        }
    }

    return true;
}

bool ivr_tools_t::split_string(const std::string& src,
                               const std::string& separator, std::vector<std::string>& dest) {

    if (src.size() == 0) {
        return false;
    }

    size_t old_pos = 0;
    size_t new_pos = 0;
    std::string tmp_data;

    while (true) {
        new_pos = src.find(separator, old_pos);

        if (new_pos != std::string::npos) {
            tmp_data = src.substr(old_pos, new_pos - old_pos);
            dest.push_back(tmp_data);
            old_pos = new_pos + separator.size();
        } else if (old_pos <= src.size()) {
            tmp_data = src.substr(old_pos);
            dest.push_back(tmp_data);
            break;
        } else {
            break;
        }
    }

    return true;

}

std::string ivr_tools_t::get_current_second() {
    time_t tm = time(NULL);

    char ret[15] = {0};

    sprintf(ret, "%ld", tm);

    return ret;

}

std::string ivr_tools_t::format_datatime_str(std::string& second) {
    time_t rawtime = atol(second.c_str());
    struct tm timeinfo;
    char buffer[25] = {0};

    if (localtime_r(&rawtime, &timeinfo)) {
        strftime(buffer, 25, "%Y-%m-%d %H:%M:%S", &timeinfo);
    }

    return buffer;

}

bool ivr_tools_t::str2double(const std::string& str, double& value) {
    std::string str_tmp = str;
    trim(str_tmp);

    const char* s = str_tmp.c_str();
    char* endptr = NULL;

    value = strtod(s, &endptr);

    if ((value ==  std::numeric_limits<double>::infinity()) ||
            (value == -std::numeric_limits<double>::infinity())) {
        return false;
    }

    if (endptr == s) {
        return false;
    }

    while (isspace(*endptr)) {
        ++endptr;
    }

    return (*endptr == '\0');// && (errno == 0);
}

void ivr_tools_t::trim(std::string& str) {
    str.erase(str.find_last_not_of("\t ") + 1);
    str.erase(0, str.find_first_not_of("\t "));
}

void ivr_tools_t::idx2vec(const char* prefix,
                          const section_map_t& sec, std::vector<uint32_t>& vec) {
    if (sec.size() == 0) {
        return;
    }

    std::vector<std::string> key_vec;

    for (section_map_t::const_iterator it = sec.begin(); it != sec.end(); ++it) {
        key_vec.push_back(it->first);
    }

    idx2vec_imp(prefix, key_vec, vec);

}

void ivr_tools_t::idx2vec(const char* prefix,
                          const key_map_t& sec, std::vector<uint32_t>& vec) {
    if (sec.size() == 0) {
        return;
    }

    std::vector<std::string> key_vec;

    for (key_map_t::const_iterator it = sec.begin(); it != sec.end(); ++it) {
        key_vec.push_back(it->first);
    }

    idx2vec_imp(prefix, key_vec, vec);


}
void ivr_tools_t::idx2vec_imp(const char* prefix,
                              const std::vector<std::string>& sec, std::vector<uint32_t>& vec) {
    size_t len_prefix = 0;

    if (NULL == prefix || (len_prefix = strlen(prefix)) == 0) {
        return;
    }

    vec.clear();

    size_t len_str = 0;
    std::string idx_str;

    for (std::vector<std::string>::const_iterator it = sec.begin(); it != sec.end(); ++it) {
        len_str = it->size();

        if (len_prefix >= len_str) {
            continue;
        }

        if (strcasecmp(it->substr(0, len_prefix).c_str(), prefix) == 0) {
            idx_str = it->substr(len_prefix, len_str - len_prefix);

            if (is_positive_integer(idx_str)) {
                vec.push_back(atoi(idx_str.c_str()));
            }
        }
    }

    sort(vec.begin(), vec.end());
}

bool ivr_tools_t::str_eq(const std::string& str, const char* sz) {
    if (NULL == sz) {
        return false;
    }

    std::string clean_str = str;
    trim(clean_str);

    return (strcasecmp(str.c_str(), sz) == 0);

}

bool ivr_tools_t::check_and_create_dir(const char* dirname) {
    struct stat _stat;

    if (stat(dirname, &_stat) != 0 || !S_ISDIR(_stat.st_mode)) { //目录不存在
        ostringstream ostm;
        ostm << "mkdir -p " << dirname;
        return (system(ostm.str().c_str()) == 0);
    }

    return true;
}

std::string ivr_tools_t::delete_blank(const std::string& exp) {
    vector<string> vec;
    split_string(exp, " ", vec);

    ostringstream ostm;

    for (uint32_t i = 0; i < vec.size(); i++) {
        ostm << vec[i];
    }

    return ostm.str();
}

std::string ivr_tools_t::map2str(const std::map<std::string, std::string>& val) {
    ostringstream ostm;

    for (std::map<std::string, std::string>::const_iterator it = val.begin();
            it != val.end(); ++it) {
        ostm << it->first << ":" << it->second << ",";
    }

    return ostm.str();
}

void ivr_tools_t::build_vars(const std::map<std::string, variable_t>& temp,
                             std::map<std::string, variable_t>& out) {
    for (std::map<std::string, variable_t>::const_iterator it = temp.begin();
            it != temp.end(); ++it) {
        variable_t var = it->second;
        var.pvalue = NULL;

        if (INT32 == var.type || STRING == var.type) {
            var.pvalue = new std::string;

            *(std::string*)(var.pvalue) = var.initial;
        } else if (MAP == var.type) {
            var.pvalue = new key_map_t;
        }

        //resultset部分由node_db来设置

        out[var.name] = var;
    }
}

void ivr_tools_t::destroy_vars(std::map<std::string, variable_t>& vars) {
    for (std::map<std::string, variable_t>::iterator it = vars.begin();
            it != vars.end(); ++it) {

        if (INT32 == it->second.type || STRING == it->second.type) {
            delete(std::string*)(it->second.pvalue);
        } else if (MAP == it->second.type) {
            delete(key_map_t*)(it->second.pvalue);
        } else if (RESULTSET == it->second.type) {
            sql::ResultSet* result = (sql::ResultSet*)(it->second.pvalue);

            /*
            if(result&&!result->isClosed()){
                result->close();
            }*/
            //delete result->getStatement();
            if (result) {
                delete result->getStatement();
                delete result;
            }
        }

        it->second.pvalue = NULL;
    }

    vars.clear();
}

const char* ivr_tools_t::inet_ntop(uint32_t src, char* dest, uint32_t dest_len) {
    if (dest == NULL) {
        return NULL;
    }

    uint32_t idx = 0;

    for (int i = 3; i >= 0 && idx < dest_len; --i) {
        uint32_t tmp = (src >> (i * 8)) & 0xff;
        char subip[5];
        int j = -1;

        while (j < 5) {
            ++j;
            subip[j] = tmp % 10 + '0';
            tmp = tmp / 10;

            if (tmp == 0) {
                break;
            }
        }

        while (j >= 0 && idx < dest_len) {
            dest[idx] = subip[j];
            --j;
            ++idx;
        }

        if (idx >= dest_len) {
            dest[0] = '\0';
            break;
        } else if (i > 0) {
            dest[idx] = '.';
            ++idx;
        } else {
            dest[idx] = '\0';
            ++idx;
        }
    }

    return dest;
}

std::string ivr_tools_t::chlname2no(const std::string& src) {
    std::string ret;

    if (chlname2no(src, ret)) {
        return ret;
    } else {
        return src;
    }
}

bool ivr_tools_t::chlname2no(const std::string& src, std::string& dest) {

    int len = src.length();
    dest = "";

    for (int i = 0; i < len; ++i) {
        if (src[i] == '/') {
            dest = "";
        } else if (src[i] == '@') {
            return true;
        } else {
            dest += src[i];
        }
    }

    dest = "";
    return false;

}

void ivr_tools_t::safe_sleeps(uint32_t s) {
    struct timespec interval, remainder;
    interval.tv_sec = s;
    interval.tv_nsec = 0;
    int32_t rr = -1;

    if ((rr = nanosleep(&interval, &remainder)) == -1) {
        while (EINTR == errno && -1 == rr) {
            rr = nanosleep(&remainder, &remainder);
        }
    }
}

void ivr_tools_t::safe_sleepms(uint32_t ms) {
    struct timespec interval, remainder;
    interval.tv_sec = ms / 1000;
    interval.tv_nsec = (ms % 1000) * 1000000;
    int32_t rr = -1;

    if ((rr = nanosleep(&interval, &remainder)) == -1) {
        while (EINTR == errno && -1 == rr) {
            rr = nanosleep(&remainder, &remainder);
        }
    }
}

std::string ivr_tools_t::get_address(uint32_t ip) {
    std::ostringstream ss;
    ss << ((ip >> 24) & 0xff) << "." << ((ip >> 16) & 0xff) << "." << ((ip >> 8) & 0xff) << "." <<
       (ip & 0xff);
    return ss.str();
}

std::string ivr_tools_t::get_callsource(uint32_t ip, uint32_t port) {
    std::ostringstream ss;
    ss << "-h " << get_address(ip)
       << " -p " << port;
    return ss.str();
}

std::string ivr_tools_t::get_callsource(const std::string& ip, uint32_t port) {
    uint32_t uip = 0;

    if (!ivr_tools_t::check_ip(ip.c_str(), uip)) {
        return "";
    }

    return get_callsource(uip, port);
}

/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */