Skip to content

Commit 9b580e5

Browse files
authored
Reduce dependency on std library (#4785)
1 parent 2f2ae8c commit 9b580e5

12 files changed

+63
-64
lines changed

source/core/slang-io.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,13 @@ namespace Slang
562562
#endif
563563
}
564564

565+
bool Path::createDirectoryRecursive(const String& path)
566+
{
567+
std::error_code ec;
568+
std::filesystem::create_directories(path.getBuffer(), ec);
569+
return !ec;
570+
}
571+
565572
/* static */SlangResult Path::getPathType(const String& path, SlangPathType* pathTypeOut)
566573
{
567574
#ifdef _WIN32
@@ -661,6 +668,13 @@ namespace Slang
661668
#endif
662669
}
663670

671+
String Path::getCurrentPath()
672+
{
673+
Slang::String path;
674+
getCanonical(".", path);
675+
return path;
676+
}
677+
664678
String Path::getRelativePath(String base, String path)
665679
{
666680
std::filesystem::path p1(base.getBuffer());

source/core/slang-io.h

+5
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ namespace Slang
145145
static void append(StringBuilder& ioBuilder, const UnownedStringSlice& path);
146146

147147
static bool createDirectory(const String& path);
148+
static bool createDirectoryRecursive(const String& path);
148149

149150
/// Accept either style of delimiter
150151
SLANG_FORCE_INLINE static bool isDelimiter(char c) { return c == '/' || c == '\\'; }
@@ -197,6 +198,10 @@ namespace Slang
197198
/// @return SLANG_OK on success
198199
static SlangResult getCanonical(const String& path, String& outCanonicalPath);
199200

201+
/// Returns the current working directory
202+
/// @return The path in platform native format. Returns empty string if failed.
203+
static String getCurrentPath();
204+
200205
/// Returns the executable path
201206
/// @return The path in platform native format. Returns empty string if failed.
202207
static String getExecutablePath();

source/slang-record-replay/record/output-stream.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@
33

44
namespace SlangRecord
55
{
6-
FileOutputStream::FileOutputStream(const std::string& filename, bool append)
6+
FileOutputStream::FileOutputStream(const Slang::String& fileName, bool append)
77
{
8-
Slang::String path(filename.c_str());
98
Slang::FileMode fileMode = append ? Slang::FileMode::Append : Slang::FileMode::Create;
109
Slang::FileAccess fileAccess = Slang::FileAccess::Write;
1110
Slang::FileShare fileShare = Slang::FileShare::None;
1211

13-
SlangResult res = m_fileStream.init(path, fileMode, fileAccess, fileShare);
12+
SlangResult res = m_fileStream.init(fileName, fileMode, fileAccess, fileShare);
1413

1514
if (res != SLANG_OK)
1615
{
17-
SlangRecord::slangRecordLog(SlangRecord::LogLevel::Error, "Failed to open file %s\n", filename.c_str());
16+
SlangRecord::slangRecordLog(SlangRecord::LogLevel::Error, "Failed to open file %s\n", fileName.getBuffer());
1817
std::abort();
1918
}
2019
}

source/slang-record-replay/record/output-stream.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef OUTPUT_STREAM_H
22
#define OUTPUT_STREAM_H
33

4-
#include <string>
4+
#include "../../core/slang-string.h"
55
#include "../../core/slang-stream.h"
66

77
namespace SlangRecord
@@ -17,7 +17,7 @@ namespace SlangRecord
1717
class FileOutputStream : public OutputStream
1818
{
1919
public:
20-
FileOutputStream(const std::string& filename, bool append = false);
20+
FileOutputStream(const Slang::String& fileName, bool append = false);
2121
virtual ~FileOutputStream() override;
2222
virtual void write(const void* data, size_t len) override;
2323
virtual void flush() override;

source/slang-record-replay/record/record-manager.cpp

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
#include <string>
31
#include <sstream>
42
#include <thread>
53
#include "../util/record-utility.h"
@@ -14,21 +12,18 @@ namespace SlangRecord
1412
std::stringstream ss;
1513
ss << "gs-"<< globalSessionHandle <<"-t-"<<std::this_thread::get_id() << ".cap";
1614

17-
m_recordFileDirectory = m_recordFileDirectory / "slang-record";
18-
19-
Slang::String recordFileDirectory {m_recordFileDirectory.string().c_str()};
20-
if (!Slang::File::exists(recordFileDirectory))
15+
m_recordFileDirectory = Slang::Path::combine(m_recordFileDirectory, "slang-record");
16+
if (!Slang::File::exists(m_recordFileDirectory))
2117
{
22-
std::error_code ec;
23-
if (!std::filesystem::create_directory(m_recordFileDirectory, ec))
18+
if (!Slang::Path::createDirectoryRecursive(m_recordFileDirectory))
2419
{
25-
slangRecordLog(LogLevel::Error, "Fail to create directory: %s, error (%d): %s\n",
26-
m_recordFileDirectory.string().c_str(), ec.value(), ec.message().c_str());
20+
slangRecordLog(LogLevel::Error, "Fail to create directory: %s\n",
21+
m_recordFileDirectory.getBuffer());
2722
}
2823
}
2924

30-
std::filesystem::path recordFilePath = m_recordFileDirectory / ss.str();
31-
m_fileStream = std::make_unique<FileOutputStream>(recordFilePath.string());
25+
Slang::String recordFilePath = Slang::Path::combine(m_recordFileDirectory, Slang::String(ss.str().c_str()));
26+
m_fileStream = std::make_unique<FileOutputStream>(recordFilePath);
3227
}
3328

3429
void RecordManager::clearWithHeader(const ApiCallId& callId, uint64_t handleId)

source/slang-record-replay/record/record-manager.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#ifndef RECORD_MANAGER_H
22
#define RECORD_MANAGER_H
33

4-
#include <filesystem>
54
#include "parameter-recorder.h"
65
#include "../util/record-format.h"
76

7+
#include "../../core/slang-string.h"
8+
#include "../../core/slang-io.h"
9+
810
namespace SlangRecord
911
{
1012
class RecordManager
@@ -20,15 +22,15 @@ namespace SlangRecord
2022
// the end of the record. It has to start with a FunctionTailer
2123
void apendOutput();
2224

23-
std::filesystem::path const& getRecordFileDirectory() const { return m_recordFileDirectory; }
25+
const Slang::String& getRecordFileDirectory() const { return m_recordFileDirectory; }
2426

2527
private:
2628
void clearWithHeader(const ApiCallId& callId, uint64_t handleId);
2729
void clearWithTailer();
2830

2931
MemoryStream m_memoryStream;
3032
std::unique_ptr<FileOutputStream> m_fileStream;
31-
std::filesystem::path m_recordFileDirectory = std::filesystem::current_path();
33+
Slang::String m_recordFileDirectory = Slang::Path::getCurrentPath();
3234
ParameterRecorder m_recorder;
3335
};
3436
} // namespace SlangRecord

source/slang-record-replay/record/slang-filesystem.cpp

+8-11
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,22 @@ namespace SlangRecord
5252
// know something wrong with the loadFile call if we can't find the file in the record directory.
5353
if ((res == SLANG_OK) && (*outBlob != nullptr) && ((*outBlob)->getBufferSize() != 0))
5454
{
55-
std::filesystem::path filePath = m_recordManager->getRecordFileDirectory();
56-
filePath = filePath / path;
57-
58-
if (!File::exists(filePath.parent_path().string().c_str()))
55+
Slang::String filePath = Slang::Path::combine(m_recordManager->getRecordFileDirectory(), path);
56+
Slang::String dirPath = Slang::Path::getParentDirectory(filePath);
57+
if (!File::exists(dirPath))
5958
{
6059
slangRecordLog(LogLevel::Debug, "Create directory: %s to save captured shader file: %s\n",
61-
filePath.parent_path().string().c_str(), filePath.filename().string().c_str());
60+
dirPath.getBuffer(), filePath.getBuffer());
6261

63-
std::error_code ec;
64-
// std::filesystem::create_directories can create the directory recursively.
65-
if (!std::filesystem::create_directories(filePath.parent_path(), ec))
62+
if (!Path::createDirectoryRecursive(dirPath))
6663
{
67-
slangRecordLog(LogLevel::Error, "Fail to create directory: %s, error (%d): %s\n",
68-
filePath.parent_path().string().c_str(), ec.value(), ec.message().c_str());
64+
slangRecordLog(LogLevel::Error, "Fail to create directory: %s\n",
65+
dirPath.getBuffer());
6966
return SLANG_FAIL;
7067
}
7168
}
7269

73-
FileOutputStream fileStream(filePath.string().c_str());
70+
FileOutputStream fileStream(filePath);
7471

7572
fileStream.write((*outBlob)->getBufferPointer(), (*outBlob)->getBufferSize());
7673
fileStream.flush();

source/slang-record-replay/replay/json-consumer.cpp

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#include <filesystem>
2-
31
#include "slang.h"
42
#include "json-consumer.h"
53
#include "../util/record-utility.h"
@@ -335,26 +333,22 @@ namespace SlangRecord
335333
}
336334

337335

338-
JsonConsumer::JsonConsumer(const std::string& filePath)
336+
JsonConsumer::JsonConsumer(const Slang::String& filePath)
339337
{
340-
std::filesystem::path jsonFileDir(filePath);
341-
jsonFileDir = std::filesystem::absolute(jsonFileDir);
342-
343-
if (!Slang::File::exists(jsonFileDir.parent_path().string().c_str()))
338+
if (!Slang::File::exists(Slang::Path::getParentDirectory(filePath)))
344339
{
345-
slangRecordLog(LogLevel::Error, "Directory for json file does not exist: %s\n", filePath.c_str());
340+
slangRecordLog(LogLevel::Error, "Directory for json file does not exist: %s\n", filePath.getBuffer());
346341
}
347342

348-
Slang::String path(filePath.c_str());
349343
Slang::FileMode fileMode = Slang::FileMode::Create;
350344
Slang::FileAccess fileAccess = Slang::FileAccess::Write;
351345
Slang::FileShare fileShare = Slang::FileShare::None;
352346

353-
SlangResult res = m_fileStream.init(path, fileMode, fileAccess, fileShare);
347+
SlangResult res = m_fileStream.init(filePath, fileMode, fileAccess, fileShare);
354348

355349
if (res != SLANG_OK)
356350
{
357-
slangRecordLog(LogLevel::Error, "Failed to open file %s\n", filePath.c_str());
351+
slangRecordLog(LogLevel::Error, "Failed to open file %s\n", filePath.getBuffer());
358352
}
359353

360354
m_isFileValid = true;

source/slang-record-replay/replay/json-consumer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ namespace SlangRecord
5656
class JsonConsumer : public IDecoderConsumer
5757
{
5858
public:
59-
JsonConsumer(const std::string& filePath);
59+
JsonConsumer(const Slang::String& filePath);
6060

6161
virtual void CreateGlobalSession(ObjectID outGlobalSessionId);
6262
virtual void IGlobalSession_createSession(ObjectID objectId, slang::SessionDesc const& desc, ObjectID outSessionId);

source/slang-record-replay/replay/recordFile-processor.h

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define FILE_PROCESSOR_H
33

44
#include <cstdlib>
5-
#include <string>
65
#include "../../core/slang-stream.h"
76
#include "../util/record-utility.h"
87
#include "slang-decoder.h"

source/slang-record-replay/util/record-utility.cpp

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

2-
#include <cstring>
3-
#include <string>
2+
#include <string.h>
43
#include <stdlib.h>
54
#include <stdarg.h>
65
#include <mutex>
76

87
#include "record-utility.h"
8+
#include "../../core/slang-string.h"
99
#include "../../core/slang-string-util.h"
1010

1111
constexpr const char* kRecordLayerEnvVar = "SLANG_RECORD_LAYER";
@@ -15,7 +15,7 @@ namespace SlangRecord
1515
{
1616
static thread_local unsigned int g_logLevel = LogLevel::Silent;
1717

18-
static bool getEnvironmentVariable(const char* name, std::string& out)
18+
static bool getEnvironmentVariable(const char* name, Slang::String& out)
1919
{
2020
#ifdef _WIN32
2121
char* envVar = nullptr;
@@ -31,12 +31,12 @@ namespace SlangRecord
3131
out = envVar;
3232
}
3333
#endif
34-
return out.empty() == false;
34+
return out.getLength() > 0;
3535
}
3636

3737
bool isRecordLayerEnabled()
3838
{
39-
std::string envVarStr;
39+
Slang::String envVarStr;
4040
if(getEnvironmentVariable(kRecordLayerEnvVar, envVarStr))
4141
{
4242
if (envVarStr == "1")
@@ -55,16 +55,12 @@ namespace SlangRecord
5555
return;
5656
}
5757

58-
std::string envVarStr;
58+
Slang::String envVarStr;
5959
if (getEnvironmentVariable(kRecordLayerLogLevel, envVarStr))
6060
{
61-
char* end = nullptr;
62-
unsigned int logLevel = std::strtol(envVarStr.c_str(), &end, 10);
63-
if (end && (*end == 0))
64-
{
65-
g_logLevel = std::min((unsigned int)(LogLevel::Verbose), logLevel);
66-
return;
67-
}
61+
unsigned int logLevel = Slang::stringToUInt(envVarStr);
62+
g_logLevel = std::min((unsigned int)(LogLevel::Verbose), logLevel);
63+
return;
6864
}
6965
}
7066

tools/slang-replay/main.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#include <stdio.h>
2-
#include <filesystem>
32

43
#include <replay/recordFile-processor.h>
54
#include <replay/json-consumer.h>
65
#include <replay/replay-consumer.h>
76
#include <replay/slang-decoder.h>
87

8+
#include "../../source/core/slang-io.h"
9+
910
struct Options
1011
{
1112
bool convertToJson {false};
@@ -77,11 +78,8 @@ int main(int argc, char *argv[])
7778

7879
SlangRecord::RecordFileProcessor recordFileProcessor(options.recordFileName);
7980

80-
81-
std::filesystem::path jsonPath = options.recordFileName.begin();
82-
jsonPath.replace_extension(".json");
83-
84-
SlangRecord::JsonConsumer jsonConsumer(jsonPath.string());
81+
Slang::String jsonPath = Slang::Path::replaceExt(options.recordFileName, "json");
82+
SlangRecord::JsonConsumer jsonConsumer(jsonPath);
8583
SlangRecord::ReplayConsumer replayConsumer;
8684

8785
SlangRecord::SlangDecoder decoder;

0 commit comments

Comments
 (0)