예제 #1
0
        public bool TryGetFirstProperty(string key, out NjoxProperty property)
        {
            int index = m_Properties.FindIndex(v => v.Key == key);

            if (index == -1)
            {
                property = null;
                return(false);
            }

            property = m_Properties[index];
            return(true);
        }
예제 #2
0
 public void AddProperty(NjoxProperty property)
 {
     m_Properties.Add(property);
 }
예제 #3
0
        /// <summary>
        /// Attempt to parse njox properties from a line
        /// </summary>
        /// <param name="line">The line which currently contains the properties</param>
        /// <param name="njoxProperties">Where the parsed properties will be stored</param>
        /// <returns>If the line is succesfully parsed</returns>
        public static bool TryParseProperties(string line, out List <NjoxProperty> njoxProperties)
        {
            // Check it is just a single line
            if (line.Count(c => c == '\n') > 1)
            {
                Logger.LogError("Cannot parse njox properties from multi-line input '" + line + "'");
                njoxProperties = null;
                return(false);
            }

            // Read key=value pairs (Taking "" into account)
            njoxProperties = new List <NjoxProperty>();

            NjoxProperty currentProp   = new NjoxProperty();
            bool         readingKey    = true;
            char         lastChar      = '\0';
            bool         readingString = false;

            foreach (char c in line)
            {
                // Have read end of property
                if (!readingString && c == ' ')
                {
                    if (currentProp.IsEmpty())
                    {
                        Logger.LogError("Cannot add empty property (Potential multi-spaces between properties?) at '" + line + "'");
                        return(false);
                    }

                    // Add and reset vars
                    njoxProperties.Add(currentProp);

                    currentProp   = new NjoxProperty();
                    readingKey    = true;
                    lastChar      = '\0';
                    readingString = false;
                    continue;
                }

                // Reading Key
                if (readingKey)
                {
                    // Found key=value split
                    if (c == '=')
                    {
                        readingKey = false;
                    }
                    else
                    {
                        // Remained of line must be a comment
                        if (lastChar == '/' && c == '/')
                        {
                            // Remove last char
                            currentProp.Key = currentProp.Key.Substring(0, currentProp.Key.Length - 1);
                            break;
                        }

                        currentProp.Key += c;
                    }
                }

                // Not Reading Value
                else
                {
                    // Read string for values and ignore any \"
                    if (c == '"' && lastChar != '\\')
                    {
                        readingString = !readingString;
                    }

                    else
                    {
                        if (readingString)
                        {
                            if (c == 'n' && lastChar == '\\')
                            {
                                currentProp.Value += '\n';
                            }

                            else if (c == 't' && lastChar == '\\')
                            {
                                currentProp.Value += '\t';
                            }

                            else if (c == 'r' && lastChar == '\\')
                            {
                                currentProp.Value += '\r';
                            }

                            else if (c == '"' && lastChar == '\\')
                            {
                                currentProp.Value += '"';
                            }

                            else if (c == '\\' && lastChar == '\\')
                            {
                                currentProp.Value += '\\';
                            }

                            else
                            {
                                currentProp.Value += c;
                            }
                        }
                        else
                        {
                            // Remained of line must be a comment
                            if (lastChar == '/' && c == '/')
                            {
                                // Remove last char
                                currentProp.Value = currentProp.Value.Substring(0, currentProp.Value.Length - 1);
                                break;
                            }

                            currentProp.Value += c;
                        }
                    }
                }

                lastChar = c;
            }

            // Add last
            if (!currentProp.IsEmpty())
            {
                njoxProperties.Add(currentProp);
            }

            return(true);
        }