yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
eaa8dcfcc
master
1#include "file-util.h" 2 3#include "core/slang-io.h" 4 5namespace CppParse 6{ 7using namespace Slang ; 8 9namespace 10{// anonymous 11struct DiagnosticReporter 12{ 13SlangResult report (SlangResult res ) 14 { 15if (SLANG_FAILED (res )) 16 { 17if (m_sink ) 18 { 19if (res == SLANG_E_CANNOT_OPEN ) 20 { 21m_sink -> diagnose (SourceLoc (),CPPDiagnostics ::cannotOpenFile ,m_filename ); 22 } 23else 24 { 25m_sink -> diagnose (SourceLoc (),CPPDiagnostics ::errorAccessingFile ,m_filename ); 26 } 27 } 28 } 29return res ; 30 } 31 32DiagnosticReporter (const String & filename ,DiagnosticSink * sink ) 33 :m_filename (filename ),m_sink (sink ) 34 { 35 } 36 37DiagnosticSink * m_sink ; 38String m_filename ; 39}; 40 41}// namespace 42 43/* static */ SlangResult FileUtil ::readAllText ( 44const Slang ::String & fileName , 45DiagnosticSink * sink , 46String & outRead ) 47{ 48DiagnosticReporter reporter (fileName ,sink ); 49 50RefPtr < FileStream > stream = new FileStream ; 51SLANG_RETURN_ON_FAIL (reporter .report ( 52stream -> init (fileName ,FileMode ::Open ,FileAccess ::Read ,FileShare ::ReadWrite ))); 53 54StreamReader reader ; 55SLANG_RETURN_ON_FAIL (reporter .report (reader .init (stream ))); 56SLANG_RETURN_ON_FAIL (reporter .report (reader .readToEnd (outRead ))); 57 58return SLANG_OK ; 59} 60 61/* static */ SlangResult FileUtil ::writeAllText ( 62const Slang ::String & fileName , 63DiagnosticSink * sink , 64const UnownedStringSlice & text ) 65{ 66// TODO(JS): 67// There is an optimization/behavior here,that checks if the contents has changed. It only 68// writes if it hasn't That might not be what you want (both because of extra work of read, the 69// file modified stamp or other reasons, file is write only etc) NOTE! That this also does the 70// work of the comparison after it is decoded, but the *bytes* might actually be different. 71 72if (File ::exists (fileName )) 73 { 74String existingText ; 75if (SLANG_SUCCEEDED (readAllText (fileName ,nullptr ,existingText ))) 76 { 77if (existingText == text ) 78return SLANG_OK ; 79 } 80 } 81 82DiagnosticReporter reporter (fileName ,sink ); 83 84RefPtr < FileStream > stream = new FileStream ; 85SLANG_RETURN_ON_FAIL (reporter .report (stream -> init (fileName ,FileMode ::Create ))); 86 87StreamWriter writer ; 88SLANG_RETURN_ON_FAIL (reporter .report (writer .init (stream ))); 89SLANG_RETURN_ON_FAIL (reporter .report (writer .write (text ))) 90return SLANG_OK ; 91} 92 93/* static */ void FileUtil ::indent (Index indentCount ,StringBuilder & out ) 94{ 95for (Index i = 0 ;i < indentCount ;++ i ) 96 { 97out <<CPP_EXTRACT_INDENT_STRING ; 98 } 99} 100 101}// namespace CppParse