Changeset 1124 for cpp/common


Ignore:
Timestamp:
04/11/21 02:08:46 (4 years ago)
Author:
Maciej Komosinski
Message:

Added helper utility functions

Location:
cpp/common
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/util-file.cpp

    r888 r1124  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2021  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    8787        return writeCompleteFile(filename, s, warn_on_fail);
    8888}
     89
     90// Just like fgets(), but string length is unlimited and does not store trailing \r \n
     91string readUntilEOL(VirtFILE *f)
     92{
     93        char buf[100];
     94        char* line;
     95        std::string ret;
     96        bool endofline;
     97        while ((line = f->Vgets(buf, sizeof(buf))))
     98        {
     99                char* end = line + strlen(line);
     100                endofline = false;
     101                while (end > line)
     102                        if ((end[-1] == '\n') || (end[-1] == '\r'))
     103                        {
     104                                endofline = true;
     105                                end--;
     106                        }
     107                        else
     108                                break;
     109                ret += std::string(line, end - line);
     110                if (endofline) break;
     111        }
     112        return ret;
     113}
  • cpp/common/util-file.h

    r840 r1124  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2021  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    77
    88#include "nonstd_stl.h"
     9#include "virtfile/virtfile.h"
    910
    1011bool readCompleteFile(const char* filename, vector<char>& data, bool warn_on_missing_file = true);
     
    1213bool writeCompleteFile(const char* filename, const std::string& text, bool warn_on_fail = true);
    1314bool writeCompleteFile(const char* filename, vector<char>& data, bool warn_on_fail = true);
     15string readUntilEOL(VirtFILE *f);
    1416
    1517#endif
  • cpp/common/util-stl.h

    r888 r1124  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2021  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    77
    88#include "nonstd_stl.h"
     9#include <map>
    910
    1011template<typename T, std::size_t N> void push_back(vector<T>& v, T(&d)[N])
     
    3637}
    3738
     39template<typename Key,typename Value> Value mapValueOrDefault(const std::map<Key,Value> &map, const Key& key, const Value& default_value)
     40{
     41        auto found = map.find(key);
     42        if (found != map.end())
     43                return found->second;
     44        else
     45                return default_value;
     46}
     47
    3848template<class T> class DeletingVector  // deletes the elements (pointers) in destructor
    3949{
Note: See TracChangeset for help on using the changeset viewer.