Exemplo n.º 1
0
            public override string GetLine()
            {
                if (stream.Eof)
                {
                    return(null);
                }

                return(stream.ReadLine(-1, null));
            }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="handle"></param>
        /// <param name="length"></param>
        /// <param name="delimiter"></param>
        /// <param name="enclosure"></param>
        /// <param name="escape_char">The escape character used in the CSV string.</param>
        /// <returns>Returns an indexed array containing the fields read.
        /// fgetcsv() returns NULL if an invalid handle is supplied or FALSE on other errors, including end of file.</returns>
        public static PhpValue fgetcsv(PhpResource handle, int length, char delimiter = DefaultCsvDelimiter, char enclosure = DefaultCsvEnclosure, char escape_char = DefaultCsvEscape)
        {
            // check arguments
            PhpStream stream = PhpStream.GetValid(handle, FileAccess.Read);

            if (stream == null)
            {
                return(PhpValue.Null);
            }
            if (length < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(length));             // TODO: Err //PhpException.InvalidArgument("length", LibResources.GetString("arg_negative"));
            }
            if (length <= 0)
            {
                length = -1;                 // no length limit
            }
            if (stream.Eof)
            {
                return(PhpValue.False);
            }

            return((PhpValue)ReadLineCsv(() => (stream.Eof ? null : stream.ReadLine(length, null)), delimiter, enclosure, escape_char));
        }
Exemplo n.º 3
0
        public static PhpArray GetMetaTags(string fileName, FileOpenOptions flags)
        {
            PhpArray result = new PhpArray();

            ScriptContext context = ScriptContext.CurrentContext;

            if (getMetaTagsRegex == null)
            {
                getMetaTagsRegex = new Regex(@"^meta\s+name\s*=\s*(?:(\w*)|'([^']*)'|\u0022([^\u0022]*)\u0022)\s+" +
                                             @"content\s*=\s*(?:(\w*)|'([^']*)'|\u0022([^\u0022]*)\u0022)\s*/?$",
                                             RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
            }

            try
            {
                PhpStream     stream = PhpStream.Open(fileName, "rt", PhpFile.ProcessOptions(flags));
                StringBuilder tag    = new StringBuilder();

                bool in_brackets = false;
                int  in_quotes   = 0;              // 1 = ', 2 = "
                int  in_comment  = 0;              // 1 = <, 2 = <!, 3 = <!-, 4 = <!--, 5 = <!-- -, 6  <!-- --

                while (!stream.Eof)
                {
                    int start_index = 0;

                    string line = stream.ReadLine(-1, null);
                    for (int i = 0; i < line.Length; i++)
                    {
                        switch (line[i])
                        {
                        case '<':
                        {
                            if (!in_brackets && in_quotes == 0 && in_comment == 0)
                            {
                                in_brackets = true;
                                in_comment  = 1;

                                start_index = i + 1;
                            }
                            break;
                        }

                        case '>':
                        {
                            if (in_brackets && in_quotes == 0 && in_comment != 4 && in_comment != 5)
                            {
                                in_brackets = false;
                                in_comment  = 0;

                                if (start_index < i)
                                {
                                    tag.Append(line, start_index, i - start_index);
                                }

                                string str = tag.ToString();
                                tag.Length = 0;

                                // did we reach the end of <head>?
                                if (str.Equals("/head", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    return(result);
                                }

                                // try to match the tag with the <meta> regex
                                Match match = getMetaTagsRegex.Match(str);
                                if (match.Success)
                                {
                                    string name = null, value = null;
                                    for (int j = 1; j <= 3; j++)
                                    {
                                        if (match.Groups[j].Success)
                                        {
                                            name = match.Groups[j].Value;
                                            break;
                                        }
                                    }

                                    if (name != null)
                                    {
                                        for (int j = 4; j <= 6; j++)
                                        {
                                            if (match.Groups[j].Success)
                                            {
                                                value = match.Groups[j].Value;
                                                break;
                                            }
                                        }

                                        result[name] = (value == null ? String.Empty : Core.Convert.Quote(value, context));
                                    }
                                }
                            }
                            break;
                        }

                        case '\'':
                        {
                            if (in_quotes == 0)
                            {
                                in_quotes = 1;
                            }
                            else if (in_quotes == 1)
                            {
                                in_quotes = 0;
                            }
                            break;
                        }

                        case '"':
                        {
                            if (in_quotes == 0)
                            {
                                in_quotes = 2;
                            }
                            else if (in_quotes == 2)
                            {
                                in_quotes = 0;
                            }
                            break;
                        }

                        case '!': if (in_comment == 1)
                            {
                                in_comment = 2;
                            }
                            break;

                        case '-': if (in_comment >= 2 && in_comment < 6)
                            {
                                in_comment++;
                            }
                            break;

                        default:
                        {
                            // reset comment state machine
                            if (in_comment < 4)
                            {
                                in_comment = 0;
                            }
                            if (in_comment > 4)
                            {
                                in_comment = 4;
                            }
                            break;
                        }
                        }
                    }

                    if (in_brackets && start_index < line.Length)
                    {
                        tag.Append(line, start_index, line.Length - start_index);
                    }
                }
            }
            catch (IOException)
            {
                return(null);
            }

            return(result);
        }