/// <summary>
        /// Sets the type of the given string. If the given string does not exist in the table than it inserts it with a given type.
        /// </summary>
        /// <param name="str">[in] The string.</param>
        /// <param name="type">[in] The type of the string.</param>
        public void setType(string str, StrType type)
        {
            if (str == null || str.Length == 0)
            {
                return;
            }

            uint key;

            ushort hashValue   = hash(str);
            uint   bucketIndex = hashValue % numberOfBuckets;

            if ((key = get(str, hashValue, bucketIndex)) == 0)
            {
                key = (((uint)hashValue) << 16) | count[bucketIndex]++;

                if (count[bucketIndex] == 0xFFFF)
                {
                    WriteMsg.WriteLine("StrTable: Bucket ({0}) is full!", WriteMsg.MsgLevel.Error, bucketIndex);
                }
                if (!strTable[bucketIndex].ContainsKey(key))
                {
                    strTable[bucketIndex].Add(key, new KeyValuePair <string, StrType>(str, type));
                }

#if (DOSTAT)
                statisticCounter[bucketIndex].ElementCount++;
#endif
            }
            else
            {
                strTable[bucketIndex][key] = new KeyValuePair <string, StrType>(strTable[bucketIndex][key].Key, type);
            }
        }
Exemple #2
0
 /// <summary>
 /// It prints out the header for debuging.
 /// </summary>
 public void dump()
 {
     foreach (KeyValuePair <string, string> entry in header)
     {
         WriteMsg.WriteLine("[" + entry.Key + "]=[" + entry.Value + "]", WriteMsg.MsgLevel.Debug);
     }
 }
        /// <summary>
        /// Saves the string table to the given file.
        /// </summary>
        /// <param name="io">[in] The file writer.</param>
        /// <param name="filterType">[in] Type of the strings to save.</param>
        public void save(IO io, StrType filterType)
        {
            try {
                io.writeData("STRTBL", 6);
                io.writeUInt4(numberOfBuckets);

                for (int i = 0; i < numberOfBuckets; ++i)
                {
                    io.writeUShort2(count[i]);
                }

                for (int j = 0; j < numberOfBuckets; ++j)
                {
                    Dictionary <uint, KeyValuePair <string, StrType> > currentMap = strTable[j];

                    foreach (KeyValuePair <uint, KeyValuePair <string, StrType> > entry in currentMap)
                    {
                        if (filterType == StrType.strTmp)
                        {
                            if (entry.Value.Value == StrType.strTmp)
                            {
                                continue;
                            }
                        }
                        else
                        {
                            if (filterType == StrType.strToSave)
                            {
                                if (entry.Value.Value != StrType.strToSave)
                                {
                                    continue;
                                }
                            }
                        }

                        io.writeUInt4(entry.Key);
                        io.writeUInt4((uint)entry.Value.Key.Length);
                        io.writeData(entry.Value.Key, entry.Value.Key.Length);
                    }
                }

                io.writeUInt4(0);
            } catch {
                WriteMsg.WriteLine("StrTable.save(): Error while writing!", WriteMsg.MsgLevel.Error);
            }
        }
        /// <summary>
        /// Loads the string table from the given file.
        /// </summary>
        /// <param name="io">[in] The file reader.</param>
        public void load(IO io)
        {
            try {
                string idBytes;
                io.readData(out idBytes, 6);
                if (idBytes != "STRTBL")
                {
                    WriteMsg.WriteLine("StrTable.load(): Wrong file format!", WriteMsg.MsgLevel.Error);
                    return;
                }

                numberOfBuckets = io.readUInt4();

                strTable = new Dictionary <uint, KeyValuePair <string, StrType> > [numberOfBuckets];
                count    = new ushort[numberOfBuckets];

#if (DOSTAT)
                statisticCounter = new TableStat[numberOfBuckets];
#endif

                for (int i = 0; i < numberOfBuckets; ++i)
                {
                    count[i]    = io.readUShort2();
                    strTable[i] = new Dictionary <uint, KeyValuePair <string, StrType> >();
                }

                while (true)
                {
                    uint key = io.readUInt4();
                    if (key == 0)
                    {
                        break;
                    }
                    int    str_size = (int)io.readUInt4();
                    string buffer;
                    io.readData(out buffer, str_size);

                    int bucket = (int)((key >> 16) % numberOfBuckets);
                    strTable[bucket].Add(key, new KeyValuePair <string, StrType>(buffer, StrType.strDefault));
                }
            } catch {
                WriteMsg.WriteLine("StrTable.load(): Error while reading!", WriteMsg.MsgLevel.Error);
            }
        }
Exemple #5
0
        public static void WriteHelp(Option[] options, bool displayInternal = false)
        {
            WriteMsg.WriteLine("", WriteMsg.MsgLevel.Silent);
            WriteMsg.WriteLine("  Options:", WriteMsg.MsgLevel.Silent);
            WriteMsg.WriteLine(Environment.NewLine, WriteMsg.MsgLevel.Silent);
            foreach (var opt in options)
            {
                if (opt.Internal && !displayInternal)
                {
                    continue;
                }

                if (opt.Type == ArgumentOptions.OT_NONE)
                {
                    WriteMsg.WriteLine("  {0}", WriteMsg.MsgLevel.Silent, opt.Name);
                }
                else
                {
                    if ((opt.Type & ArgumentOptions.OT_WE) != 0 || opt.Type == ArgumentOptions.OT_DEFAULT)
                    {
                        WriteMsg.WriteLine("  {0}={1}", WriteMsg.MsgLevel.Silent, opt.Name, opt.ParameterName);
                    }
                    if ((opt.Type & ArgumentOptions.OT_WC) != 0 || opt.Type == ArgumentOptions.OT_DEFAULT)
                    {
                        WriteMsg.WriteLine("  {0}:{1}", WriteMsg.MsgLevel.Silent, opt.Name, opt.ParameterName);
                    }
                    if ((opt.Type & ArgumentOptions.OT_WOS) != 0)
                    {
                        WriteMsg.WriteLine("  {0}{1}", WriteMsg.MsgLevel.Silent, opt.Name, opt.ParameterName);
                    }
                    if ((opt.Type & ArgumentOptions.OT_WS) != 0)
                    {
                        WriteMsg.WriteLine("  {0} {1}", WriteMsg.MsgLevel.Silent, opt.Name, opt.ParameterName);
                    }
                }
                if (!string.IsNullOrEmpty(opt.Description))
                {
                    WriteMsg.WriteWithBreak(opt.Description, WriteMsg.MsgLevel.Silent, 4);
                    WriteMsg.WriteLine("", WriteMsg.MsgLevel.Silent);
                }
            }
        }
        /// <summary>
        /// Prints out the content of the table.
        /// </summary>
        public void dump()
        {
            uint i            = 0;
            uint totalCounter = 0;

            for (int j = 0; j < numberOfBuckets; ++j)
            {
                ++i;
                WriteMsg.WriteLine("Bucket: {0} (total number: {1})", WriteMsg.MsgLevel.Debug, i, strTable[j].Count);

                foreach (KeyValuePair <uint, KeyValuePair <string, StrType> > entry in strTable[j])
                {
                    WriteMsg.WriteLine("[{0}]:[{1}][{1}]", WriteMsg.MsgLevel.Debug, entry.Key, entry.Value.Key, entry.Value.Value);
                }

                ++totalCounter;
            }

            WriteMsg.WriteLine("Total Count: {0}", WriteMsg.MsgLevel.Debug, totalCounter);
        }
Exemple #7
0
        public static bool ProcessArguments(string[] argv, Option[] options, string optionPrefixes, DelegateOption unrecOption)
        {
            List <Option> defCallbacks = new List <Option>();

            if (options == null)
            {
                WriteMsg.WriteLine("Error: No option given", WriteMsg.MsgLevel.Error);
                return(false);
            }

            for (int pnum = 0; pnum < argv.Length; ++pnum)
            {
                Option?found_option = null;

                foreach (Option opt in options)
                {
                    bool match = false;
                    if (((opt.Type & ArgumentOptions.OT_PREFIX) != ArgumentOptions.OT_NONE) && (opt.Type & (ArgumentOptions.OT_WOS | ArgumentOptions.OT_WC | ArgumentOptions.OT_WE)) == ArgumentOptions.OT_NONE)
                    {
                        if (opt.Name.ToLower().IndexOf(argv[pnum].ToLower()) == 0)
                        {
                            match = true;
                        }
                    }
                    else
                    {
                        if (argv[pnum].ToLower().IndexOf(opt.Name.ToLower()) == 0)
                        {
                            int oplen = opt.Name.Length;
                            try
                            {
                                if (
                                    (argv[pnum].Length == oplen && (((opt.NumberOfParameters == 0 && (opt.Type & ArgumentOptions.OT_OPTION_FILE) == ArgumentOptions.OT_NONE) || (opt.NumberOfParameters == 0 && (opt.Type & ArgumentOptions.OT_WS) != ArgumentOptions.OT_NONE && (opt.Type & ArgumentOptions.OT_DEFAULT) == ArgumentOptions.OT_NONE)))) ||
                                    (argv[pnum].Length == oplen && opt.NumberOfParameters == 1 && (opt.Type & ArgumentOptions.OT_DEFAULT) != ArgumentOptions.OT_NONE && (opt.Type & ArgumentOptions.OT_WS) == ArgumentOptions.OT_NONE && (opt.Type & ArgumentOptions.OT_OPTION_FILE) == ArgumentOptions.OT_NONE && (opt.Type & ArgumentOptions.OT_WOS) == ArgumentOptions.OT_NONE) ||
                                    (argv[pnum].Length != oplen && opt.NumberOfParameters == 1 && (opt.Type & ArgumentOptions.OT_WOS) != ArgumentOptions.OT_NONE && (opt.Type & ArgumentOptions.OT_DEFAULT) == ArgumentOptions.OT_NONE) ||
                                    (argv[pnum][oplen] == '=' && opt.NumberOfParameters == 1 && (opt.Type & ArgumentOptions.OT_WE) != ArgumentOptions.OT_NONE) ||
                                    (argv[pnum][oplen] == ':' && opt.NumberOfParameters == 1 && (opt.Type & ArgumentOptions.OT_WC) != ArgumentOptions.OT_NONE))
                                {
                                    match = true;
                                }
                            }
                            catch (ArgumentOutOfRangeException) { }
                        }
                    }

                    if (match)
                    {
                        if (found_option != null)
                        {
                            WriteMsg.WriteLine("Error: Parameter {0} specified more than once.", WriteMsg.MsgLevel.Error, opt);
                            return(false);
                        }
                        found_option = opt;
                    }
                }

                if (found_option != null)
                {
                    if (found_option.Value.NumberOfParameters == 0)
                    {
                        if (found_option.Value.Process != null && !found_option.Value.Process(found_option.Value, null))
                        {
                            WriteMsg.WriteLine("Error: Cannot process the '{0}' option", WriteMsg.MsgLevel.Error, found_option.Value.Name);
                            return(false);
                        }
                    }
                    else if (found_option.Value.NumberOfParameters == 1)
                    {
                        if ((found_option.Value.Type & ArgumentOptions.OT_PREFIX) != ArgumentOptions.OT_NONE)
                        {
                            if (pnum + 1 >= argv.Length)
                            {
                                WriteMsg.WriteLine("Error: Cannot process the '{0}' option", WriteMsg.MsgLevel.Error, found_option.Value.Name);
                                return(false);
                            }

                            if (!processOptionFile(options, optionPrefixes, unrecOption, found_option.Value, new[] { argv[pnum + 1] }))
                            {
                                WriteMsg.WriteLine("Error: Cannot process the '{0}' option", WriteMsg.MsgLevel.Error, found_option.Value.Name);
                                return(false);
                            }
                            pnum++;
                        }
                        else if ((found_option.Value.Type & ArgumentOptions.OT_DEFAULT) != ArgumentOptions.OT_NONE)
                        {
                            bool   needProcess = true;
                            int    oplen       = found_option.Value.Name.Length;
                            string prm         = string.Empty;

                            if (argv[pnum][oplen] == '=')         //  -option=value (OT_WE)
                            {
                                prm = argv[pnum].Substring(oplen + 1, argv[pnum].Length - oplen - 1);
                            }
                            else if (argv[pnum][oplen] == ':')    //  -option:value (OT_WC)
                            {
                                prm = argv[pnum].Substring(oplen + 1, argv[pnum].Length - oplen - 1);
                            }
                            else
                            {
                                if (found_option.Value.DefaultProcess != null)
                                {
                                    defCallbacks.Add(found_option.Value);
                                    needProcess = false;
                                }
                                else
                                {
                                    WriteMsg.WriteLine("Error: The default value of '{0}' option is used but it does not have default value", WriteMsg.MsgLevel.Error, found_option.Value.Name);
                                    return(false);
                                }
                                prm = argv[pnum + 1];
                                pnum++;
                            }
                            if (needProcess)
                            {
                                if (!processOptionFile(options, optionPrefixes, unrecOption, found_option.Value, new[] { prm }))
                                {
                                    WriteMsg.WriteLine("Error: Cannot process the '{0}' option", WriteMsg.MsgLevel.Error, found_option.Value.Name);
                                    return(false);
                                }
                            }
                        }
                        else
                        {
                            int    oplen = found_option.Value.Name.Length;
                            string prm   = string.Empty;

                            if (argv[pnum][oplen] == '=')         //  -option=value (OT_WE)
                            {
                                prm = argv[pnum].Substring(oplen + 1, argv[pnum].Length - oplen - 1);
                            }
                            else if (argv[pnum][oplen] == ':')    //  -option:value (OT_WC)
                            {
                                prm = argv[pnum].Substring(oplen + 1, argv[pnum].Length - oplen - 1);
                            }
                            else if (argv[pnum][oplen] > 0)       //  -optionvalue (OT_WOS)
                            {
                                prm = argv[pnum].Substring(oplen, argv[pnum].Length - oplen);
                            }
                            else
                            {                                     // -option value (OT_WS)
                                if (pnum + 1 >= argv.Length)
                                {
                                    WriteMsg.WriteLine("Error: Cannot process the '{0}' option", WriteMsg.MsgLevel.Error, found_option.Value.Name);
                                    return(false);
                                }
                                prm = argv[pnum + 1];
                                pnum++;
                            }

                            if (!processOptionFile(options, optionPrefixes, unrecOption, found_option.Value, new[] { prm }))
                            {
                                WriteMsg.WriteLine("Error: Cannot process the '{0}' option", WriteMsg.MsgLevel.Error, found_option.Value.Name);
                                return(false);
                            }
                        }
                    }
                    else if (found_option.Value.NumberOfParameters > 1)
                    {
                        if (pnum + found_option.Value.NumberOfParameters >= argv.Length)
                        {
                            WriteMsg.WriteLine("Error: Cannot process the '{0}' option", WriteMsg.MsgLevel.Error, found_option.Value.Name);
                            return(false);
                        }

                        if (found_option.Value.Process != null && !found_option.Value.Process(found_option.Value, argv[pnum + 1].Split(' ')))
                        {
                            WriteMsg.WriteLine("Error: Cannot process the '{0}' option", WriteMsg.MsgLevel.Error, found_option.Value.Name);
                            return(false);
                        }

                        pnum += found_option.Value.NumberOfParameters;
                    }
                }
                else
                {
                    if (Strchr(optionPrefixes, argv[pnum][0]) != null)
                    {
                        if (unrecOption != null)
                        {
                            unrecOption(argv[pnum]);
                        }
                        else
                        {
                            WriteMsg.WriteLine("Warning: Unrecognized parameter: '{0}'", WriteMsg.MsgLevel.Warning, argv[pnum]);
                        }
                    }
                }
            }

            foreach (var opt in defCallbacks)
            {
                opt.DefaultProcess(opt);
            }

            return(true);
        }