Changeset 1331


Ignore:
Timestamp:
01/02/25 02:04:21 (3 days ago)
Author:
Maciej Komosinski
Message:
  • more input types in writeCompleteFile(), with "span" being the common interface
  • writeCompleteFile() no longer produces a false warning when writing an empty file
Location:
cpp/common
Files:
2 edited

Legend:

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

    r1153 r1331  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2021  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2024  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    1919#ifdef USE_VIRTFILE
    2020#ifdef DEBUGGING_READWRITECOMPLETEFILE
    21         logPrintf("","readCompleteFile",LOG_DEBUG,"virtfile: '%s'",filename);
     21        logPrintf("", "readCompleteFile", LOG_DEBUG, "virtfile: '%s'", filename);
    2222#endif 
    23 //      if (!isAbsolutePath(filename))
     23        //      if (!isAbsolutePath(filename))
    2424        {
    25                 VirtFILE *f=Vfopen(filename,FOPEN_READ_BINARY);
     25                VirtFILE *f = Vfopen(filename, FOPEN_READ_BINARY);
    2626                if (f)
    2727                {
    28                         int size=f->getSize();
     28                        int size = f->getSize();
    2929                        data.resize(size);
    3030                        int przeczytane = (int)f->Vread(&data[0], size, 1);
     
    3333                }
    3434        }
    35 //      else
     35        //      else
    3636#endif
    3737        {
    3838#ifdef DEBUGGING_READWRITECOMPLETEFILE
    39         if (isAbsolutePath(filename))
    40                 logPrintf("","readCompleteFile",LOG_DEBUG,"mfopen absolute path: '%s'",filename);
    41         else
    42                 logPrintf("","readCompleteFile",LOG_DEBUG,"mfopen: '%s' in current dir: '%s'",filename,getCurrentDirectory().c_str());
     39                if (isAbsolutePath(filename))
     40                        logPrintf("", "readCompleteFile", LOG_DEBUG, "mfopen absolute path: '%s'", filename);
     41                else
     42                        logPrintf("", "readCompleteFile", LOG_DEBUG, "mfopen: '%s' in current dir: '%s'", filename, getCurrentDirectory().c_str());
    4343#endif 
    4444                MFILE *f = mfopen(filename, FOPEN_READ_BINARY);
    4545#ifdef DEBUGGING_READWRITECOMPLETEFILE
    46         logPrintf("", "readCompleteFile", LOG_DEBUG, "mfopen status: %s", f?"ok":"fail");
     46                logPrintf("", "readCompleteFile", LOG_DEBUG, "mfopen status: %s", f ? "ok" : "fail");
    4747#endif
    4848                if (f)
     
    5858                logPrintf("", "readCompleteFile", LOG_WARN, "Couldn't open file '%s'", filename);
    5959#ifdef DEBUGGING_READWRITECOMPLETEFILE
    60         logPrintf("", "readCompleteFile", LOG_DEBUG, "bytes:%d status: %s", data.size(), ok?"ok":"fail");
     60        logPrintf("", "readCompleteFile", LOG_DEBUG, "bytes:%d status: %s", data.size(), ok ? "ok" : "fail");
    6161#endif
    6262        return ok;
     
    7474}
    7575
    76 bool writeCompleteFile(const char* filename, const string& text, bool warn_on_fail)
     76bool writeCompleteFile(const char* filename, const span<char>& data, bool warn_on_fail)
    7777{
    7878#ifdef USE_VIRTFILE
    7979#ifdef DEBUGGING_READWRITECOMPLETEFILE
    80         logPrintf("","writeCompleteFile",LOG_DEBUG,"virtfile: '%s'",filename);
     80        logPrintf("", "writeCompleteFile", LOG_DEBUG, "virtfile: '%s'", filename);
    8181#endif 
    8282        VirtFILE *f = Vfopen(filename, FOPEN_WRITE_BINARY);
     
    8484        if (f)
    8585        {
    86                 int zapisane = (int)f->Vwrite(text.c_str(), text.length(), 1);
     86                int zapisane = data.size() > 0 ? (int)f->Vwrite(data.begin(), data.size(), 1) : 1; //size()==0 is a special case (creating an empty file)
    8787                delete f;
    8888                ok &= (zapisane == 1);
     
    9191#ifdef DEBUGGING_READWRITECOMPLETEFILE
    9292        if (isAbsolutePath(filename))
    93                 logPrintf("","writeCompleteFile",LOG_DEBUG,"mfopen absolute path: '%s'",filename);
     93                logPrintf("", "writeCompleteFile", LOG_DEBUG, "mfopen absolute path: '%s'", filename);
    9494        else
    95                 logPrintf("","writeCompleteFile",LOG_DEBUG,"mfopen: '%s' in current dir: '%s'",filename,getCurrentDirectory().c_str());
     95                logPrintf("", "writeCompleteFile", LOG_DEBUG, "mfopen: '%s' in current dir: '%s'", filename, getCurrentDirectory().c_str());
    9696#endif
    9797        MFILE *f = mfopen(filename, FOPEN_WRITE_BINARY);
    9898        bool ok = f != NULL;
    9999#ifdef DEBUGGING_READWRITECOMPLETEFILE
    100         logPrintf("", "writeCompleteFile", LOG_DEBUG, "mfopen status: %s", ok?"ok":"fail");
     100        logPrintf("", "writeCompleteFile", LOG_DEBUG, "mfopen status: %s", ok ? "ok" : "fail");
    101101#endif
    102102        if (f)
    103103        {
    104                 int zapisane = (int)mfwrite(text.c_str(), text.length(), 1, f);
     104                int zapisane = data.size() > 0 ? (int)mfwrite(data.begin(), data.size(), 1, f) : 1; //size()==0 is a special case (creating an empty file)
    105105                mfclose(f);
    106106                ok &= (zapisane == 1);
     
    110110                logPrintf("", "writeCompleteFile", LOG_WARN, "Couldn't write file '%s'", filename);
    111111#ifdef DEBUGGING_READWRITECOMPLETEFILE
    112         logPrintf("", "writeCompleteFile", LOG_DEBUG, "status: %s", ok?"ok":"fail");
     112        logPrintf("", "writeCompleteFile", LOG_DEBUG, "status: %s", ok ? "ok" : "fail");
    113113#endif
    114114        return ok;
    115115}
    116116
    117 bool writeCompleteFile(const char* filename, vector<char>& data, bool warn_on_fail)
     117bool writeCompleteFile(const char* filename, const span<uint8_t>& data, bool warn_on_fail)
    118118{
    119         string s(&data[0], data.size());
    120         return writeCompleteFile(filename, s, warn_on_fail);
     119        return writeCompleteFile(filename, span<char>((char*)&data[0], data.size()), warn_on_fail);
    121120}
     121
     122bool writeCompleteFile(const char* filename, const vector<char>& data, bool warn_on_fail)
     123{
     124        return writeCompleteFile(filename, span<char>((char*)&data[0], data.size()), warn_on_fail);
     125}
     126
     127bool writeCompleteFile(const char* filename, const string& text, bool warn_on_fail)
     128{
     129        return writeCompleteFile(filename, span<char>((char*)text.c_str(), text.size()), warn_on_fail);
     130}
     131
    122132
    123133// Just like fgets(), but string length is unlimited and does not store trailing \r \n
     
    134144        while ((line =
    135145#ifdef USE_VIRTFILE
    136             f->Vgets(buf, sizeof(buf))
     146                f->Vgets(buf, sizeof(buf))
    137147#else
    138             fgets(buf, sizeof(buf), f)
     148                fgets(buf, sizeof(buf), f)
    139149#endif
    140             ))
     150                ))
    141151        {
    142152                char* end = line + strlen(line);
  • cpp/common/util-file.h

    r1153 r1331  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2021  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2024  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    1212#include <stdio.h>
    1313#endif
     14#include <common/nonstd_span.h>
    1415
    1516bool readCompleteFile(const char* filename, vector<char>& data, bool warn_on_missing_file = true);
    1617bool readCompleteFile(const char* filename, string& out, bool warn_on_missing_file = true);
    1718bool writeCompleteFile(const char* filename, const std::string& text, bool warn_on_fail = true);
    18 bool writeCompleteFile(const char* filename, vector<char>& data, bool warn_on_fail = true);
     19bool writeCompleteFile(const char* filename, const span<char>& data, bool warn_on_fail = true);
     20bool writeCompleteFile(const char* filename, const span<uint8_t>& data, bool warn_on_fail = true);
     21bool writeCompleteFile(const char* filename, const vector<char>& data, bool warn_on_fail);
    1922#ifdef USE_VIRTFILE
    2023string readUntilEOL(VirtFILE *f);
Note: See TracChangeset for help on using the changeset viewer.