Пример #1
0
void JSONValue::WriteIndent(String& dest, int indent)
{
    uint oldLength = dest.Length();
    dest.Resize(oldLength + indent);
    for (int i = 0; i < indent; ++i)
        dest[oldLength + i] = ' ';
}
Пример #2
0
String Deserializer::ReadFileID()
{
    String ret;
    ret.Resize(4);
    Read(&ret[0], 4);
    return ret;
}
Пример #3
0
void MessagePackReader::Read( String& value )
{
	uint32_t length = ReadRawLength();
	value.Resize( length + 1 );
	ReadRaw( &value.GetFirst(), length );
	value += '\0';
}
Пример #4
0
// Joins the path of an RML or RCSS file with the path of a resource specified within the file.
void SystemInterface::JoinPath(String& translated_path, const String& document_path, const String& path)
{
	// If the path is absolute, strip the leading / and return it.
	if (path.Substring(0, 1) == "/")
	{
		translated_path = path.Substring(1);
		return;
	}

	// If the path is a Windows-style absolute path, return it directly.
	size_t drive_pos = path.Find(":");
	size_t slash_pos = Math::Min(path.Find("/"), path.Find("\\"));
	if (drive_pos != String::npos &&
		drive_pos < slash_pos)
	{
		translated_path = path;
		return;
	}

	// Strip off the referencing document name.
	translated_path = document_path;
	translated_path = translated_path.Replace("\\", "/");
	size_t file_start = translated_path.RFind("/");
	if (file_start != String::npos)
		translated_path.Resize(file_start + 1);
	else
		translated_path.Clear();

	// Append the paths and send through URL to removing any '..'.
	URL url(translated_path.Replace(":", "|") + path.Replace("\\", "/"));
	translated_path = url.GetPathedFileName().Replace("|", ":");
}
Пример #5
0
String RemoveTrailingSlash(const String& pathName)
{
    String ret = pathName.Trimmed();
    ret.Replace('\\', '/');
    if (!ret.Empty() && ret.Back() == '/')
        ret.Resize(ret.Length() - 1);
    return ret;
}
Пример #6
0
String ReadUtf8String(kNet::DataDeserializer &dd)
{
    String ret;
    ret.Resize(dd.Read<u16>());
    if (ret.Length())
        dd.ReadArray<u8>((u8*)&ret[0], ret.Length());
    return ret;
}
Пример #7
0
String String::operator + (const String& rhs) const
{
    String ret;
    ret.Resize(Length() + rhs.Length());
    CopyChars(ret.Buffer(), Buffer(), Length());
    CopyChars(ret.Buffer() + Length(), rhs.Buffer(), rhs.Length());
    
    return ret;
}
Пример #8
0
static int Deserializer_Read(duk_context* ctx)
{
    duk_int_t magic = duk_get_current_magic(ctx);

    duk_push_this(ctx);

    // safe cast based on type check above
    Deserializer* deserial = CastToDeserializer(ctx, duk_get_top_index(ctx));

    duk_pop(ctx);

    if (!deserial)
    {
        duk_push_boolean(ctx, 0);
        return 1;
    }

    char* data;
    String str;
    size_t length;

    IO_MAGIC_TYPE v = (IO_MAGIC_TYPE) magic;

    bool success = false;

    switch(v)
    {
        case IO_MAGIC_INT:
            duk_push_number(ctx, (double) deserial->ReadInt());
            return 1;
        case IO_MAGIC_STRING:
            length = deserial->GetSize() - deserial->GetPosition();
            str.Resize(length + 1);
            deserial->Read(&str[0], length);
            str[length] = '\0';
            duk_push_string(ctx, str.CString());
            return 1;
        case IO_MAGIC_ZEROSTRING:
            success = duk_push_string(ctx, deserial->ReadString().CString());
            return 1;
        case IO_MAGIC_BINARY:
            length = deserial->GetSize() - deserial->GetPosition();
            duk_push_fixed_buffer(ctx, length);
            duk_push_buffer_object(ctx, -1, 0, length, DUK_BUFOBJ_UINT8ARRAY);
            duk_replace(ctx, -2);
            data = (char*) duk_require_buffer_data(ctx, 0, &length);
            success = deserial->Read(data, length);
            return 1;
        default:
            break;
    }

    duk_push_undefined(ctx);

    return 1;

}
Пример #9
0
String String::operator + (const char* rhs) const
{
    size_t rhsLength = CStringLength(rhs);
    String ret;
    ret.Resize(Length() + rhsLength);
    CopyChars(ret.Buffer(), Buffer(), Length());
    CopyChars(ret.Buffer() + Length(), rhs, rhsLength);
    
    return ret;
}
Пример #10
0
void File::ReadText(String& text)
{
    text.Clear();

    if (!size_)
        return;

    text.Resize(size_);

    Read((void*)text.CString(), size_);
}
Пример #11
0
String GetCurrentDirectory() {
  String result;
  size_t capacity = 128;
  for (;;) {
    result.Resize(capacity);
    if (getcwd(&result[0], capacity))
      break;
    capacity *= 2;
  }
  return result;
}
Пример #12
0
void gStripWhitespace(String& ioString)
{
	size64 len = ioString.GetLength();
	size64 out_idx = 0;
	for (uint idx = 0; idx < len; idx++)
	{
		if (!sIsWhiteSpace(ioString[idx])) 
			ioString[out_idx++] = ioString[idx];
	}
	ioString.Resize(out_idx);
}
Пример #13
0
String String::Substring(unsigned pos) const
{
    if (pos < length_)
    {
        String ret;
        ret.Resize(length_ - pos);
        CopyChars(ret.buffer_, buffer_ + pos, ret.length_);

        return ret;
    }
    else
        return String();
}
Пример #14
0
String String::Substring(size_t pos) const
{
    if (pos < Length())
    {
        String ret;
        ret.Resize(Length() - pos);
        CopyChars(ret.Buffer(), Buffer() + pos, ret.Length());
        
        return ret;
    }
    else
        return String();
}
Пример #15
0
String Scene::GetVarNamesAttr() const
{
    String ret;

    if (!varNames_.Empty())
    {
        for (HashMap<ShortStringHash, String>::ConstIterator i = varNames_.Begin(); i != varNames_.End(); ++i)
            ret += i->second_ + ';';

        ret.Resize(ret.Length() - 1);
    }

    return ret;
}
Пример #16
0
//----------------------------------------------------------------------------------------------------------------------------------------------------
String EditorControl::GetTextRange( LONG Begin, LONG End )
{
  TextRange Range;
  String Text;
  SIZE_T Length = End-Begin;
  Text.Resize(Length);
  Range.lpstrText = Text.Str();
  Range.chrg.cpMin = Begin;
  Range.chrg.cpMax = End;

  EditFunc( SCI_GETTEXTRANGE, 0, (WPARAM)&Range );

  return Text;
}
Пример #17
0
tistream& SimpleSortedSetData< KeyT, CompareKeyT, AllocatorT >::operator<<( tistream& stream )
{
    m_Data->Clear();

    String str;
    std::streamsize size = stream.rdbuf()->in_avail();
    str.Reserve( static_cast< size_t >( size ) );
    str.Resize( static_cast< size_t >( size ) );
    stream.read( &str[ 0 ], size );

    Tokenize< KeyT, CompareKeyT, AllocatorT >( str, *m_Data, s_ContainerItemDelimiter );

    return stream;
}  
Пример #18
0
static int Deserializer_Read(duk_context* ctx)
{
    duk_int_t magic = duk_get_current_magic(ctx);

    duk_push_this(ctx);

    // safe cast based on type check above
    Deserializer* deserial = CastToDeserializer(ctx, duk_get_top_index(ctx));

    duk_pop(ctx);

    if (!deserial)
    {
        duk_push_boolean(ctx, 0);
        return 1;
    }

    PODVector<unsigned char> buffer;
    String str;
    size_t length;

    IO_MAGIC_TYPE v = (IO_MAGIC_TYPE) magic;

    bool success = false;

    switch(v)
    {
        case IO_MAGIC_INT:
            duk_push_number(ctx, (double) deserial->ReadInt());
            return 1;
        case IO_MAGIC_STRING:
            length = deserial->GetSize() - deserial->GetPosition();
            str.Resize(length + 1);
            deserial->Read(&str[0], length);
            str[length] = '\0';
            duk_push_string(ctx, str.CString());
            return 1;
        case IO_MAGIC_ZEROSTRING:
            success = duk_push_string(ctx, deserial->ReadString().CString());
            return 1;
        default:
            break;
    }

    duk_push_undefined(ctx);

    return 1;

}
Пример #19
0
String String::Substring(size_t pos, size_t numChars) const
{
    if (pos < Length())
    {
        String ret;
        if (pos + numChars > Length())
            numChars = Length() - pos;
        ret.Resize(numChars);
        CopyChars(ret.Buffer(), Buffer() + pos, ret.Length());
        
        return ret;
    }
    else
        return String();
}
Пример #20
0
void StreamReader::ReadString(String *result, const String &delimiter) {
    Array<Char> retString;
    Array<Char> delString;
    String strDelimiter = delimiter;
    Char c;
    if (strDelimiter.IsEmpty()) {
        strDelimiter.Resize(1);
        strDelimiter.Append("\0");
    }
    while (true) {
        c = ReadByte();
        if (this->GetError())
            break;
        if (c == strDelimiter.GetData()[0]) {
            delString.Clear();
            delString.Append(c);
            for (Int i = 1; i < strDelimiter.GetSizeInBytes(); i++) {
                c = ReadByte();
                if (this->GetError()) {
                    for (Int j = 0; j < delString.GetElementCount(); j++)
                        retString.Append(delString[j]);
                    break;
                }
                delString.Append(c);
                if (c != strDelimiter.GetData()[i])
                    break;
            }
            if (strDelimiter.GetSizeInBytes() > delString.GetElementCount())
                for (Int i = 0; i < delString.GetElementCount(); i++)
                    retString.Append(delString[i]);
            else {
                if (memcmp(strDelimiter.GetData(),
                           delString.GetData(), delString.GetElementCount()))
                {
                    for (Int i = 0; i < delString.GetElementCount(); i++)
                        retString.Append(delString[i]);
                }
                else
                    break;
            }
        } else {
            retString.Append(c);
        }
    }
    result->Resize(retString.GetElementCount());
    memcpy((Char*)result->GetData(),
           retString.GetData(), retString.GetElementCount());
}
Пример #21
0
void BufferToString(String& dest, const void* data, unsigned size)
{
    // Precalculate needed string size
    const unsigned char* bytes = (const unsigned char*)data;
    unsigned length = 0;
    for (unsigned i = 0; i < size; ++i)
    {
        // Room for separator
        if (i)
            ++length;

        // Room for the value
        if (bytes[i] < 10)
            ++length;
        else if (bytes[i] < 100)
            length += 2;
        else
            length += 3;
    }

    dest.Resize(length);
    unsigned index = 0;

    // Convert values
    for (unsigned i = 0; i < size; ++i)
    {
        if (i)
            dest[index++] = ' ';

        if (bytes[i] < 10)
        {
            dest[index++] = '0' + bytes[i];
        }
        else if (bytes[i] < 100)
        {
            dest[index++] = (char)('0' + bytes[i] / 10);
            dest[index++] = (char)('0' + bytes[i] % 10);
        }
        else
        {
            dest[index++] = (char)('0' + bytes[i] / 100);
            dest[index++] = (char)('0' + bytes[i] % 100 / 10);
            dest[index++] = (char)('0' + bytes[i] % 10);
        }
    }
}
Пример #22
0
static String GenerateNameFromType(StringHash typeHash)
{
    if (unknownTypeToName.Contains(typeHash))
        return unknownTypeToName[typeHash];
    
    String test;
    
    // Begin brute-force search
    unsigned numLetters = letters.Length();
    unsigned combinations = numLetters;
    bool found = false;
    
    for (unsigned i = 1; i < 6; ++i)
    {
        test.Resize(i);
        
        for (unsigned j = 0; j < combinations; ++j)
        {
            unsigned current = j;
            
            for (unsigned k = 0; k < i; ++k)
            {
                test[k] = letters[current % numLetters];
                current /= numLetters;
            }
            
            if (StringHash(test) == typeHash)
            {
                found = true;
                break;
            }
        }
        
        if (found)
            break;
        
        combinations *= numLetters;
    }
    
    unknownTypeToName[typeHash] = test;
    return test;
}
Пример #23
0
ShaderVariation* Shader::GetVariation(ShaderType type, const String& name)
{
    StringHash nameHash(name);
    
    if (type == VS)
    {
        if (vsParser_.HasCombination(name))
        {
            HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = vsVariations_.Find(nameHash);
            // Create the shader variation now if not created yet
            if (i == vsVariations_.End())
            {
                ShaderCombination combination = vsParser_.GetCombination(name);
                
                i = vsVariations_.Insert(MakePair(nameHash, SharedPtr<ShaderVariation>(new ShaderVariation(this, VS))));
                
                String path, fileName, extension;
                SplitPath(GetName(), path, fileName, extension);
                String fullName = path + fileName + "_" + name;
                if (fullName.EndsWith("_"))
                    fullName.Resize(fullName.Length() - 1);
                
                i->second_->SetName(fullName);
                i->second_->SetSourceCode(vsSourceCode_, vsSourceCodeLength_);
                i->second_->SetDefines(combination.defines_, combination.defineValues_);
                
                SetMemoryUse(GetMemoryUse() + sizeof(ShaderVariation));
            }
            
            return i->second_;
        }
        else
            return 0;
    }
    else
    {
        if (psParser_.HasCombination(name))
        {
            HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = psVariations_.Find(nameHash);
            // Create the shader variation now if not created yet
            if (i == psVariations_.End())
            {
                ShaderCombination combination = psParser_.GetCombination(name);
                
                i = psVariations_.Insert(MakePair(nameHash, SharedPtr<ShaderVariation>(new ShaderVariation(this, PS))));
                
                String path, fileName, extension;
                SplitPath(GetName(), path, fileName, extension);
                String fullName = path + fileName + "_" + name;
                if (fullName.EndsWith("_"))
                    fullName.Resize(fullName.Length() - 1);
                
                i->second_->SetName(fullName);
                i->second_->SetSourceCode(psSourceCode_, psSourceCodeLength_);
                i->second_->SetDefines(combination.defines_, combination.defineValues_);
                
                SetMemoryUse(GetMemoryUse() + sizeof(ShaderVariation));
            }
            
            return i->second_;
        }
        else
            return 0;
    }
}
Пример #24
0
ROCKETCORE_API void EMPStringTests()
{
	std::string ss = "test";
	String es = "test";

	es = "hello";
	es.Resize(100);
	es.Erase(4);
	es.Erase(2,100);
	es += "y";

	String sub1 = es.Replace("lo", "l");
	sub1 = sub1.Replace("h", "!");

	EMPTime start;

	{
		// Create a few free buffers
		String tempstring("buffer");
		String tempstring1("buffer1");
		String tempstring2("buffer2");
	}	

	start = SYSClock::GetRealTime();
	for (int i = 0; i < 100000; i++)
	{
		std::string str("test");	
	}
	Log::Message(LC_CORE, Log::LT_ALWAYS, "SS Assign Short: %f", SYSClock::GetRealTime() - start);
	
	start = SYSClock::GetRealTime();	
	for (int i = 0; i < 100000; i++)
	{
		String str("test");
	}
	Log::Message(LC_CORE, Log::LT_ALWAYS, "ES Assign Short: %f", SYSClock::GetRealTime() - start);

	start = SYSClock::GetRealTime();
	for (int i = 0; i < 100000; i++)
	{
		std::string str("test this really long string that won't fit in a local buffer");	
	}
	Log::Message(LC_CORE, Log::LT_ALWAYS, "SS Assign Long: %f", SYSClock::GetRealTime() - start);
	
	start = SYSClock::GetRealTime();	
	for (int i = 0; i < 100000; i++)
	{
		String str("test this really long string that won't fit in a local buffer");
	}
	Log::Message(LC_CORE, Log::LT_ALWAYS, "ES Assign Long: %f", SYSClock::GetRealTime() - start);

	start = SYSClock::GetRealTime();
	for (int i = 0; i < 100000; i++)
	{
		if (ss == "hello")
		{
			int bob = 10;
		}
	}
	Log::Message(LC_CORE, Log::LT_ALWAYS, "SS Compare: %f (char*)", SYSClock::GetRealTime() - start);

	std::string compare = "hello";
	start = SYSClock::GetRealTime();
	for (int i = 0; i < 100000; i++)
	{
		if (ss == compare)
		{
			int bob = 10;
		}
	}
	Log::Message(LC_CORE, Log::LT_ALWAYS, "SS Compare: %f (std::string)", SYSClock::GetRealTime() - start);

	start = SYSClock::GetRealTime();
	for (int i = 0; i < 100000; i++)
	{
		if (es == "hello")
		{
			int bob = 10;
		}
	}
	Log::Message(LC_CORE, Log::LT_ALWAYS, "ES Compare: %f (char*)", SYSClock::GetRealTime() - start);
	
	String oes = es;
	start = SYSClock::GetRealTime();
	for (int i = 0; i < 100000; i++)
	{
		if (es == oes)
		{
			int bob = 10;
		}
	}
	Log::Message(LC_CORE, Log::LT_ALWAYS, "ES Compare: %f (String)", SYSClock::GetRealTime() - start);

	start = SYSClock::GetRealTime();
	std::string ss_concat = "hello";
	for (int i = 0; i < 100000; i++)
	{
		ss_concat += "y";
	}
	Log::Message(LC_CORE, Log::LT_ALWAYS, "SS +=: %f", SYSClock::GetRealTime() - start);

	String es_concat = "hello";
	start = SYSClock::GetRealTime();
	for (int i = 0; i < 100000; i++)
	{
		if (i == 1016)
		{
			int bob = 10;
		}
		es_concat += "y";
	}
	Log::Message(LC_CORE, Log::LT_ALWAYS, "ES +=: %f", SYSClock::GetRealTime() - start);

	const char* x1 = "bob";
	String s;
	String t;
	String u;
	s = "hello";
	t = "hell";
	u = "hello";
	if (s == t)
	{
		int bob = 10;
	}
	if (s == u)
	{
		int bob = 10;
	}

	t = s + u;
	if (t == "hellohello")
	{
		int bob = 10;
	}
	if (t == "x")
	{
		int bob = 10;
	}

	t += u;


	size_t x = s.Find("e");
	size_t y = s.Find("z");
	
	String sub = t.Replace("lo", "l");
	sub = sub.Replace("h", "!");

	sub.FormatString(128, "%s", "hello");
	int bob = 10;
}
Пример #25
0
bool ShaderProgram::Link()
{
    PROFILE(LinkShaderProgram);

    Release();

    if (!graphics || !graphics->IsInitialized())
    {
        LOGERROR("Can not link shader program without initialized Graphics subsystem");
        return false;
    }
    if (!vs || !ps)
    {
        LOGERROR("Shader(s) are null, can not link shader program");
        return false;
    }
    if (!vs->GLShader() || !ps->GLShader())
    {
        LOGERROR("Shaders have not been compiled, can not link shader program");
        return false;
    }

    const String& vsSourceCode = vs->Parent() ? vs->Parent()->SourceCode() : String::EMPTY;
    const String& psSourceCode = ps->Parent() ? ps->Parent()->SourceCode() : String::EMPTY;

    program = glCreateProgram();
    if (!program)
    {
        LOGERROR("Could not create shader program");
        return false;
    }

    glAttachShader(program, vs->GLShader());
    glAttachShader(program, ps->GLShader());
    glLinkProgram(program);

    int linked;
    glGetProgramiv(program, GL_LINK_STATUS, &linked);
    if (!linked)
    {
        int length, outLength;
        String errorString;

        glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length);
        errorString.Resize(length);
        glGetProgramInfoLog(program, length, &outLength, &errorString[0]);
        glDeleteProgram(program);
        program = 0;

        LOGERRORF("Could not link shaders %s: %s", FullName().CString(), errorString.CString());
        return false;
    }

    LOGDEBUGF("Linked shaders %s", FullName().CString());

    glUseProgram(program);

    char nameBuffer[MAX_NAME_LENGTH];
    int numAttributes, numUniforms, numUniformBlocks, nameLength, numElements;
    GLenum type;

    attributes.Clear();

    glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &numAttributes);
    for (int i = 0; i < numAttributes; ++i)
    {
        glGetActiveAttrib(program, i, (GLsizei)MAX_NAME_LENGTH, &nameLength, &numElements, &type, nameBuffer);

        VertexAttribute newAttribute;
        newAttribute.name = String(nameBuffer, nameLength);
        newAttribute.semantic = SEM_POSITION;
        newAttribute.index = 0;

        for (size_t j = 0; elementSemanticNames[j]; ++j)
        {
            if (newAttribute.name.StartsWith(elementSemanticNames[j], false))
            {
                int index = NumberPostfix(newAttribute.name);
                if (index >= 0)
                    newAttribute.index = (unsigned char)index;
                break;
            }
            newAttribute.semantic = (ElementSemantic)(newAttribute.semantic + 1);
        }

        if (newAttribute.semantic == MAX_ELEMENT_SEMANTICS)
        {
            LOGWARNINGF("Found vertex attribute %s with no known semantic in shader program %s", newAttribute.name.CString(), FullName().CString());
            continue;
        }

        newAttribute.location = glGetAttribLocation(program, newAttribute.name.CString());
        attributes.Push(newAttribute);
    }

    glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &numUniforms);
    int numTextures = 0;
    for (int i = 0; i < numUniforms; ++i)
    {
        glGetActiveUniform(program, i, MAX_NAME_LENGTH, &nameLength, &numElements, &type, nameBuffer);

        String name(nameBuffer, nameLength);
        if (type >= GL_SAMPLER_1D && type <= GL_SAMPLER_2D_SHADOW)
        {
            // Assign sampler uniforms to a texture unit according to the number appended to the sampler name
            int location = glGetUniformLocation(program, name.CString());
            int unit = NumberPostfix(name);
            // If no unit number specified, assign in appearance order starting from unit 0
            if (unit < 0)
                unit = numTextures;

            // Array samplers may have multiple elements, assign each sequentially
            if (numElements > 1)
            {
                Vector<int> units;
                for (int j = 0; j < numElements; ++j)
                    units.Push(unit++);
                glUniform1iv(location, numElements, &units[0]);
            }
            else
                glUniform1iv(location, 1, &unit);

            numTextures += numElements;
        }
    }

    glGetProgramiv(program, GL_ACTIVE_UNIFORM_BLOCKS, &numUniformBlocks);
    for (int i = 0; i < numUniformBlocks; ++i)
    {
        glGetActiveUniformBlockName(program, i, (GLsizei)MAX_NAME_LENGTH, &nameLength, nameBuffer);

        // Determine whether uniform block belongs to vertex or pixel shader
        String name(nameBuffer, nameLength);
        bool foundVs = vsSourceCode.Contains(name);
        bool foundPs = psSourceCode.Contains(name);
        if (foundVs && foundPs)
        {
            LOGWARNINGF("Found uniform block %s in both vertex and pixel shader in shader program %s");
            continue;
        }

        // Vertex shader constant buffer bindings occupy slots starting from zero to maximum supported, pixel shader bindings
        // from that point onward
        unsigned blockIndex = glGetUniformBlockIndex(program, name.CString());

        int bindingIndex = NumberPostfix(name);
        // If no number postfix in the name, use the block index
        if (bindingIndex < 0)
            bindingIndex = blockIndex;
        if (foundPs)
            bindingIndex += (unsigned)graphics->NumVSConstantBuffers();

        glUniformBlockBinding(program, blockIndex, bindingIndex);
    }

    return true;
}