LCOV - code coverage report
Current view: top level - muse/framework/global/io - fileinfo.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 87.8 % 74 65
Test Date: 2026-11-05 04:07:02

            Line data    Source code
       1              : /*
       2              :  * SPDX-License-Identifier: GPL-3.0-only
       3              :  * MuseScore-CLA-applies
       4              :  *
       5              :  * MuseScore Studio
       6              :  * Music Composition & Notation
       7              :  *
       8              :  * Copyright (C) 2021 MuseScore Limited and others
       9              :  *
      10              :  * This program is free software: you can redistribute it and/or modify
      11              :  * it under the terms of the GNU General Public License version 3 as
      12              :  * published by the Free Software Foundation.
      13              :  *
      14              :  * This program is distributed in the hope that it will be useful,
      15              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17              :  * GNU General Public License for more details.
      18              :  *
      19              :  * You should have received a copy of the GNU General Public License
      20              :  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
      21              :  */
      22              : #include "fileinfo.h"
      23              : 
      24              : using namespace muse;
      25              : using namespace muse::io;
      26              : 
      27         5228 : FileInfo::FileInfo(const path_t& filePath)
      28         5228 :     : m_filePath(filePath.toString())
      29              : {
      30         5228 : }
      31              : 
      32            4 : String FileInfo::path() const
      33              : {
      34            4 :     size_t lastSep = m_filePath.lastIndexOf(u'/');
      35            4 :     if (lastSep == muse::nidx) {
      36              : #if defined(Q_OS_WIN)
      37              :         if (m_filePath.size() >= 2 && m_filePath.at(1) == u':') {
      38              :             return m_filePath.left(2);
      39              :         }
      40              : #endif
      41            1 :         return String(u'.');
      42              :     }
      43            3 :     if (lastSep == 0) {
      44            0 :         return String(u'/');
      45              :     }
      46              : #if defined(Q_OS_WIN)
      47              :     if (lastSep == 2 && m_filePath.at(1) == u':') {
      48              :         return m_filePath.left(lastSep + 1);
      49              :     }
      50              : #endif
      51            3 :     return m_filePath.left(lastSep);
      52              : }
      53              : 
      54            4 : String FileInfo::filePath() const
      55              : {
      56            4 :     return m_filePath;
      57              : }
      58              : 
      59           15 : String FileInfo::canonicalFilePath() const
      60              : {
      61           15 :     return fileSystem()->canonicalFilePath(m_filePath).toString();
      62              : }
      63              : 
      64            0 : String FileInfo::absolutePath() const
      65              : {
      66            0 :     return fileSystem()->absolutePath(m_filePath).toString();
      67              : }
      68              : 
      69            2 : String FileInfo::absoluteFilePath() const
      70              : {
      71            2 :     return fileSystem()->absoluteFilePath(m_filePath).toString();
      72              : }
      73              : 
      74         1216 : String FileInfo::fileName() const
      75              : {
      76         1216 :     size_t lastSep = m_filePath.lastIndexOf(u'/');
      77              : #if defined(Q_OS_WIN)
      78              :     if (lastSep == muse::nidx && m_filePath.size() >= 2 && m_filePath.at(1) == u':') {
      79              :         return m_filePath.mid(2);
      80              :     }
      81              : #endif
      82         1216 :     return m_filePath.mid(lastSep + 1);
      83              : }
      84              : 
      85           62 : String FileInfo::baseName() const
      86              : {
      87           62 :     size_t lastSep = m_filePath.lastIndexOf(u'/');
      88           62 :     size_t from = lastSep + 1;
      89           62 :     size_t firstDot = m_filePath.indexOf(u'.', from);
      90           62 :     size_t to = firstDot > 0 ? firstDot : m_filePath.size();
      91           62 :     size_t length = to - from;
      92              : 
      93              : #if defined(Q_OS_WIN)
      94              :     if (lastSep == muse::nidx && m_filePath.size() >= 2 && m_filePath.at(1) == u':') {
      95              :         return m_filePath.mid(2, length - 2);
      96              :     }
      97              : #endif
      98           62 :     return m_filePath.mid(from, length);
      99              : }
     100              : 
     101         1158 : String FileInfo::completeBaseName() const
     102              : {
     103         1158 :     size_t lastSep = m_filePath.lastIndexOf(u'/');
     104         1158 :     size_t from = lastSep + 1;
     105         1158 :     size_t lastDot = m_filePath.lastIndexOf(u'.');
     106         1158 :     size_t to = lastDot > 0 ? lastDot : m_filePath.size();
     107         1158 :     size_t length = to - from;
     108              : 
     109              : #if defined(Q_OS_WIN)
     110              :     if (lastSep == muse::nidx && m_filePath.size() >= 2 && m_filePath.at(1) == u':') {
     111              :         return m_filePath.mid(2, length - 2);
     112              :     }
     113              : #endif
     114         1158 :     return m_filePath.mid(from, length);
     115              : }
     116              : 
     117            5 : String FileInfo::suffix() const
     118              : {
     119            5 :     return doSuffix(m_filePath);
     120              : }
     121              : 
     122         4609 : String FileInfo::suffix(const path_t& filePath)
     123              : {
     124         4609 :     return doSuffix(filePath.toString());
     125              : }
     126              : 
     127         4614 : String FileInfo::doSuffix(const String& filePath)
     128              : {
     129         4614 :     size_t lastDot = filePath.lastIndexOf(u'.');
     130         4614 :     if (lastDot == muse::nidx) {
     131            2 :         return String();
     132              :     }
     133              : 
     134         4612 :     size_t lastSep = filePath.lastIndexOf(u'/');
     135         4612 :     if (lastSep == muse::nidx) {
     136          195 :         lastSep = 0;
     137              :     }
     138              : 
     139         4612 :     if (lastDot < lastSep) {
     140            1 :         return String();
     141              :     }
     142              : 
     143         4611 :     return filePath.mid(lastDot + 1);
     144              : }
     145              : 
     146            0 : DateTime FileInfo::birthTime() const
     147              : {
     148            0 :     return fileSystem()->birthTime(m_filePath);
     149              : }
     150              : 
     151           33 : DateTime FileInfo::lastModified() const
     152              : {
     153           33 :     return fileSystem()->lastModified(m_filePath);
     154              : }
     155              : 
     156            5 : EntryType FileInfo::entryType() const
     157              : {
     158            5 :     return fileSystem()->entryType(m_filePath);
     159              : }
     160              : 
     161            9 : bool FileInfo::isRelative() const
     162              : {
     163            9 :     return !isAbsolute();
     164              : }
     165              : 
     166         1516 : bool FileInfo::isAbsolute() const
     167              : {
     168              : #ifdef Q_OS_WIN
     169              :     return (m_filePath.size() >= 3
     170              :             && m_filePath.at(0).isLetter()
     171              :             && m_filePath.at(1) == u':'
     172              :             && m_filePath.at(2) == u'/')
     173              :            || (!m_filePath.isEmpty()
     174              :                && (m_filePath.at(0) == u'/'
     175              :                    || m_filePath.at(0) == u':'));
     176              : #else
     177         1516 :     return !m_filePath.empty() && (m_filePath.at(0) == u'/' || m_filePath.at(0) == u':');
     178              : #endif
     179              : }
     180              : 
     181            0 : bool FileInfo::isSymLink() const
     182              : {
     183            0 :     return fileSystem()->isSymLink(m_filePath);
     184              : }
     185              : 
     186            0 : path_t FileInfo::symLinkTarget() const
     187              : {
     188            0 :     return fileSystem()->symLinkTarget(m_filePath);
     189              : }
     190              : 
     191           24 : bool FileInfo::exists() const
     192              : {
     193           24 :     return fileSystem()->exists(m_filePath);
     194              : }
     195              : 
     196         5345 : bool FileInfo::exists(const path_t& filePath)
     197              : {
     198         5345 :     return fileSystem()->exists(filePath);
     199              : }
     200              : 
     201         1186 : path_t FileInfo::dirPath() const
     202              : {
     203         1186 :     size_t lastSep = m_filePath.lastIndexOf(u'/');
     204         1186 :     if (lastSep == muse::nidx) {
     205           25 :         return ".";
     206              :     }
     207         1161 :     return m_filePath.mid(0, lastSep);
     208              : }
     209              : 
     210         1182 : Dir FileInfo::dir() const
     211              : {
     212         1182 :     return Dir(dirPath());
     213              : }
        

Generated by: LCOV version 2.0-1