Пример #1
0
 static public int Sort(IntPtr l)
 {
     try {
         int argc = LuaDLL.lua_gettop(l);
         if (argc == 1)
         {
             IntList self = (IntList)checkSelf(l);
             self.Sort();
             pushValue(l, true);
             return(1);
         }
         else if (matchType(l, argc, 2, typeof(System.Comparison <System.Int32>)))
         {
             IntList self = (IntList)checkSelf(l);
             System.Comparison <System.Int32> a1;
             LuaDelegation.checkDelegate(l, 2, out a1);
             self.Sort(a1);
             pushValue(l, true);
             return(1);
         }
         else if (matchType(l, argc, 2, typeof(IComparer <System.Int32>)))
         {
             IntList self = (IntList)checkSelf(l);
             System.Collections.Generic.IComparer <System.Int32> a1;
             checkType(l, 2, out a1);
             self.Sort(a1);
             pushValue(l, true);
             return(1);
         }
         else if (argc == 4)
         {
             IntList      self = (IntList)checkSelf(l);
             System.Int32 a1;
             checkType(l, 2, out a1);
             System.Int32 a2;
             checkType(l, 3, out a2);
             System.Collections.Generic.IComparer <System.Int32> a3;
             checkType(l, 4, out a3);
             self.Sort(a1, a2, a3);
             pushValue(l, true);
             return(1);
         }
         pushValue(l, false);
         LuaDLL.lua_pushstring(l, "No matched override function to call");
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Пример #2
0
        private void _rotate(string currentName)
        {
            string[] files = null;
            string   basename;
            string   name;
            string   pattern;

            name     = Path.GetFileName(currentName);
            basename = currentName.Substring(0, currentName.Length - name.Length);

            /* find all files which have names we're concerned about. */
            pattern = name + "*";
            files   = Directory.GetFiles(basename, pattern, SearchOption.TopDirectoryOnly);

            IntList ids = new IntList();

            /* get the list of files */
            foreach (string fullpath in files)
            {
                int id  = 0;
                int idx = fullpath.IndexOf(currentName);
                if (idx == 0)
                {
                    System.Console.WriteLine(fullpath);

                    if (fullpath.Length != currentName.Length &&
                        fullpath.Length > (currentName.Length + 1))
                    {
                        idx = currentName.Length + 1;
                        string number = fullpath.Substring(idx);

                        /* skip this file if the outermost extension is not a number. */
                        if (!int.TryParse(number, out id))
                        {
                            continue;
                        }
                    }
                    else
                    {
                        id = -1;
                    }

                    ids.Add(id);
                    System.Console.WriteLine(id);
                }
                else
                {
                    /* umm ??? */
                    continue;
                }
            }

            /* sort them, biggest first. */
            ids.Sort((x, y) => y.CompareTo(x));

            {
                /* torch the open writer. */
                this.Writer.Close();
                this.Writer = null;
            }


            System.Console.WriteLine("[{0}", ids.Count);

            int idsIdx = -1;

            /* this will give us:
             * filename + filename.0 -> filename.(maxLogs -1)
             * eg:
             * maxLogs = 5
             * you will have:
             * foo.log
             * foo.log.0
             * foo.log.1
             * foo.log.2
             * foo.log.3
             * foo.log.4
             *
             * anything past foo.log.4 will be deleted.
             */
            if (ids.Count > 0 && ids.Count > (this._maxLogs + 1))
            {
                while ((ids.Count - (idsIdx + 1)) > _maxLogs)
                {
                    ++idsIdx;
                    string destFile = string.Format("{0}{1}.{2}", basename, name, ids[idsIdx]);
                    if (ids[idsIdx] == -1)
                    {
                        destFile = currentName;
                    }

                    System.Console.WriteLine("-- {0}", destFile);
                    try{
                        File.Delete(destFile);
                    } catch (System.IO.IOException ex) { }
                }
            }

            if (idsIdx < 0)
            {
                idsIdx = 0;
            }

            /* now move them around.
             * starting from where we left off deleting, or the beginning.
             */
            for (; idsIdx < ids.Count; ++idsIdx)
            {
                string sourceFile;
                string destFile = string.Format("{0}{1}.{2}", basename, name, ids[idsIdx] + 1);

                if (ids[idsIdx] == -1)
                {
                    sourceFile = currentName;
                }
                else
                {
                    sourceFile = string.Format("{0}{1}.{2}", basename, name, ids[idsIdx]);
                }

                try
                {
                    if (File.Exists(destFile))
                    {
                        /* if for some reason the dest file was created
                         * after we listed the dir,
                         * move it out of the way.
                         */
                        string newName = Path.GetRandomFileName();
                        newName = Path.Combine(basename, newName);

                        File.Move(destFile, newName);
                    }

                    if (File.Exists(sourceFile))
                    {
                        /* i don't want to do this in this case.
                         * // take care of tunneling issues http://support.microsoft.com/kb/172190
                         * File.SetCreationTime(sourceFile, DateTime.Now);
                         */

                        File.Move(sourceFile, destFile);
                    }
                }
                catch (System.IO.IOException) { }
            }

            /* let the base class do the file-opening... */
            this.Writer = null;
        }