// Entry.cs
// Copyright (C) 2001 Mike Krueger
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// As a special exception, if you link this library with other files to
// produce an executable, this library does not by itself cause the
// resulting executable to be covered by the GNU General Public License.
// This exception does not however invalidate any other reasons why the
// executable file might be covered by the GNU General Public License.
using System;
using System.Collections;
using System.IO;
using System.Globalization;
namespace CVS.Misc{
public class Entry
{
public bool NewEntry = true;
bool isDir = false;
string name = null;
string revision = "1.1.1.1";
DateTime timestamp = DateTime.Now;
string conflict = null;
string options = null;
string tag = null;
string date = null;
public DateTime TimeStamp {
get {
return timestamp;
}
set {
timestamp = value;
}
}
public string Conflict {
get {
return conflict;
}
set {
conflict = value;
}
}
public string Date {
get {
return date;
}
set {
date = value;
SetTimeStamp();
}
}
public string Tag {
get {
return tag;
}
set {
tag = value;
}
}
public string Options {
get {
return options;
}
set {
options = value;
}
}
public string Revision {
get {
return revision;
}
set {
revision = value;
}
}
public string Name {
get {
return name;
}
set {
name = value;
}
}
public bool IsDirectory {
get {
return isDir;
}
set {
isDir = value;
}
}
public bool IsBinaryFile {
get {
return options == "-kb";
}
set {
options = value ? "-kb" : null;
}
}
public Entry()
{
}
public Entry(string line)
{
Parse(line);
NewEntry = false;
}
public void SetTimeStamp()
{
if (date != null && date.Length > 0) {
Console.WriteLine(">"+date+"<");
try {
timestamp = DateTime.ParseExact(date, "dd MMM yyyy HH':'mm':'ss '-0000'", DateTimeFormatInfo.InvariantInfo);
} catch (Exception) {
try {
timestamp = DateTime.ParseExact("0" + date, "dd MMM yyyy HH':'mm':'ss '-0000'", DateTimeFormatInfo.InvariantInfo);
} catch (Exception) {
timestamp = DateTime.ParseExact(date, "ddd MMM dd HH':'mm':'ss yyyy", DateTimeFormatInfo.InvariantInfo);
}
}
}
}
public void Parse(string line)
{
if (line.StartsWith("D/")) {
this.isDir = true;
line = line.Substring(1);
}
string[] tokens = line.Split( new char[] { '/' });
if (tokens.Length < 6) {
throw new ArgumentException( "not enough tokens in entry line (#" + tokens.Length + ")\n" + line);
}
name = tokens[1];
revision = tokens[2];
date = tokens[3];
int conflictIndex = date.IndexOf('+');
if (conflictIndex > 0) {
Conflict = date.Substring(conflictIndex + 1);
date = date.Substring(0, conflictIndex);
}
SetTimeStamp();
options = tokens[4];
tag = tokens[5];
}
/// <summary>
/// This class gets all entries in from the path <code>path</code>.
/// (but not it's subpaths)
/// </summary>
/// <remarks>
/// Note that the path/CVS directory must exists and this directory
/// must contain an the file Entries for the function to work.
/// </remarks>
/// <returns>
/// null if no entry could be read.
/// </returns>
public static Entry[] RetrieveEntries(string path)
{
if (File.Exists(path + "\\CVS\\Entries")) {
ArrayList entries = new ArrayList();
StreamReader sr = File.OpenText(path + "\\CVS\\Entries");
while (true) {
string line = sr.ReadLine();
if (line == null) {
break;
}
if (line.Length > 1) {
entries.Add(new Entry(line));
}
}
sr.Close();
if (File.Exists(path + "\\CVS\\Entries.Log")) {
sr = File.OpenText(path + "\\CVS\\Entries.Log");
while (true) {
string line = sr.ReadLine();
if (line == null) {
break;
}
if (line.Length > 1) {
switch (line[0]) {
case 'A': // Add file
entries.Add(new Entry(line.Substring(2)));
break;
case 'R': // Remove entry
Entry removeMe = new Entry(line.Substring(2));
Entry removeObject = null;
foreach (Entry entry in entries) {
if (removeMe.ToString() == entry.ToString()) {
removeObject = entry;
break;
}
}
if (removeObject != null) {
entries.Remove(removeObject);
}
break;
default: // other chars are silently ignored (specified behaviour)
break;
}
}
}
}
sr.Close();
return (Entry[])entries.ToArray(typeof(Entry));
}
return null;
}
public override string ToString()
{
string str = "";
if (isDir) {
str += "D";
}
str += "/";
if (name != null) {
str += name + "/";
if (revision != null && !isDir) {
str += revision;
}
str += "/";
if (date != null) {
str += timestamp.ToString("ddd MMM dd HH':'mm':'ss yyyy", DateTimeFormatInfo.InvariantInfo);
}
if (conflict != null) {
str += "+" + conflict;
}
str += "/";
if (options != null) {
str += options;
}
str += "/";
if (tag != null) {
str += tag;
} else if (date != null) {
str += date;
}
}
return str;
}
}
}
|