thirdparty/glslcc/src/glslcc.cpp
// if binary_size > 0, then we assume the data is binary
if binary_size > 0, then we assume the data is binary
if binary_size > 0 then we assume the data is binary
static bool write_file(const char* filepath, const char* data, const char* cvar, 
                       bool append = false, int binary_size = -1)
{
    sx_file_writer writer;
    if (!sx_file_open_writer(&writer, filepath, append ? SX_FILE_OPEN_APPEND : 0))
        return false;
    if (cvar && cvar[0]) {
        const int chars_per_line = 16;
        if (!append) {
            char header[512];
            sx_snprintf(header, sizeof(header),
                        "// This file is automatically created by glslcc v%d.%d.%d\n"
                        "// http://www.github.com/septag/glslcc\n"
                        "// \n"
                        "#pragma once\n\n", VERSION_MAJOR, VERSION_MINOR, VERSION_SUB);
            sx_file_write_text(&writer, header);
        }
        char var[128];
        int len;
        int char_offset = 0;
        char hex[32];
        if (binary_size > 0) 
            len = binary_size;
        else
            len = sx_strlen(data) + 1;   
        sx_snprintf(var, sizeof(var), "static const unsigned char %s[%d] = {\n\t", cvar, len);
        sx_file_write_text(&writer, var);
        for (int i = 0; i < len; i++) {
            if (i != len - 1) {
                sx_snprintf(hex, sizeof(hex), "0x%02x, ", (uint8_t)data[i]);
            } else {
                sx_snprintf(hex, sizeof(hex), "0x%02x };\n", (uint8_t)data[i]);
            }
            sx_file_write_text(&writer, hex);
            ++char_offset;
            if (char_offset == chars_per_line) {
                sx_file_write_text(&writer, "\n\t");
                char_offset = 0;
            }
        }
        sx_file_write_text(&writer, "\n");
    } else {
        if (binary_size > 0)
            sx_file_write(&writer, data, binary_size);
        else
            sx_file_write(&writer, data, sx_strlen(data));
    }
    sx_file_close_writer(&writer);
    return true;
}
static bool write_file ( const char * filepath , const char * data , const char * cvar , bool append = false , int binary_size = -1 ) { sx_file_writer writer ; if ( ! sx_file_open_writer ( & writer , filepath , append ? SX_FILE_OPEN_APPEND : 0 ) ) return false ; if ( cvar && cvar [ 0 ] ) { const int chars_per_line = 16 ; if ( ! append ) { char header [ 512 ] ; sx_snprintf ( header , sizeof ( header ) , " \n " " \n " " \n " " \n \n " , VERSION_MAJOR , VERSION_MINOR , VERSION_SUB ) ; sx_file_write_text ( & writer , header ) ; } char var [ 128 ] ; int len ; int char_offset = 0 ; char hex [ 32 ] ; if ( binary_size > 0 ) len = binary_size ; else len = sx_strlen ( data ) + 1 ; sx_snprintf ( var , sizeof ( var ) , " \n \t " , cvar , len ) ; sx_file_write_text ( & writer , var ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( i != len - 1 ) { sx_snprintf ( hex , sizeof ( hex ) , " " , ( uint8_t ) data [ i ] ) ; } else { sx_snprintf ( hex , sizeof ( hex ) , " \n " , ( uint8_t ) data [ i ] ) ; } sx_file_write_text ( & writer , hex ) ; ++ char_offset ; if ( char_offset == chars_per_line ) { sx_file_write_text ( & writer , " \n \t " ) ; char_offset = 0 ; } } sx_file_write_text ( & writer , " \n " ) ; } else { if ( binary_size > 0 ) sx_file_write ( & writer , data , binary_size ) ; else sx_file_write ( & writer , data , sx_strlen ( data ) ) ; } sx_file_close_writer ( & writer ) ; return true ; }
if binary_size > 0, then we assume the data is binary
if binary_size > 0 then we assume the data is binary
// .C file // file header // include the '\0' at the end to null-terminate the string