Changeset 05c0ac


Ignore:
Timestamp:
12/28/11 16:06:46 (17 months ago)
Author:
Micke Prag <micke.prag@…>
Branches:
('master', '033cf796174446f5fff5bbfad1cbf1e4af35c0d8')('controller-upgrade', '72b31cc86eeeef18f1371a3067b6e8a5ca21abfc')('windows_service_fixes', 'df6bd2788365991d36d5af2a75833b8de2a5860f')
Children:
89036564232ea8feec7404f23ea17c96831701ef
Parents:
98624032674044df121f5cf446fa6d9e1e287c1d
git-author:
Micke Prag <micke.prag@telldus.se>2011-12-28 16:06:46+01:00
git-committer:
Micke Prag <micke.prag@telldus.se>2011-12-28 16:06:46+01:00
Message:

Add functions formatf() and sformatf()

Location:
telldus-core/common
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • telldus-core/common/Strings.cpp

    r11dd17 r05c0ac  
    159159#endif 
    160160} 
     161 
     162std::string TelldusCore::formatf(const char *format, ...) { 
     163        va_list ap; 
     164        va_start(ap, format); 
     165        std::string retval = sformatf(format, ap); 
     166        va_end(ap); 
     167        return retval; 
     168} 
     169 
     170std::string TelldusCore::sformatf(const char *format, va_list ap) { 
     171        //This code is based on code from the Linux man-pages project (man vsprintf) 
     172        int n; 
     173        int size = 100;     /* Guess we need no more than 100 bytes. */ 
     174        char *p, *np; 
     175 
     176        if ((p = (char*)malloc(size)) == NULL) { 
     177                return ""; 
     178        } 
     179 
     180        while (1) { 
     181                /* Try to print in the allocated space. */ 
     182                n = vsnprintf(p, size, format, ap); 
     183 
     184                /* If that worked, return the string. */ 
     185                if (n > -1 && n < size) { 
     186                        std::string retval(p); 
     187                        free(p); 
     188                        return retval; 
     189                } 
     190 
     191                /* Else try again with more space. */ 
     192 
     193                if (n > -1) {   /* glibc 2.1 */ 
     194                        size = n+1; /* precisely what is needed */ 
     195                } else {        /* glibc 2.0 */ 
     196                        size *= 2;  /* twice the old size */ 
     197                } 
     198                if ((np = (char *)realloc (p, size)) == NULL) { 
     199                        free(p); 
     200            return ""; 
     201                } else { 
     202                        p = np; 
     203                } 
     204        } 
     205} 
  • telldus-core/common/Strings.h

    r11dd17 r05c0ac  
    1515 
    1616        int wideToInteger(const std::wstring &input); 
     17 
     18        std::string formatf(const char *format, ...); 
     19        std::string sformatf(const char *format, va_list ap); 
    1720} 
    1821 
Note: See TracChangeset for help on using the changeset viewer.