File: StringFormat.js
Size: 15597
Date: Sat, 08 Dec 2007 15:37:07 +0100
Type: js
 
 
//**************************************************************************
 
//      Copyright  Sybase, Inc. 1998-1999
 
//                       All Rights reserved.
 
//
 
//  Sybase, Inc. ("Sybase") claims copyright in this
 
//  program and documentation as an unpublished work, versions of
 
//  which were first licensed on the date indicated in the foregoing
 
//  notice.  Claim of copyright does not imply waiver of Sybase's
 
//  other rights.
 
//
 
//   This code is generated by the PowerBuilder HTML DataWindow generator.
 
//   It is provided subject to the terms of the Sybase License Agreement
 
//   for use as is, without alteration or modification.  
 
//   Sybase shall have no obligation to provide support or error correction 
 
//   services with respect to any altered or modified versions of this code.  
 
//
 
//       ***********************************************************
 
//       **     DO NOT MODIFY OR ALTER THIS CODE IN ANY WAY       **
 
//       ***********************************************************
 
//
 
//       ***************************************************************
 
//       ** IMPLEMENTATION DETAILS SUBJECT TO CHANGE WITHOUT NOTICE.  **
 
//       **            DO NOT RELY ON IMPLEMENTATION!!!!              **
 
//       ***************************************************************
 
//
 
// Use the public interface only.
 
//**************************************************************************
 
 
 
 
 
function DW_StringParse(inString)
 
{
 
    var result = new DW_StringClass();
 
 
 
    if (DW_parseStringAgainstMask(inString, result))
 
    {
 
        return result.str;
 
    }
 
    else
 
        return null;
 
}
 
 
 
function DW_IsString(inString)
 
{
 
    if (inString == "")
 
        return true;
 
    return DW_parseStringAgainstMask(inString, null);
 
}
 
 
 
function DW_parseStringAgainstMask(inString, outStr)
 
{
 
    var Mask = gMask;
 
 
 
    if (Mask == "") 
 
    {
 
        if(outStr != null)
 
            outStr.str = inString;
 
        return true;
 
    }
 
 
 
    // Get encoded format against mask
 
    var format = new DW_StringEncodingClass(Mask);
 
 
 
    // if invald mask, return false
 
    if (!format.bValid) return false;
 
 
 
    // Create a new number class
 
    var s = new DW_StringClass();
 
 
 
    var currChar;
 
    var charIndex = 0;
 
 
 
    var index = 0;
 
    var encodedFormat = format.encodedFormat;
 
    var action;
 
 
 
    // Create a new number class
 
    var s = new DW_StringClass();
 
 
 
    while (charIndex < inString.length && index < encodedFormat.length)
 
    {
 
        // Now extract one token from encode string
 
        action = 0;
 
 
 
        if( index < encodedFormat.length)
 
        {
 
            action = encodedFormat[index];
 
            index++;
 
        }
 
 
 
        if (typeof action == "string")
 
        {
 
            currChar = inString.substring(charIndex, charIndex+action.length); 
 
            charIndex = charIndex + action.length
 
 
 
            if (action != currChar)
 
                return false;
 
            else
 
                continue;
 
        }
 
        else
 
            currChar = inString.charAt(charIndex++);
 
 
 
        if(action == DWFMT_allChars)
 
        {
 
            s.str = inString;
 
            break;
 
        }
 
        else if(action == DWFMT_allChar)
 
        {
 
            s.str += currChar;
 
        }
 
        else if(action == DWFMT_stringChar)
 
        {
 
            if (DW_parseIsAlpha(currChar) || DW_parseIsDigit(currChar) || currChar==" ") 
 
                s.str += currChar;
 
            else
 
                return false;
 
        }
 
        else if(action == DWFMT_upperCaseChar)
 
        {
 
            if (DW_parseIsAlpha(currChar) || DW_parseIsDigit(currChar) || currChar==" " || currChar==DW_decimalChar) 
 
                s.str += currChar.toUpperCase();
 
            else
 
                return false;
 
        }
 
        else if(action == DWFMT_lowerCaseChar || currChar==" ")
 
        {
 
            if (DW_parseIsAlpha(currChar) || DW_parseIsDigit(currChar) || currChar==" " || currChar==DW_decimalChar) 
 
                s.str += currChar.toLowerCase();
 
            else
 
                return false;
 
        }
 
        else if(action == DWFMT_numberChar)
 
        {
 
            if (DW_parseIsDigit(currChar)) 
 
                s.str += currChar;
 
            else
 
                return false;
 
        }
 
    }
 
 
 
    if(outStr != null)
 
        outStr.str = s.str;
 
 
 
    return true;
 
}
 
 
 
 
 
//
 
// String formatting code
 
//
 
var DWFMT_allChars = 0;
 
var DWFMT_allChar = 1;
 
var DWFMT_stringChar = 2;
 
var DWFMT_upperCaseChar = 3;
 
var DWFMT_lowerCaseChar = 4;
 
var DWFMT_numberChar = 5;
 
 
 
function DW_StringEncodingClass(inString)
 
{
 
    var index;
 
    var currChar;
 
    var encodedFormat = new Array();
 
    var accum = "";
 
    var numInSection;
 
    var offset = 0;
 
    var bValid = true;
 
 
 
    this.encodedFormat = encodedFormat;
 
    this.color = "";
 
 
 
    var strLen = inString.length;
 
    for (index=0; index < strLen && bValid; )
 
        {
 
        currChar = inString.charAt(index);
 
        // handle keywords
 
        if (currChar == "[")
 
            {
 
            if (accum != "")
 
                encodedFormat[offset++] = accum;
 
            accum = "";
 
            index++;
 
            for (; inString.charAt(index) != "]"; index++)
 
                accum += inString.charAt(index);
 
            index++; // skip ]
 
            if (accum.toUpperCase() == "GENERAL")
 
                encodedFormat[offset++] = DWFMT_allChars;
 
            else
 
                {
 
                if (!parseInt(accum)) 
 
                    this.color = accum.toLowerCase();
 
                else
 
                    this.color = eval(accum);
 
                this.keyword = accum;
 
                }
 
            accum = "";
 
            }
 
        else if (currChar == "X" || currChar == "x" || currChar == "@")
 
            {
 
            if (accum != "")
 
                encodedFormat[offset++] = accum;
 
            accum = "";
 
            index++;
 
            encodedFormat[offset++] = DWFMT_allChar;
 
            }
 
        else if (currChar == "A" || currChar == "a")
 
            {
 
            if (accum != "")
 
                encodedFormat[offset++] = accum;
 
            accum = "";
 
            index++;
 
            encodedFormat[offset++] = DWFMT_stringChar;
 
            }
 
        else if (currChar == "!")
 
            {
 
            if (accum != "")
 
                encodedFormat[offset++] = accum;
 
            accum = "";
 
            index++;
 
            encodedFormat[offset++] = DWFMT_upperCaseChar;
 
            }
 
        else if (currChar == "^")
 
            {
 
            if (accum != "")
 
                encodedFormat[offset++] = accum;
 
            accum = "";
 
            index++;
 
            encodedFormat[offset++] = DWFMT_lowerCaseChar;
 
            }
 
        else if (currChar == "#" || currChar == "0")
 
            {
 
            if (accum != "")
 
                encodedFormat[offset++] = accum;
 
            accum = "";
 
            index++;
 
            encodedFormat[offset++] = DWFMT_numberChar;
 
            }
 
        else if (currChar == "\\")
 
            {
 
            index++;
 
            accum += inString.charAt(index++);
 
            }
 
        else if (currChar == "'")
 
            {
 
            index++;
 
            while (index < strLen)
 
                {
 
                currChar = inString.charAt(index);
 
                if (currChar == "'")
 
                    break;
 
                accum += currChar;
 
                index++;
 
                }
 
            // check if we fell off end before finding closing quotes
 
            if (index == strLen)
 
                bValid = false;
 
 
 
            index++; // skip trailing '
 
            }
 
        else
 
            {
 
            accum += currChar;
 
            index++;
 
            }
 
        }
 
 
 
    if (accum != "")
 
        encodedFormat[offset++] = accum;
 
 
 
    if (encodedFormat.length == 0 ) 
 
        bValid = false;
 
 
 
    this.bValid = bValid;
 
}
 
 
 
function DW_StringFormatClass(formatString)
 
{
 
    var semiOffset = formatString.indexOf(";");
 
 
 
    if (semiOffset != -1)
 
        {
 
        this.mainFormat = new DW_StringEncodingClass(formatString.substring(0, semiOffset));
 
        this.nullFormat = new DW_StringEncodingClass(formatString.substring(semiOffset+1, formatString.length));
 
 
 
        this.bValid = this.mainFormat.bValid && this.nullFormat.bValid;
 
        }
 
    else
 
        {
 
        this.mainFormat = new DW_StringEncodingClass(formatString);
 
        this.nullFormat = null;
 
        this.bValid = this.mainFormat.bValid;
 
        }
 
 
 
}
 
 
 
function DW_FormatString(formatString, value, control)
 
{
 
    var stringFormat = new DW_StringFormatClass(formatString);
 
    var result = "";
 
    var format;
 
 
 
    if (stringFormat.bValid)
 
        {
 
        if (value == null && stringFormat.nullFormat != null)
 
            format = stringFormat.nullFormat;
 
        else
 
            format = stringFormat.mainFormat;
 
 
 
        var index;
 
        var encodedFormat = format.encodedFormat;
 
        var action;
 
        var strIndex = 0;
 
        var strLen;
 
 
 
        // a length of -1 means that we will always put in " " chars
 
        if (value != null)
 
            strLen = value.length;
 
        else
 
            strLen = -1;
 
 
 
        for (index=0; index < encodedFormat.length ; index++)
 
            {
 
            action = encodedFormat[index];
 
            if (typeof action == "string")
 
                result += action;
 
            else if (action == DWFMT_allChars)
 
                {
 
                // get the rest
 
                if (strIndex < strLen)
 
                    {
 
                    result += value.substring(strIndex, strLen);
 
                    strIndex = strLen;
 
                    }
 
                }
 
            else if (action == DWFMT_allChar)
 
                {
 
                if (strIndex < strLen)
 
                    result += value.charAt(strIndex);
 
                else
 
                    result += " ";
 
                strIndex++;
 
                }
 
            else if (action == DWFMT_stringChar)
 
                {
 
                if (strIndex < strLen && (DW_parseIsAlpha(value.charAt(strIndex)) || DW_parseIsDigit(value.charAt(strIndex))))
 
                    result += value.charAt(strIndex);
 
                else
 
                    result += " ";
 
                strIndex++;
 
                }
 
            else if (action == DWFMT_upperCaseChar)
 
                {
 
                if (strIndex < strLen && (DW_parseIsAlpha(value.charAt(strIndex)) || DW_parseIsDigit(value.charAt(strIndex)) || value.charAt(strIndex)==DW_decimalChar))
 
                    result += value.charAt(strIndex).toUpperCase();
 
                else
 
                    result += " ";
 
                strIndex++;
 
                }
 
            else if (action == DWFMT_lowerCaseChar)
 
                {
 
                if (strIndex < strLen && (DW_parseIsAlpha(value.charAt(strIndex)) || DW_parseIsDigit(value.charAt(strIndex)) || value.charAt(strIndex)==DW_decimalChar))
 
                    result += value.charAt(strIndex).toLowerCase();
 
                else
 
                    result += " ";
 
                strIndex++;
 
                }
 
            else if (action == DWFMT_numberChar)
 
                {
 
                if (strIndex < strLen && DW_parseIsDigit(value.charAt(strIndex)))
 
                    result += value.charAt(strIndex);
 
                else
 
                    result += " ";
 
                strIndex++;
 
                }
 
            }
 
        }
 
    else if ( value != null )
 
        // Simulating a [general] format.
 
        result = value;
 
    else
 
        result = "";
 
 
 
    if (this.bStylePositioning && format && format.bValid)  
 
        if ( format.color == "" || typeof format.color == "string" )
 
            control.style.color = format.color;
 
        else
 
            control.style.color = convertToRGB( format.color );
 
 
 
    return result;
 
}
 
 
 
function DW_StringClass(str) 
 
{
 
    if (arguments.length == 0)
 
        str = "";
 
 
 
    this.str = str;
 
}
 
 
 
 
 
function DW_LeftTrim(inString)
 
{
 
    var index, tempChar, outString = "";
 
    var strLength = inString.length;
 
    // skip leading blanks
 
    for (index=0; index < strLength; index++)
 
        {
 
        tempChar= inString.charAt (index);
 
        if (tempChar != " ")
 
            break;
 
        }
 
    if (index < strLength)
 
        outString = inString.substring(index, strLength);
 
 
 
    return outString;
 
}
 
 
 
function DW_RightTrim(inString)
 
{
 
    var index, tempChar, outString = "";
 
    // skip trailing blanks
 
    for (index=inString.length-1; index >= 0; index--)
 
        {
 
        tempChar= inString.charAt (index);
 
        if (tempChar != " ")
 
            break;
 
        }
 
    if (index >= 0)    
 
        outString = inString.substring(0, index+1);
 
 
 
    return outString;
 
}
 
 
 
function DW_ConvertLike(inString, escapeChar)
 
{
 
    var index;
 
    var outString = "";
 
    var tempChar;
 
    var strLength = inString.length;
 
 
 
    if (arguments.length < 2)
 
        escapeChar = "";
 
 
 
    // force to string type or charAt will fail!
 
    inString = inString + "";
 
    for ( index=0; index < strLength; index++ )
 
        {
 
        tempChar = inString.charAt( index );
 
        if (tempChar == escapeChar) 
 
            {
 
            index++;
 
            outString += inString.charAt( index );
 
            }
 
        else if (tempChar == "%")
 
            outString += ".*";
 
        else if (tempChar == "_")
 
            outString += ".";
 
        else
 
            outString += tempChar;
 
        }
 
    return new RegExp(outString);
 
}
 
 
 
function DW_Fill(inString, length)
 
{
 
    var outIndex, inIndex;
 
    var outString = "";
 
    var strLength = inString.length;
 
 
 
    // Fill("*--", 4) = "*--*"
 
    inString = inString + "";
 
    for ( outIndex=0, inIndex=0; outIndex < length; outIndex++, inIndex++ )
 
        {
 
        // restart input string if necessary
 
        if (inIndex >= strLength)
 
            inIndex = 0;
 
        outString += inString.charAt( inIndex );
 
        }
 
    return outString;
 
}
 
 
 
function DW_Mid(inString, start, length)
 
{
 
    var end
 
 
 
    if (arguments.length < 3)
 
        end = inString.length;
 
    else
 
        end = start + length;
 
 
 
    return inString.substring(start-1,end)
 
}
 
 
 
function DW_Replace(inString, start, length, replString)
 
{
 
    var sResult = inString.substring(0, start-1);
 
    sResult += replString;
 
    sResult += inString.substring(start + length -1, inString.length);
 
 
 
    return sResult;
 
}
 
 
 
function DW_Right(inString, length)
 
{
 
    var strLen = inString.length
 
    return inString.substring(strLen - length, strLen)
 
}
 
 
 
function DW_Space(n)
 
{
 
    var sResult = "";
 
    for (var i=0; i < n;  i++)
 
        sResult += " ";
 
 
 
    return sResult;
 
}
 
 
 
function DW_WordCap(inString)
 
{
 
    var lowerCaseString = inString.toLowerCase();
 
    var outString = "";
 
    var index;
 
    var tempChar;
 
    var bCap = true;
 
    var strLength = lowerCaseString.length;
 
 
 
    for(index=0; index < strLength; index++)
 
        {
 
        tempChar = lowerCaseString.charAt( index );
 
        if (bCap && tempChar != " ")
 
            {
 
            outString += tempChar.toUpperCase();
 
            bCap = false;
 
            }
 
        else if (tempChar == " ")
 
            {
 
            bCap = true;
 
            outString += tempChar;
 
            }
 
        else
 
            outString += tempChar;
 
        }
 
 
 
    return outString;
 
}