Example #1
0
 internal HistoryInfo[] GetEntries(WildcardPattern wildcardpattern, long count, SwitchParameter newest)
 {
     lock (this._syncRoot)
     {
         if (count < -1L)
         {
             throw PSTraceSource.NewArgumentOutOfRangeException("count", count);
         }
         if (newest.ToString() == null)
         {
             throw PSTraceSource.NewArgumentNullException("newest");
         }
         if ((count > this._countEntriesAdded) || (count == -1L))
         {
             count = this._countEntriesInBuffer;
         }
         ArrayList list = new ArrayList();
         long num = 1L;
         if (this._capacity != 0x1000)
         {
             num = this.SmallestIDinBuffer();
         }
         if (count != 0L)
         {
             if (!newest.IsPresent)
             {
                 long id = 1L;
                 if ((this._capacity != 0x1000) && (this._countEntriesAdded > this._capacity))
                 {
                     id = num;
                 }
                 long num3 = 0L;
                 while (num3 <= (count - 1L))
                 {
                     if (id > this._countEntriesAdded)
                     {
                         break;
                     }
                     if (!this._buffer[this.GetIndexFromId(id)].Cleared && wildcardpattern.IsMatch(this._buffer[this.GetIndexFromId(id)].CommandLine.Trim()))
                     {
                         list.Add(this._buffer[this.GetIndexFromId(id)].Clone());
                         num3 += 1L;
                     }
                     id += 1L;
                 }
             }
             else
             {
                 long num4 = this._countEntriesAdded;
                 long num5 = 0L;
                 while (num5 <= (count - 1L))
                 {
                     if ((((this._capacity != 0x1000) && (this._countEntriesAdded > this._capacity)) && (num4 < num)) || (num4 < 1L))
                     {
                         break;
                     }
                     if (!this._buffer[this.GetIndexFromId(num4)].Cleared && wildcardpattern.IsMatch(this._buffer[this.GetIndexFromId(num4)].CommandLine.Trim()))
                     {
                         list.Add(this._buffer[this.GetIndexFromId(num4)].Clone());
                         num5 += 1L;
                     }
                     num4 -= 1L;
                 }
             }
         }
         else
         {
             for (long i = 1L; i <= this._countEntriesAdded; i += 1L)
             {
                 if (!this._buffer[this.GetIndexFromId(i)].Cleared && wildcardpattern.IsMatch(this._buffer[this.GetIndexFromId(i)].CommandLine.Trim()))
                 {
                     list.Add(this._buffer[this.GetIndexFromId(i)].Clone());
                 }
             }
         }
         HistoryInfo[] array = new HistoryInfo[list.Count];
         list.CopyTo(array);
         return array;
     }
 }
Example #2
0
        }// end function


        /// <summary>
        /// Get History Entries based on the WildCard Pattern value.
        /// If passed 0, returns all the values, else return on the basis of count.
        /// </summary>
        /// <param name="wildcardpattern"></param>
        /// <param name="count"></param>
        /// <param name="newest"></param>
        /// <returns></returns>
        internal HistoryInfo[] GetEntries(WildcardPattern wildcardpattern, long count, SwitchParameter newest)
        {
            lock (_syncRoot)
            {
                if (count < -1)
                {
                    throw PSTraceSource.NewArgumentOutOfRangeException("count", count);
                }
                if (newest.ToString() == null)
                {
                    throw PSTraceSource.NewArgumentNullException("newest");
                }
                if (count > _countEntriesAdded || count == -1)
                {
                    count = _countEntriesInBuffer;
                }
                List<HistoryInfo> cmdlist = new List<HistoryInfo>();
                long SmallestID = 1;
                //if buffersize is changes,Get the smallest entry that's not cleared in the buffer
                if (_capacity != DefaultHistorySize)
                    SmallestID = SmallestIDinBuffer();
                if (count != 0)
                {
                    if (!newest.IsPresent)
                    {
                        long id = 1;
                        if (_capacity != DefaultHistorySize)
                        {
                            if (_countEntriesAdded > _capacity)
                                id = SmallestID;
                        }
                        for (long i = 0; i <= count - 1;)
                        {
                            if (id > _countEntriesAdded) break;
                            if (_buffer[GetIndexFromId(id)].Cleared == false && wildcardpattern.IsMatch(_buffer[GetIndexFromId(id)].CommandLine.Trim()))
                            {
                                cmdlist.Add(_buffer[GetIndexFromId(id)].Clone()); i++;
                            }
                            id++;
                        }
                    }
                    else
                    {
                        long id = _countEntriesAdded;
                        for (long i = 0; i <= count - 1;)
                        {
                            //if buffersize is changed,we have to loop from max entry to min entry thats not cleared
                            if (_capacity != DefaultHistorySize)
                            {
                                if (_countEntriesAdded > _capacity)
                                {
                                    if (id < SmallestID)
                                        break;
                                }
                            }
                            if (id < 1) break;
                            if (_buffer[GetIndexFromId(id)].Cleared == false && wildcardpattern.IsMatch(_buffer[GetIndexFromId(id)].CommandLine.Trim()))
                            {
                                cmdlist.Add(_buffer[GetIndexFromId(id)].Clone()); i++;
                            }
                            id--;
                        }
                    }
                }
                else
                {
                    for (long i = 1; i <= _countEntriesAdded; i++)
                    {
                        if (_buffer[GetIndexFromId(i)].Cleared == false && wildcardpattern.IsMatch(_buffer[GetIndexFromId(i)].CommandLine.Trim()))
                        {
                            cmdlist.Add(_buffer[GetIndexFromId(i)].Clone());
                        }
                    }
                }
                HistoryInfo[] entries = new HistoryInfo[cmdlist.Count];
                cmdlist.CopyTo(entries);
                return entries;
            }
        }
Example #3
0
 internal HistoryInfo[] GetEntries(long id, long count, SwitchParameter newest)
 {
     this.ReallocateBufferIfNeeded();
     if (count < -1L)
     {
         throw PSTraceSource.NewArgumentOutOfRangeException("count", count);
     }
     if (newest.ToString() == null)
     {
         throw PSTraceSource.NewArgumentNullException("newest");
     }
     if (((count == -1L) || (count > this._countEntriesAdded)) || (count > this._countEntriesInBuffer))
     {
         count = this._countEntriesInBuffer;
     }
     if ((count == 0L) || (this._countEntriesInBuffer == 0))
     {
         return new HistoryInfo[0];
     }
     lock (this._syncRoot)
     {
         ArrayList list = new ArrayList();
         if (id > 0L)
         {
             long num;
             long num2 = id;
             if (!newest.IsPresent)
             {
                 num = (num2 - count) + 1L;
                 if (num < 1L)
                 {
                     num = 1L;
                 }
                 for (long i = num2; i >= num; i -= 1L)
                 {
                     if (num <= 1L)
                     {
                         break;
                     }
                     if ((this._buffer[this.GetIndexFromId(i)] != null) && this._buffer[this.GetIndexFromId(i)].Cleared)
                     {
                         num -= 1L;
                     }
                 }
                 for (long j = num; j <= num2; j += 1L)
                 {
                     if ((this._buffer[this.GetIndexFromId(j)] != null) && !this._buffer[this.GetIndexFromId(j)].Cleared)
                     {
                         list.Add(this._buffer[this.GetIndexFromId(j)].Clone());
                     }
                 }
             }
             else
             {
                 num = (num2 + count) - 1L;
                 if (num >= this._countEntriesAdded)
                 {
                     num = this._countEntriesAdded;
                 }
                 for (long k = num2; k <= num; k += 1L)
                 {
                     if (num >= this._countEntriesAdded)
                     {
                         break;
                     }
                     if ((this._buffer[this.GetIndexFromId(k)] != null) && this._buffer[this.GetIndexFromId(k)].Cleared)
                     {
                         num += 1L;
                     }
                 }
                 for (long m = num; m >= num2; m -= 1L)
                 {
                     if ((this._buffer[this.GetIndexFromId(m)] != null) && !this._buffer[this.GetIndexFromId(m)].Cleared)
                     {
                         list.Add(this._buffer[this.GetIndexFromId(m)].Clone());
                     }
                 }
             }
         }
         else
         {
             long num7;
             long num8 = 0L;
             if (this._capacity != 0x1000)
             {
                 num8 = this.SmallestIDinBuffer();
             }
             if (!newest.IsPresent)
             {
                 num7 = 1L;
                 if ((this._capacity != 0x1000) && (this._countEntriesAdded > this._capacity))
                 {
                     num7 = num8;
                 }
                 long num9 = count - 1L;
                 while (num9 >= 0L)
                 {
                     if (num7 > this._countEntriesAdded)
                     {
                         break;
                     }
                     if (((num7 <= 0L) || (this.GetIndexFromId(num7) >= this._buffer.Length)) || this._buffer[this.GetIndexFromId(num7)].Cleared)
                     {
                         num7 += 1L;
                     }
                     else
                     {
                         list.Add(this._buffer[this.GetIndexFromId(num7)].Clone());
                         num9 -= 1L;
                         num7 += 1L;
                     }
                 }
             }
             else
             {
                 num7 = this._countEntriesAdded;
                 long num10 = count - 1L;
                 while (num10 >= 0L)
                 {
                     if ((((this._capacity != 0x1000) && (this._countEntriesAdded > this._capacity)) && (num7 < num8)) || (num7 < 1L))
                     {
                         break;
                     }
                     if (((num7 <= 0L) || (this.GetIndexFromId(num7) >= this._buffer.Length)) || this._buffer[this.GetIndexFromId(num7)].Cleared)
                     {
                         num7 -= 1L;
                     }
                     else
                     {
                         list.Add(this._buffer[this.GetIndexFromId(num7)].Clone());
                         num10 -= 1L;
                         num7 -= 1L;
                     }
                 }
             }
         }
         HistoryInfo[] array = new HistoryInfo[list.Count];
         list.CopyTo(array);
         return array;
     }
 }
Example #4
0
        /// <summary>
        /// Get count HistoryEntries 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="count"></param>
        /// <param name="newest"></param>        
        /// <returns>history entries</returns>
        internal HistoryInfo[] GetEntries(long id, long count, SwitchParameter newest)
        {
            ReallocateBufferIfNeeded();

            if (count < -1)
            {
                throw PSTraceSource.NewArgumentOutOfRangeException("count", count);
            }

            if (newest.ToString() == null)
            {
                throw PSTraceSource.NewArgumentNullException("newest");
            }


            if (count == -1 || count > _countEntriesAdded || count > _countEntriesInBuffer)
                count = _countEntriesInBuffer;

            if (count == 0 || _countEntriesInBuffer == 0)
            {
                return Utils.EmptyArray<HistoryInfo>();
            }

            lock (_syncRoot)
            {
                //Using list instead of an array to store the entries.With array we are getting null values
                //when the historybuffer size is changed
                List<HistoryInfo> entriesList = new List<HistoryInfo>();
                if (id > 0)
                {
                    long firstId, baseId;
                    baseId = id;

                    //get id,count,newest values
                    if (!newest.IsPresent)
                    {
                        //get older entries

                        //Calculate the first id (i.e lowest id to fetch)
                        firstId = baseId - count + 1;

                        //If first id is less than the lowest id in history store,
                        //assign lowest id as first ID
                        if (firstId < 1)
                        {
                            firstId = 1;
                        }

                        for (long i = baseId; i >= firstId; --i)
                        {
                            if (firstId <= 1) break;
                            // if entry is null , continue the loop with the next entry
                            if (_buffer[GetIndexFromId(i)] == null) continue;
                            if (_buffer[GetIndexFromId(i)].Cleared == true)
                            {
                                // we have to clear count entries before an id, so if an entry is null,decrement 
                                // first id as long as its is greater than the lowest entry in the buffer.
                                firstId--;
                                continue;
                            }
                        }

                        for (long i = firstId; i <= baseId; ++i)
                        {
                            //if an entry is null after being cleared by clear-history cmdlet,
                            //continue with the next entry
                            if (_buffer[GetIndexFromId(i)] == null || _buffer[GetIndexFromId(i)].Cleared == true)
                                continue;
                            entriesList.Add(_buffer[GetIndexFromId(i)].Clone());
                        }
                    }
                    else
                    { //get latest entries
                        // first id becomes the id +count no of entries from the end of the buffer
                        firstId = baseId + count - 1;
                        // if first id is more than the no of entries in the buffer, first id will be the last entry in the buffer
                        if (firstId >= _countEntriesAdded)
                        {
                            firstId = _countEntriesAdded;
                        }

                        for (long i = baseId; i <= firstId; i++)
                        {
                            if (firstId >= _countEntriesAdded) break;
                            // if entry is null , continue the loop with the next entry
                            if (_buffer[GetIndexFromId(i)] == null) continue;
                            if (_buffer[GetIndexFromId(i)].Cleared == true)
                            {
                                // we have to clear count entries before an id, so if an entry is null,increment  first id 
                                firstId++;
                                continue;
                            }
                        }

                        for (long i = firstId; i >= baseId; --i)
                        {
                            //if an entry is null after being cleared by clear-history cmdlet,
                            //continue with the next entry
                            if (_buffer[GetIndexFromId(i)] == null || _buffer[GetIndexFromId(i)].Cleared == true)
                                continue;
                            entriesList.Add(_buffer[GetIndexFromId(i)].Clone());
                        }
                    }
                }
                else
                {
                    //get entries for count,newest                                               

                    long index, SmallestID = 0;
                    //if we change the defaulthistory size and when no of entries exceed the size, then 
                    //we need to get the smallest entry in the buffer when we want to clear the oldest entry
                    //eg if size is 5 and then the entries can be 7,6,1,2,3
                    if (_capacity != DefaultHistorySize)
                        SmallestID = SmallestIDinBuffer();
                    if (!newest.IsPresent)
                    {
                        //get oldest count entries
                        index = 1;
                        if (_capacity != DefaultHistorySize)
                        {
                            if (_countEntriesAdded > _capacity)
                                index = SmallestID;
                        }
                        for (long i = count - 1; i >= 0;)
                        {
                            if (index > _countEntriesAdded) break;
                            if ((index <= 0 || GetIndexFromId(index) >= _buffer.Length) ||
                                (_buffer[GetIndexFromId(index)].Cleared == true))
                            {
                                index++; continue;
                            }
                            else
                            {
                                entriesList.Add(_buffer[GetIndexFromId(index)].Clone());
                                i--; index++;
                            }
                        }
                    }
                    else
                    {
                        index = _countEntriesAdded;//SmallestIDinBuffer

                        for (long i = count - 1; i >= 0;)
                        {
                            // if an entry is cleared continue to the next entry
                            if (_capacity != DefaultHistorySize)
                            {
                                if (_countEntriesAdded > _capacity)
                                {
                                    if (index < SmallestID)
                                        break;
                                }
                            }
                            if (index < 1) break;
                            if ((index <= 0 || GetIndexFromId(index) >= _buffer.Length) ||
                                (_buffer[GetIndexFromId(index)].Cleared == true))
                            { index--; continue; }
                            else
                            {
                                //clone the entry from the history buffer
                                entriesList.Add(_buffer[GetIndexFromId(index)].Clone());
                                i--; index--;
                            }
                        }
                    }
                }
                HistoryInfo[] entries = new HistoryInfo[entriesList.Count];
                entriesList.CopyTo(entries);
                return entries;
            }// end lock
        }// end function