Changeset 257 for cpp/common


Ignore:
Timestamp:
12/03/14 18:52:05 (10 years ago)
Author:
Maciej Komosinski
Message:
  • added strmove(): strcpy() for overlapping strings
  • ExtObject? operator== can handle NULL arguments
  • source formatting and improved genetic operator messages
Location:
cpp/common
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/Convert.cpp

    r247 r257  
    9090#if defined LINUX // || android?
    9191        return *::localtime_r(&timep,&ret);
    92 #elif defined _WIN32 && !defined __BORLANDC__ // and not bada?
     92#elif defined _WIN32 && !defined __BORLANDC__
    9393        ::localtime_s(&ret, &timep);
    9494        return ret;
     
    115115#if defined LINUX // || android?
    116116        ret=::asctime_r(&tm,buf);
    117 #elif defined _WIN32 && !defined __BORLANDC__ // and not bada?
     117#elif defined _WIN32 && !defined __BORLANDC__
    118118        asctime_s(buf, sizeof(buf), &tm);
    119119        ret = buf;
  • cpp/common/stl-util.cpp

    r247 r257  
    1111#include <assert.h>
    1212#ifdef USE_VIRTFILE
    13  #include <frams/virtfile/virtfile.h>
     13#include <frams/virtfile/virtfile.h>
    1414#endif
    1515#ifdef __BORLANDC__
    16  #define va_copy(to,from) to=from //borland does not have va_copy() at all; va_list is just a pointer in borland
     16#define va_copy(to,from) to=from //borland does not have va_copy() at all; va_list is just a pointer in borland
    1717#endif
    1818
     
    2727        //almost like SString::sprintf, but there is no common code to share because SString can use its directWrite to avoid double allocating/copying
    2828#ifdef USE_VSCPRINTF
    29         va_copy(ap_copy,ap);
     29        va_copy(ap_copy, ap);
    3030        size = _vscprintf(format, ap_copy) + 1; //+1 for terminating null character
    3131        va_end(ap_copy);
     
    3636                buf = (char*)malloc(size);
    3737                assert(buf != NULL);
    38                 va_copy(ap_copy,ap);
     38                va_copy(ap_copy, ap);
    3939                int n = vsnprintf(buf, size, format, ap_copy);
    4040                va_end(ap_copy);
     
    5555}
    5656
     57char* strmove(char *a, char *b) //strcpy that works well for overlapping strings ("Source and destination overlap")
     58{
     59        if (a == NULL || b == NULL)
     60                return NULL;
     61        memmove(a, b, strlen(b) + 1);
     62        return a;
     63}
     64
    5765string ssprintf(const char* format, ...)
    5866{
     
    6674bool readCompleteFile(const char* filename, vector<char>& data, bool warn_on_missing_file)
    6775{
    68     bool ok=false;
     76        bool ok = false;
    6977#ifdef USE_VIRTFILE
    7078        if (!isAbsolutePath(filename))
    71                 {
     79        {
    7280                VirtFILE *f=Vfopen(filename,FOPEN_READ_BINARY);
    7381                if (f)
    74                         {
     82                {
    7583                        int size=f->getSize();
    7684                        data.resize(size);
     
    7886                        ok = przeczytane == 1;
    7987                        delete f;
    80                         }
    8188                }
     89        }
    8290        else
    8391#endif
    8492        {
    85         MFILE *f = mfopen(filename, FOPEN_READ_BINARY);
    86         if (f)
    87         {
    88                 int size=getFileSize(f);
    89                 data.resize(size);
    90                 int przeczytane = mfread(&data[0], size, 1, f);
    91                 mfclose(f);
    92                 ok = przeczytane == 1;
    93         }
     93                MFILE *f = mfopen(filename, FOPEN_READ_BINARY);
     94                if (f)
     95                {
     96                        int size = getFileSize(f);
     97                        data.resize(size);
     98                        int przeczytane = mfread(&data[0], size, 1, f);
     99                        mfclose(f);
     100                        ok = przeczytane == 1;
     101                }
    94102        }
    95103        if (warn_on_missing_file && !ok)
  • cpp/common/stl-util.h

    r246 r257  
    1111template<typename T, std::size_t N> void push_back(vector<T>& v, T(&d)[N])
    1212{
    13 for(unsigned int i=0;i<N;i++)
    14         v.push_back(d[i]);
     13        for (unsigned int i = 0; i < N; i++)
     14                v.push_back(d[i]);
    1515}
    1616
    1717template<typename T> void erase(vector<T>& v, const T& e)
    1818{
    19 typename vector<T>::iterator it=std::find(v.begin(),v.end(),e);
    20 if (it!=v.end())
    21         v.erase(it);
     19        typename vector<T>::iterator it = std::find(v.begin(), v.end(), e);
     20        if (it != v.end())
     21                v.erase(it);
    2222}
    2323
    2424template<typename T> void deleteVectorElements(vector<T*>& v)
    2525{
    26 for(typename vector<T*>::iterator it=v.begin();it!=v.end();it++)
    27         delete *it;
    28 v.clear();
     26        for (typename vector<T*>::iterator it = v.begin(); it != v.end(); it++)
     27                delete *it;
     28        v.clear();
    2929}
    3030
    3131template<typename T> int findIndex(vector<T>& v, const T& e)
    3232{
    33 typename vector<T>::iterator it=find(v.begin(),v.end(),e);
    34 if (it!=v.end())
    35         return &*it-&v.front();
    36 return -1;
     33        typename vector<T>::iterator it = find(v.begin(), v.end(), e);
     34        if (it != v.end())
     35                return &*it - &v.front();
     36        return -1;
    3737}
     38
     39
     40char* strmove(char *a, char *b); //strcpy that works well for overlapping strings ("Source and destination overlap")
    3841
    3942string ssprintf(const char* format, ...);
     
    4548
    4649
    47 bool readCompleteFile(const char* filename, vector<char>& data, bool warn_on_missing_file=true);
    48 bool readCompleteFile(const char* filename, string& out, bool warn_on_missing_file=true);
    49 bool writeCompleteFile(const char* filename, const std::string& text,bool warn_on_fail=true);
    50 bool writeCompleteFile(const char* filename, vector<char>& data, bool warn_on_fail=true);
     50bool readCompleteFile(const char* filename, vector<char>& data, bool warn_on_missing_file = true);
     51bool readCompleteFile(const char* filename, string& out, bool warn_on_missing_file = true);
     52bool writeCompleteFile(const char* filename, const std::string& text, bool warn_on_fail = true);
     53bool writeCompleteFile(const char* filename, vector<char>& data, bool warn_on_fail = true);
    5154
    5255template<class T> class DeletingVector  // deletes the elements (pointers) in destructor
    5356{
    54   public:
    55 std::vector<T*> vector;
    56 ~DeletingVector()
     57public:
     58        std::vector<T*> vector;
     59        ~DeletingVector()
    5760        {
    58         for(int i=vector.size()-1;i>=0;i--)
    59                 delete vector[i];
     61                for (int i = vector.size() - 1; i >= 0; i--)
     62                        delete vector[i];
    6063        }
    61 T* operator[](int i) {return vector[i];}
    62 int size() {return vector.size();}
    63 void push_back(T* x) {vector.push_back(x);}
     64        T* operator[](int i) { return vector[i]; }
     65        int size() { return vector.size(); }
     66        void push_back(T* x) { vector.push_back(x); }
    6467};
    6568
Note: See TracChangeset for help on using the changeset viewer.