Simple List System

Moderator: Event DM

Post Reply
User avatar
Silk
Co-Founder
Posts: 6662
Joined: Fri Sep 14, 2001 6:47 pm
Contact:

Simple List System

Post by Silk » Tue Aug 27, 2002 5:15 am

I've created a very simple list system that is extremely effecient and easy to use. It's uses only strings, but you can use converstion functions to return or store the datatype you need.
<p>

Code: Select all

void Remove_List_Item(object oAttachTo, string sVarName, string sItem){
    string sVarList;
    int iLeftCount;
    int iRightCount;

    if (GetIsObjectValid(oAttachTo) && GetStringLength(sVarName) > 0){
        sVarName = "LIST_" + sVarName;
        sVarList = GetLocalString(oAttachTo, sVarName);

        iLeftCount = FindSubString(sVarList, sItem);
        iRightCount = GetStringLength(sVarList) - (iLeftCount + GetStringLength(sItem) + 1);    // +1 is for the deliminator

        if (iLeftCount != -1){
            sVarList = GetStringLeft(sVarList, iLeftCount) + GetStringRight(sVarList, iRightCount);
        }
        SetLocalString(oAttachTo, sVarName, sVarList);
    }else{
        WriteTimestampedLogEntry("* [Remove_List_Item] : Variable Name is Blank or oAttachTo is not valid");
    }
}

string Get_List_Nth(string sVarList, int N){
    int iDeliminatorPos;
    string sReturnValue;

    if (GetStringLength(sVarList) > 0){
        iDeliminatorPos = FindSubString(sVarList, "|");
        if (iDeliminatorPos != -1){
            sReturnValue = GetStringLeft(sVarList, iDeliminatorPos);
            sVarList = GetStringRight(sVarList, GetStringLength(sVarList) - (iDeliminatorPos + 1));
        }else{
            sReturnValue = "";
            sVarList = "";
        }
    }else{
        sReturnValue = "";
    }

    if (N == 1 || sReturnValue == ""){
        return sReturnValue;
    }else{
        return Get_List_Nth(sVarList, N - 1);
    }
}

void Push_List(object oAttachTo, string sVarName, string sVarValue){
    string sVarList;

    if (GetIsObjectValid(oAttachTo) && GetStringLength(sVarName) > 0){
        sVarName = "LIST_" + sVarName;

        sVarList = GetLocalString(oAttachTo, sVarName);
        sVarList = sVarValue + "|" + sVarList;

        SetLocalString(oAttachTo, sVarName, sVarList);
    }else{
        WriteTimestampedLogEntry("* [Push_List] : Variable Name is Blank or oAttachTo is not valid");
    }
}

void EnQueue_List(object oAttachTo, string sVarName, string sVarValue){
    string sVarList;

    if (GetIsObjectValid(oAttachTo) && GetStringLength(sVarName) > 0){
        sVarName = "LIST_" + sVarName;

        sVarList = GetLocalString(oAttachTo, sVarName);
        sVarList = sVarList + sVarValue + "|";

        SetLocalString(oAttachTo, sVarName, sVarList);
    }else{
        WriteTimestampedLogEntry("* Variable Name is Blank : EnQueue_List");
    }
}

string Pop_List(object oAttachTo, string sVarName){
    string sVarList;
    int iDeliminatorPos;
    string sReturnValue;

    if (GetIsObjectValid(oAttachTo) && GetStringLength(sVarName) > 0){
        sVarName = "LIST_" + sVarName;
        sVarList = GetLocalString(oAttachTo, sVarName);

/* Debug */  WriteTimestampedLogEntry(" milliseconds=[" + IntToString(GetTimeMillisecond()) + "] " + "list_include::Pop_List> Start.  VarName=" + sVarName + ", VarList=" + sVarList + "...");
        sReturnValue = Get_List_Nth(sVarList, 1);
        if (GetStringLength(sReturnValue) > 0){
            Remove_List_Item(oAttachTo, sVarName, sReturnValue);
        }
/* Debug */  WriteTimestampedLogEntry(" milliseconds=[" + IntToString(GetTimeMillisecond()) + "] " + "list_include::Pop_List> End.  VarName=" + sVarName + ", VarList=" + sVarList + ", ReturnValue=" + sReturnValue + "...");
    }else{
        WriteTimestampedLogEntry("* [Pop_List] : Variable Name is Blank or oAttachTo is not valid");
    }

    return sReturnValue;
}

string Get_List_Item(object oAttachTo, string sVarName, int N = 1){
    string sVarList;
    int iDeliminatorPos;
    string sReturnValue;

    if (GetIsObjectValid(oAttachTo) && GetStringLength(sVarName) > 0){
        sVarName = "LIST_" + sVarName;
        sVarList = GetLocalString(oAttachTo, sVarName);

/* Debug */  WriteTimestampedLogEntry(" milliseconds=[" + IntToString(GetTimeMillisecond()) + "] " + "list_include::Pop_List> Start.  VarName=" + sVarName + ", VarList=" + sVarList + "...");
        sReturnValue = Get_List_Nth(sVarList, N);
/* Debug */  WriteTimestampedLogEntry(" milliseconds=[" + IntToString(GetTimeMillisecond()) + "] " + "list_include::Pop_List> End.  VarName=" + sVarName + ", VarList=" + sVarList + ", ReturnValue=" + sReturnValue + "...");
    }else{
        WriteTimestampedLogEntry("* [Get_List_Item] : Variable Name is Blank or oAttachTo is not valid");
    }

    return sReturnValue;
}

void Copy_List(object oAttachTo, string sVarName, string sNewName){
    string sVarList;

    if (GetIsObjectValid(oAttachTo) && GetStringLength(sVarName) > 0 && GetStringLength(sNewName) > 0){
        sVarName = "LIST_" + sVarName;
        sNewName = "LIST_" + sNewName;

        sVarList = GetLocalString(oAttachTo, sVarName);

        if (GetStringLength(sVarList) > 0){
            SetLocalString(oAttachTo, sNewName, sVarList);
        }
    }else{
        WriteTimestampedLogEntry("* [Copy_List] : A Variable Name is Blank or oAttachTo is not valid");
    }
}

void Delete_List(object oAttachTo, string sVarName){
    if (GetIsObjectValid(oAttachTo) && GetStringLength(sVarName) > 0){
        sVarName = "LIST_" + sVarName;
        DeleteLocalString(oAttachTo, sVarName);
    }else{
        WriteTimestampedLogEntry("* [Delete_List] : Variable Name is Blank or oAttachTo is not valid");
    }
}

int InList_List(object oAttachTo, string sVarName, string sItem){
    string sVarList;
    int iReturnValue = FALSE;

    if (GetIsObjectValid(oAttachTo) && GetStringLength(sVarName) > 0){
        sVarName = "LIST_" + sVarName;
        sVarList = GetLocalString(oAttachTo, sVarName);

        iReturnValue = FindSubString(sVarList, sItem) != -1;
    }else{
        WriteTimestampedLogEntry("* [InList_List] : Variable Name is Blank or oAttachTo is not valid");
    }

    return iReturnValue;
}

int IsEmpty_List(object oAttachTo, string sVarName){
    string sVarList;

    if (GetIsObjectValid(oAttachTo) && GetStringLength(sVarName) > 0){
        sVarName = "LIST_" + sVarName;
        sVarList = GetLocalString(oAttachTo, sVarName);
    }else{
        WriteTimestampedLogEntry("* [IsEmpty_List] : Variable Name is Blank or oAttachTo is not valid");
    }

    return (GetStringLength(sVarList) == 0);
}
Silk

Member of the MadK@t lover's group.
Post Reply