fCraft.StringTree.Add C# (CSharp) Method

Add() public method

public Add ( string name, PlayerInfo payload ) : bool
name string
payload PlayerInfo
return bool
        public bool Add( string name, PlayerInfo payload ) {
            StringNode temp = root;
            int code;
            for( int i = 0; i < name.Length; i++ ) {
                code = CharCode( name[i] );
                if( temp.children[code] == null ) {
                    temp.children[code] = new StringNode();
                }
                if( temp.tag == EMPTY ) {
                    temp.tag = (byte)code;
                } else {
                    temp.tag = MULTI;
                }
                temp = temp.children[code];
            }
            if( temp.payload != null )
                return false;
            temp.payload = payload;
            count++;
            return true;
        }

Usage Example

Example #1
0
 public void Load()
 {
     if (File.Exists(FileName))
     {
         using (StreamReader reader = File.OpenText(FileName)) {
             reader.ReadLine(); // header
             while (!reader.EndOfStream)
             {
                 string[] fields = reader.ReadLine().Split(',');
                 if (fields.Length == PlayerInfo.fieldCount)
                 {
                     try {
                         PlayerInfo info = new PlayerInfo(world, fields);
                         tree.Add(info.name, info);
                         list.Add(info);
                     } catch (FormatException ex) {
                         world.log.Log("PlayerDB.Load: Could not parse a record: {0}.", LogType.Error, ex.Message);
                     } catch (IOException ex) {
                         world.log.Log("PlayerDB.Load: Error while trying to read from file: {0}.", LogType.Error, ex.Message);
                     }
                 }
             }
         }
         world.log.Log("PlayerDB.Load: Done loading player DB ({0} records).", LogType.Debug, tree.Count());
     }
     else
     {
         world.log.Log("PlayerDB.Load: No player DB file found.", LogType.Warning);
     }
 }