aboutsummaryrefslogblamecommitdiff
path: root/src/util.cpp
blob: a0d50442245156ce37ed3700ed248bc961418fa8 (plain) (tree)




























                                                                                  
                
                     




                            







                                           




                                              



                                                                           
                                                         

                            

                     
                              
                                                             









                                                               
               










                                                












                                                              
                          
                 









                                                                         

 










                                                                                     

                      
                                       


               
                                          

                      
                                         





                                            
                                          




                                          
                                         


               
                     



















                                                              
                                                                       





                                                      


                                                                        
                          

 
                                                                                       
                                   
                                                

                                                            






                                                                    

 












                                                                     
                                                  





























                                                                            
                       









                                                                                
                               
                                                                          
            
                          
                                      
     


                                  
         


                                                         

         


                                   
                                                                                  








                                                           


                  
                                       


                                                            
                               
     

                                                             




                                                     


     





























                                                                                
 
/**
 * Copyright (c) 2018 Cornell University.
 *
 * Author: Ted Yin <tederminant@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * 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 <cstdarg>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <sys/time.h>

#include "salticidae/util.h"

namespace salticidae {

const char *TTY_COLOR_RED = "\x1b[31m";
const char *TTY_COLOR_GREEN = "\x1b[32m";
const char *TTY_COLOR_YELLOW = "\x1b[33m";
const char *TTY_COLOR_BLUE = "\x1b[34m";
const char *TTY_COLOR_MAGENTA = "\x1b[35m";
const char *TTY_COLOR_CYAN = "\x1b[36m";
const char *TTY_COLOR_RESET = "\x1b[0m";

void sec2tv(double t, struct timeval &tv) {
    tv.tv_sec = trunc(t);
    tv.tv_usec = trunc((t - tv.tv_sec) * 1e6);
}

double gen_rand_timeout(double base_timeout, double alpha) {
    return base_timeout + rand() / (double)RAND_MAX * alpha * base_timeout;
}

std::string vstringprintf(const char *fmt, va_list _ap) {
    int guessed_size = 1024;
    std::string buff;
    va_list ap;
    va_copy(ap, _ap);
    buff.resize(guessed_size);
    int nwrote = vsnprintf(&buff[0], guessed_size, fmt, _ap);
    if (nwrote < 0) buff = "";
    else
    {
        buff.resize(nwrote);
        if (nwrote > guessed_size)
        {
            if (vsnprintf(&buff[0], nwrote, fmt, ap) != nwrote)
                buff = "";
        }
    }
    va_end(ap);
    return std::move(buff);
}

std::string stringprintf(const char *fmt, ...) {
    va_list ap;
    va_start(ap, fmt);
    auto ret = vstringprintf(fmt, ap);
    va_end(ap);
    return std::move(ret);
}

const std::string get_current_datetime() {
    /* credit: http://stackoverflow.com/a/41381479/544806 */
    char fmt[64], buf[64];
    struct timeval tv;