Exemple #1
0
        public static object GetFileStats(string path)
        {
            StatResult sr = new StatResult();

            try {
                sr.atime = (long)Directory.GetLastAccessTime(path).Subtract(DateTime.MinValue).TotalSeconds;
                sr.ctime = (long)Directory.GetCreationTime(path).Subtract(DateTime.MinValue).TotalSeconds;
                sr.mtime = (long)Directory.GetLastWriteTime(path).Subtract(DateTime.MinValue).TotalSeconds;

                if (Directory.Exists(path))
                {
                    sr.mode = 0x4000;
                }
                else if (File.Exists(path))
                {
                    FileInfo fi = new FileInfo(path);
                    sr.size = fi.Length;
                    sr.mode = 0x8000; //@TODO - Set other valid mode types (S_IFCHR, S_IFBLK, S_IFIFO, S_IFLNK, S_IFSOCK) (to the degree that they apply)
                }
                else
                {
                    throw new IOException("file does not exist");
                }
            } catch (Exception e) {
                throw ExceptionConverter.CreateThrowable(error, e.Message);
            }

            return(sr);
        }
Exemple #2
0
 public static RE_Pattern Compile(object pattern, object flags)
 {
     try {
         return(new RE_Pattern(ValidatePattern(pattern), Converter.ConvertToInt32(flags)));
     } catch (ArgumentException e) {
         throw ExceptionConverter.CreateThrowable(error, e.Message);
     }
 }
Exemple #3
0
 public RE_Pattern(object pattern, int flags)
 {
     pre = PreParseRegex(ValidatePattern(pattern));
     try {
         RegexOptions opts = FlagsToOption(flags);
         this.re = new Regex(pre.Pattern, opts);
     } catch (ArgumentException e) {
         throw ExceptionConverter.CreateThrowable(error, e.Message);
     }
     this.compileFlags = flags;
 }
Exemple #4
0
        /// <summary>
        /// Preparses a regular expression text returning a ParsedRegex class
        /// that can be used for further regular expressions.
        /// </summary>
        private static ParsedRegex PreParseRegex(string pattern)
        {
            ParsedRegex res = new ParsedRegex(pattern);

            //string newPattern;
            int  cur = 0, nameIndex;
            int  curGroup           = 0;
            bool containsNamedGroup = false;

            for (; ;)
            {
                nameIndex = pattern.IndexOf("(", cur);

                if (nameIndex == -1)
                {
                    break;
                }
                if (nameIndex == pattern.Length - 1)
                {
                    break;
                }

                switch (pattern[++nameIndex])
                {
                case '?':
                    // extension syntax
                    if (nameIndex == pattern.Length - 1)
                    {
                        throw ExceptionConverter.CreateThrowable(error, "unexpected end of regex");
                    }
                    switch (pattern[++nameIndex])
                    {
                    case 'P':
                        //  named regex, .NET doesn't expect the P so we'll remove it;
                        //  also, once we see a named group i.e. ?P then we need to start artificially
                        //  naming all unnamed groups from then on---this is to get around the fact that
                        //  the CLR RegEx support orders all the unnamed groups before all the named
                        //  groups, even if the named groups are before the unnamed ones in the pattern;
                        //  the artificial naming preserves the order of the groups and thus the order of
                        //  the matches
                        containsNamedGroup = true;
                        pattern            = pattern.Remove(nameIndex, 1);
                        break;

                    case 'i': res.Options |= RegexOptions.IgnoreCase; break;

                    case 'L': res.Options &= ~(RegexOptions.CultureInvariant); break;

                    case 'm': res.Options |= RegexOptions.Multiline; break;

                    case 's': res.Options |= RegexOptions.Singleline; break;

                    case 'u': break;

                    case 'x': res.Options |= RegexOptions.IgnorePatternWhitespace; break;

                    case ':': break;   // non-capturing

                    case '=': break;   // look ahead assertion

                    case '<': break;   // positive look behind assertion

                    case '!': break;   // negative look ahead assertion

                    case '(':          // yes/no if group exists, we don't support this
                    default: throw ExceptionConverter.CreateThrowable(error, "Unrecognized extension " + pattern[nameIndex]);
                    }
                    break;

                default:
                    // just another group
                    curGroup++;
                    if (containsNamedGroup)
                    {
                        // need to name this unnamed group
                        pattern = pattern.Insert(nameIndex, "?<Named" + GetRandomString() + ">");
                    }
                    break;
                }

                cur = nameIndex;
            }

            cur = 0;
            for (; ;)
            {
                nameIndex = pattern.IndexOf('\\', cur);

                if (nameIndex == -1 || nameIndex == pattern.Length - 1)
                {
                    break;
                }
                char curChar = pattern[++nameIndex];
                switch (curChar)
                {
                case 'x':
                case 'u':
                case 'a':
                case 'b':
                case 'e':
                case 'f':
                case 'n':
                case 'r':
                case 't':
                case 'v':
                case 'c':
                case 's':
                case 'W':
                case 'w':
                case 'p':
                case 'P':
                case 'S':
                case 'd':
                case 'D':
                    // known escape sequences, leave escaped.
                    break;

                default:
                    System.Globalization.UnicodeCategory charClass = Char.GetUnicodeCategory(curChar);
                    switch (charClass)
                    {
                    // recognized word characters, always unescape.
                    case System.Globalization.UnicodeCategory.ModifierLetter:
                    case System.Globalization.UnicodeCategory.LowercaseLetter:
                    case System.Globalization.UnicodeCategory.UppercaseLetter:
                    case System.Globalization.UnicodeCategory.TitlecaseLetter:
                    case System.Globalization.UnicodeCategory.OtherLetter:
                    case System.Globalization.UnicodeCategory.DecimalDigitNumber:
                    case System.Globalization.UnicodeCategory.LetterNumber:
                    case System.Globalization.UnicodeCategory.OtherNumber:
                    case System.Globalization.UnicodeCategory.ConnectorPunctuation:
                        pattern = pattern.Remove(nameIndex - 1, 1);
                        break;
                    }
                    break;
                }
                cur++;
            }

            res.Pattern = pattern;
            return(res);
        }
Exemple #5
0
        public static int CalculateSize(string fmt)
        {
            int len   = 0;
            int count = 1;

            for (int i = 0; i < fmt.Length; i++)
            {
                switch (fmt[i])
                {
                case 'x':     // pad byte
                case 'c':     // char
                case 'b':     // signed char
                case 'B':     // unsigned char
                case 's':     // char[]
                    len  += (count);
                    count = 1;
                    break;

                case 'h':     // short
                case 'H':     // unsigned short
                    len  += 2 * count;
                    count = 1;
                    break;

                case 'i':     // int
                case 'I':     // unsigned int
                case 'l':     // long
                case 'L':     // unsigned long
                    len  += 4 * count;
                    count = 1;
                    break;

                case 'q':     // long long
                case 'Q':     // unsigned long long
                    len  += 8 * count;
                    count = 1;
                    break;

                case 'f':     // float
                    len  += 4 * count;
                    count = 1;
                    break;

                case 'd':     // double
                    len  += 8 * count;
                    count = 1;
                    break;

                case 'p':     // char[]
                    len += count + 1;
                    break;

                case 'P':     // void *
                    len += IntPtr.Size;
                    break;

                case ' ':
                case '\t':
                    break;

                case '=':     // native
                case '@':     // native
                case '<':     // little endian
                case '>':     // big endian
                case '!':     // big endian
                    if (i != 0)
                    {
                        ExceptionConverter.CreateThrowable(error, "unexpected byte order");
                    }
                    break;

                default:
                    if (Char.IsDigit(fmt[i]))
                    {
                        count = 0;
                        while (Char.IsDigit(fmt[i]))
                        {
                            count = count * 10 + (fmt[i] - '0');
                            i++;
                        }
                        i--;
                        break;
                    }

                    throw Error("bad format string");
                }
            }
            return(len);
        }
Exemple #6
0
 private static Exception Error(string msg)
 {
     return(ExceptionConverter.CreateThrowable(error, msg));
 }