LCOV - code coverage report
Current view: top level - extra/webserver/webserver/utils - SPWebVirtualFile.cc (source / functions) Hit Total Coverage
Test: coverage.info Lines: 16 23 69.6 %
Date: 2024-05-12 00:16:13 Functions: 5 6 83.3 %

          Line data    Source code
       1             : /**
       2             :  Copyright (c) 2024 Stappler LLC <admin@stappler.dev>
       3             : 
       4             :  Permission is hereby granted, free of charge, to any person obtaining a copy
       5             :  of this software and associated documentation files (the "Software"), to deal
       6             :  in the Software without restriction, including without limitation the rights
       7             :  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
       8             :  copies of the Software, and to permit persons to whom the Software is
       9             :  furnished to do so, subject to the following conditions:
      10             : 
      11             :  The above copyright notice and this permission notice shall be included in
      12             :  all copies or substantial portions of the Software.
      13             : 
      14             :  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      15             :  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      16             :  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      17             :  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      18             :  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      19             :  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
      20             :  THE SOFTWARE.
      21             :  **/
      22             : 
      23             : #include "SPWebVirtualFile.h"
      24             : 
      25             : namespace STAPPLER_VERSIONIZED stappler::web {
      26             : 
      27             : struct VirtualFilesystemHandle {
      28         425 :         static VirtualFilesystemHandle *get() {
      29         425 :                 static VirtualFilesystemHandle s_handle;
      30         425 :                 return &s_handle;
      31             :         }
      32             : 
      33          25 :         VirtualFilesystemHandle() : count(0) { }
      34             : 
      35         325 :         VirtualFile add(StringView n, const StringView &c) {
      36         325 :                 if (n.starts_with("serenity/virtual")) {
      37           0 :                         n += "serenity/virtual"_len;
      38             :                 }
      39         325 :                 if (count < 255) {
      40         325 :                         table[count].name = n;
      41         325 :                         table[count].content = c;
      42         325 :                         ++ count;
      43             :                 }
      44         325 :                 return table[count - 1];
      45             :         }
      46             : 
      47             :         size_t count = 0;
      48             :         VirtualFile table[255] = { };
      49             : };
      50             : 
      51             : 
      52         325 : VirtualFile VirtualFile::add(const StringView &n, const StringView &c) {
      53         325 :         return VirtualFilesystemHandle::get()->add(n, c);
      54             : }
      55             : 
      56           0 : StringView VirtualFile::get(const StringView &path) {
      57           0 :         auto ptr = VirtualFilesystemHandle::get();
      58           0 :         for (size_t i = 0; i < ptr->count; ++i) {
      59           0 :                 if (path == ptr->table[i].name) {
      60           0 :                         return StringView(ptr->table[i].content);
      61             :                         break;
      62             :                 }
      63             :         }
      64           0 :         return StringView();
      65             : }
      66             : 
      67         100 : SpanView<VirtualFile> VirtualFile::getList() {
      68         100 :         auto ptr = VirtualFilesystemHandle::get();
      69         100 :         return SpanView<VirtualFile>(ptr->table, ptr->count);
      70             : }
      71             : 
      72             : }

Generated by: LCOV version 1.14