LCOV - code coverage report
Current view: top level - source/lib - status.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 6 33 18.2 %
Date: 2026-12-03 01:19:39 Functions: 2 5 40.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  *
       3             :  * Permission is hereby granted, free of charge, to any person obtaining
       4             :  * a copy of this software and associated documentation files (the
       5             :  * "Software"), to deal in the Software without restriction, including
       6             :  * without limitation the rights to use, copy, modify, merge, publish,
       7             :  * distribute, sublicense, and/or sell copies of the Software, and to
       8             :  * permit persons to whom the Software is furnished to do so, subject to
       9             :  * the following conditions:
      10             :  *
      11             :  * The above copyright notice and this permission notice shall be included
      12             :  * in all copies or substantial portions of the Software.
      13             :  *
      14             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
      15             :  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      16             :  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
      17             :  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
      18             :  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
      19             :  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      20             :  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      21             :  */
      22             : 
      23             : /*
      24             :  * error handling system: defines status codes, translates them to/from
      25             :  * other schemes (e.g. errno), associates them with descriptive text,
      26             :  * simplifies propagating errors / checking if functions failed.
      27             :  */
      28             : 
      29             : #include "precompiled.h"
      30             : 
      31             : #include "status.h"
      32             : 
      33             : #include "lib/secure_crt.h"
      34             : 
      35             : #include <cerrno>
      36             : 
      37             : static StatusDefinitionBucket* buckets;
      38             : 
      39           9 : StatusDefinitionBucket* StatusAddDefinitions(StatusDefinitionBucket* bucket)
      40             : {
      41             :     // insert at front of list
      42           9 :     StatusDefinitionBucket* next = buckets;
      43           9 :     buckets = bucket;
      44           9 :     return next;
      45             : }
      46             : 
      47             : 
      48           0 : static const StatusDefinition* DefinitionFromStatus(Status status)
      49             : {
      50           0 :     for(const StatusDefinitionBucket* bucket = buckets; bucket; bucket = bucket->next)
      51             :     {
      52           0 :         for(size_t i = 0; i < bucket->numDefinitions; i++)
      53             :         {
      54           0 :             if(bucket->definitions[i].status == status)
      55             :                 return &bucket->definitions[i];
      56             :         }
      57             :     }
      58             : 
      59             :     return 0;
      60             : }
      61             : 
      62             : 
      63           0 : static const StatusDefinition* DefinitionFromErrno(int errno_equivalent)
      64             : {
      65           0 :     for(const StatusDefinitionBucket* bucket = buckets; bucket; bucket = bucket->next)
      66             :     {
      67           0 :         for(size_t i = 0; i < bucket->numDefinitions; i++)
      68             :         {
      69           0 :             if(bucket->definitions[i].errno_equivalent == errno_equivalent)
      70             :                 return &bucket->definitions[i];
      71             :         }
      72             :     }
      73             : 
      74             :     return 0;
      75             : }
      76             : 
      77             : 
      78           0 : wchar_t* StatusDescription(Status status, wchar_t* buf, size_t max_chars)
      79             : {
      80           0 :     const StatusDefinition* def = DefinitionFromStatus(status);
      81           0 :     if(def)
      82             :     {
      83           0 :         wcscpy_s(buf, max_chars, def->description);
      84           0 :         return buf;
      85             :     }
      86             : 
      87           0 :     swprintf_s(buf, max_chars, L"Unknown error (%lld, 0x%llX)", (long long)status, (unsigned long long)status);
      88           0 :     return buf;
      89             : }
      90             : 
      91             : 
      92           0 : int ErrnoFromStatus(Status status)
      93             : {
      94           0 :     const StatusDefinition* def = DefinitionFromStatus(status);
      95           0 :     if(def && def->errno_equivalent != 0)
      96           0 :         return def->errno_equivalent;
      97             : 
      98             :     // the set of errnos in wposix.h doesn't have an "unknown error".
      99             :     // we use this one as a default because it's not expected to come up often.
     100             :     return EPERM;
     101             : }
     102             : 
     103         239 : Status StatusFromSystemError(std::error_code& ec)
     104             : {
     105         239 :     if (!ec)
     106             :         return INFO::OK;
     107           0 :     std::error_condition cond = ec.default_error_condition();
     108           0 :     if (cond.category() != std::generic_category())
     109             :         return ERR::FAIL;
     110           0 :     const StatusDefinition* def = DefinitionFromErrno(cond.value());
     111           0 :     return def ? def->status : ERR::FAIL;
     112             : }
     113             : 
     114           0 : Status StatusFromErrno()
     115             : {
     116           0 :     if(errno == 0)
     117             :         return INFO::OK;
     118           0 :     const StatusDefinition* def = DefinitionFromErrno(errno);
     119           0 :     return def? def->status : ERR::FAIL;
     120             : }
     121             : 
     122             : 
     123             : //-----------------------------------------------------------------------------
     124             : 
     125             : static const StatusDefinition statusDefs[] = {
     126             : 
     127             : // INFO::OK doesn't really need a string because calling StatusDescription(0)
     128             : // should never happen, but we'll play it safe.
     129             : { INFO::OK,             L"No error reported here" },
     130             : { ERR::FAIL,            L"Function failed (no details available)" },
     131             : 
     132             : { INFO::SKIPPED,        L"Skipped (not an error)" },
     133             : { INFO::CANNOT_HANDLE,  L"Cannot handle (not an error)" },
     134             : { INFO::ALL_COMPLETE,   L"All complete (not an error)" },
     135             : 
     136             : { ERR::LOGIC,           L"Logic error in code" },
     137             : { ERR::EXCEPTION,       L"Caught an exception" },
     138             : { ERR::TIMED_OUT,       L"Timed out" },
     139             : { ERR::REENTERED,       L"Single-call function was reentered" },
     140             : { ERR::CORRUPTED,       L"File/memory data is corrupted" },
     141             : { ERR::ABORTED,         L"Operation aborted" },
     142             : 
     143             : { ERR::INVALID_ALIGNMENT, L"Invalid alignment", EINVAL },
     144             : { ERR::INVALID_OFFSET,    L"Invalid offset",    EINVAL },
     145             : { ERR::INVALID_HANDLE,    L"Invalid handle",    EINVAL },
     146             : { ERR::INVALID_POINTER,   L"Invalid pointer",   EINVAL },
     147             : { ERR::INVALID_SIZE,      L"Invalid size",      EINVAL },
     148             : { ERR::INVALID_FLAG,      L"Invalid flag",      EINVAL },
     149             : { ERR::INVALID_PARAM,     L"Invalid parameter", EINVAL },
     150             : { ERR::INVALID_VERSION,   L"Invalid version",   EINVAL },
     151             : 
     152             : { ERR::AGAIN,           L"Try again later", EAGAIN },
     153             : { ERR::LIMIT,           L"Fixed limit exceeded", E2BIG },
     154             : { ERR::NOT_SUPPORTED,   L"Function not supported", ENOSYS },
     155             : { ERR::NO_MEM,          L"Not enough memory", ENOMEM},
     156             : 
     157             : { ERR::_1, L"Case 1" },
     158             : { ERR::_2, L"Case 2" },
     159             : { ERR::_3, L"Case 3" },
     160             : { ERR::_4, L"Case 4" },
     161             : { ERR::_5, L"Case 5" },
     162             : { ERR::_6, L"Case 6" },
     163             : { ERR::_7, L"Case 7" },
     164             : { ERR::_8, L"Case 8" },
     165             : { ERR::_9, L"Case 9" },
     166             : { ERR::_11, L"Case 11" },
     167             : { ERR::_12, L"Case 12" },
     168             : { ERR::_13, L"Case 13" },
     169             : { ERR::_14, L"Case 14" },
     170             : { ERR::_15, L"Case 15" },
     171             : { ERR::_16, L"Case 16" },
     172             : { ERR::_17, L"Case 17" },
     173             : { ERR::_18, L"Case 18" },
     174             : { ERR::_19, L"Case 19" },
     175             : { ERR::_21, L"Case 21" },
     176             : { ERR::_22, L"Case 22" },
     177             : { ERR::_23, L"Case 23" },
     178             : { ERR::_24, L"Case 24" },
     179             : { ERR::_25, L"Case 25" },
     180             : { ERR::_26, L"Case 26" },
     181             : { ERR::_27, L"Case 27" },
     182             : { ERR::_28, L"Case 28" },
     183             : { ERR::_29, L"Case 29" }
     184             : 
     185             : };
     186             : STATUS_ADD_DEFINITIONS(statusDefs);

Generated by: LCOV version 1.16