Ignore:
Timestamp:
12/08/18 05:31:09 (5 years ago)
Author:
Maciej Komosinski
Message:

Split stl-util into util-stl, util-file and util-string

File:
1 moved

Legend:

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

    r839 r840  
    33// See LICENSE.txt for details.
    44
    5 #include "stl-util.h"
     5#include "util-file.h"
    66#include <stdarg.h>
    7 #include <stdlib.h>
    87#include "nonstd_stdio.h"
    9 #include "Convert.h"
    108#include "nonstd.h"
    11 #include "log.h"
    129#include <assert.h>
    1310#ifdef USE_VIRTFILE
    1411#include <common/virtfile/virtfile.h>
    1512#endif
    16 
    1713
    1814string ssprintf_va(const char* format, va_list ap)
     
    7167}
    7268
    73 bool readCompleteFile(const char* filename, vector<char>& data, bool warn_on_missing_file)
    74 {
    75         bool ok = false;
    76 #ifdef USE_VIRTFILE
    77 //      if (!isAbsolutePath(filename))
    78         {
    79                 VirtFILE *f=Vfopen(filename,FOPEN_READ_BINARY);
    80                 if (f)
    81                 {
    82                         int size=f->getSize();
    83                         data.resize(size);
    84                         int przeczytane = f->Vread(&data[0], size, 1);
    85                         ok = przeczytane == 1;
    86                         delete f;
    87                 }
    88         }
    89 //      else
    90 #endif
    91         {
    92                 MFILE *f = mfopen(filename, FOPEN_READ_BINARY);
    93                 if (f)
    94                 {
    95                         int size = getFileSize(f);
    96                         data.resize(size);
    97                         int przeczytane = mfread(&data[0], size, 1, f);
    98                         mfclose(f);
    99                         ok = przeczytane == 1;
    100                 }
    101         }
    102         if (warn_on_missing_file && !ok)
    103                 logPrintf("stl-util", "readCompleteFile", LOG_WARN, "Couldn't open file '%s'", filename);
    104         return ok;
    105 }
    106 
    107 bool readCompleteFile(const char* filename, string& out, bool warn_on_missing_file)
    108 {
    109         vector<char> data;
    110         if (readCompleteFile(filename, data, warn_on_missing_file))
    111         {
    112                 out = string(&data[0], data.size());
    113                 return true;
    114         }
    115         return false;
    116 }
    117 
    118 bool writeCompleteFile(const char* filename, const string& text, bool warn_on_fail)
    119 {
    120 #ifdef USE_VIRTFILE
    121         VirtFILE *f = Vfopen(filename, FOPEN_WRITE_BINARY);
    122         bool ok = f != NULL;
    123         if (f)
    124         {
    125                 int zapisane = f->Vwrite(text.c_str(), text.length(), 1);
    126                 delete f;
    127                 ok &= zapisane == 1;
    128         }
    129 #else
    130         MFILE *f = mfopen(filename, FOPEN_WRITE_BINARY);
    131         bool ok = f != NULL;
    132         if (f)
    133         {
    134                 int zapisane = mfwrite(text.c_str(), text.length(), 1, f);
    135                 mfclose(f);
    136                 ok &= zapisane == 1;
    137         }
    138 #endif
    139         if (warn_on_fail && !ok)
    140                 logPrintf("stl-util", "writeCompleteFile", LOG_WARN, "Couldn't write file '%s'", filename);
    141         return ok;
    142 }
    143 
    144 bool writeCompleteFile(const char* filename, vector<char>& data, bool warn_on_fail)
    145 {
    146         string s(&data[0], data.size());
    147         return writeCompleteFile(filename, s, warn_on_fail);
    148 }
    149 
    150 
    151 
    15269string stripExt(const string& filename)
    15370{
     
    183100        return filename.substr(0, slash);
    184101}
    185 
Note: See TracChangeset for help on using the changeset viewer.