yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
1// test-process-main.cpp 2 3#include "../../source/core/slang-http.h" 4#include "../../source/core/slang-io.h" 5#include "../../source/core/slang-process-util.h" 6#include "../../source/core/slang-string-util.h" 7#include "../../source/core/slang-string.h" 8#include "../../source/core/slang-test-tool-util.h" 9#include "slang-com-helper.h" 10 11#include <stdio.h> 12#include <stdlib.h> 13#include <string.h> 14 15namespace TestProcess 16{ 17using namespace Slang ; 18 19static void _makeStdStreamsUnbuffered () 20{ 21// Make these streams unbuffered 22setvbuf (stdout ,nullptr ,_IONBF ,0 ); 23setvbuf (stderr ,nullptr ,_IONBF ,0 ); 24} 25 26static SlangResult _outputCount (int argc ,const char * const * argv ) 27{ 28if (argc < 3 ) 29 { 30return SLANG_FAIL ; 31 } 32// Get the count 33const Index count = stringToInt (argv [2 ]); 34 35// If we want to crash 36Index crashIndex = -1 ; 37if (argc > 3 ) 38 { 39// When we crash, we want to make sure everything is flushed... 40_makeStdStreamsUnbuffered (); 41crashIndex = stringToInt (argv [3 ]); 42 } 43 44FILE * fileOut = stdout ; 45 46StringBuilder buf ; 47for (Index i = 0 ;i < count ;++ i ) 48 { 49buf .clear (); 50buf <<i <<"\n" ; 51 52fwrite (buf .getBuffer (),1 ,buf .getLength (),fileOut ); 53 54if (i == crashIndex ) 55 { 56// Use to simulate a crash. 57SLANG_BREAKPOINT (0 ); 58throw ; 59 } 60 } 61 62// NOTE! There is no flush here, we want to see if everything works if we just stream out. 63return SLANG_OK ; 64} 65 66static SlangResult _outputReflect () 67{ 68// Read lines from std input. 69// When hit line with just 'end', terminate 70 71// Get in as Stream 72 73RefPtr < Stream > stdinStream ; 74Process ::getStdStream (StdStreamType ::In ,stdinStream ); 75 76FILE * fileOut = stdout ; 77 78List < Byte > buffer ; 79 80Index startIndex = 0 ; 81 82while (true) 83 { 84SLANG_RETURN_ON_FAIL (StreamUtil ::read (stdinStream ,0 ,buffer )); 85 86while (true) 87 { 88UnownedStringSlice slice ( 89 (const char * )buffer .begin ()+ startIndex , 90 (const char * )buffer .end ()); 91UnownedStringSlice line ; 92 93if (!StringUtil ::extractLine (slice ,line )|| slice .begin ()== nullptr ) 94 { 95break ; 96 } 97 98// Process the line 99if (line == UnownedStringSlice ::fromLiteral ("end" )) 100 { 101return SLANG_OK ; 102 } 103 104// Write the text to the output stream 105fwrite (line .begin (),1 ,line .getLength (),fileOut ); 106fputc ('\n' ,fileOut ); 107 108// Move the start index forward 109const Index newStartIndex = slice .begin () 110 ?Index (slice .begin ()- (const char * )buffer .getBuffer ()) 111 :buffer .getCount (); 112SLANG_ASSERT (newStartIndex > startIndex ); 113startIndex = newStartIndex ; 114 } 115 } 116} 117 118static SlangResult _httpReflect (int argc ,const char * const * argv ) 119{ 120SLANG_UNUSED (argc ); 121SLANG_UNUSED (argv ); 122 123RefPtr < Stream > stdinStream ,stdoutStream ; 124 125Process ::getStdStream (StdStreamType ::In ,stdinStream ); 126Process ::getStdStream (StdStreamType ::Out ,stdoutStream ); 127 128RefPtr < BufferedReadStream > readStream (new BufferedReadStream (stdinStream )); 129 130RefPtr < HTTPPacketConnection > connection = new HTTPPacketConnection (readStream ,stdoutStream ); 131 132while (connection -> isActive ()) 133 { 134// Block waiting for content (or error/closed) 135SLANG_RETURN_ON_FAIL (connection -> waitForResult ()); 136 137// If we have content do something with it 138if (connection -> hasContent ()) 139 { 140auto content = connection -> getContent (); 141 142// If it just holds 'end' then we are done 143const UnownedStringSlice slice ((const char * )content .begin (),content .getCount ()); 144 145if (slice == UnownedStringSlice ::fromLiteral ("end" )) 146 { 147break ; 148 } 149 150// Else reflect it back 151SLANG_RETURN_ON_FAIL (connection -> write (content .begin (),content .getCount ())); 152 153// Consume that content/packet 154connection -> consumeContent (); 155 } 156 } 157 158return SLANG_OK ; 159} 160 161static SlangResult execute (int argc ,const char * const * argv ) 162{ 163if (argc < 2 ) 164 { 165return SLANG_FAIL ; 166 } 167 168// Get the tool name 169const String toolName = argv [1 ]; 170if (toolName == "reflect" ) 171 { 172return _outputReflect (); 173 } 174else if (toolName == "count" ) 175 { 176return _outputCount (argc ,argv ); 177 } 178else if (toolName == "http-reflect" ) 179 { 180return _httpReflect (argc ,argv ); 181 } 182return SLANG_E_NOT_AVAILABLE ; 183} 184 185}// namespace TestProcess 186 187int main (int argc ,const char * const * argv ) 188{ 189SlangResult res = TestProcess ::execute (argc ,argv ); 190return (int )Slang ::TestToolUtil ::getReturnCode (res ); 191}