summaryrefslogtreecommitdiffstats
path: root/vcs/clearcase/clearcasemanipulator.cpp
blob: 65f05c4ed361a917b4dc8aac208a32dab517e62b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// C++ Implementation: ClearcaseManipulator
//
// Description:
//
//
// Author: KDevelop Authors <kdevelop-devel@kdevelop.org>, (C) 2005
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "config.h"
#include "clearcasemanipulator.h"

#include <kprocess.h>

#include <tqregexp.h>
#include <tqstring.h>

#include <stdlib.h>
#include <libgen.h>
#include <errno.h>
#ifdef __NetBSD__
#include <sys/param.h>
#endif

#if !defined( HAVE_GETLINE )
//this function is taken from GNU libc
int getline(char **lineptr, size_t *n, FILE *stream)
{
    static char line[256];
    char *ptr;
    unsigned int len;

   if (lineptr == NULL || n == NULL)
   {
      errno = EINVAL;
      return -1;
   }

   if (ferror (stream))
      return -1;

   if (feof(stream))
      return -1;

   fgets(line,256,stream);

   ptr = strchr(line,'\n');
   if (ptr)
      *ptr = '\0';

   len = strlen(line);

   if ((len+1) < 256)
   {
      ptr = (char*)realloc(*lineptr, 256);
      if (ptr == NULL)
         return(-1);
      *lineptr = ptr;
      *n = 256;
   }

   strcpy(*lineptr,line);
   return(len);
}
#endif

const char ClearcaseManipulator::CT_DESC_SEPARATOR = ';';


ClearcaseManipulator::ClearcaseManipulator()
{
}


ClearcaseManipulator::~ClearcaseManipulator()
{}


bool ClearcaseManipulator::isCCRepository( const TQString & directory ) {
  TQString cmd;
  cmd = "cd " + directory + " && cleartool pwv -root";
  if ( system(cmd.ascii()) == 0 ) return true;

  return false;
}

VCSFileInfoMap* ClearcaseManipulator::retreiveFilesInfos(const TQString& directory) {


  VCSFileInfoMap* fileInfoMap = new VCSFileInfoMap();

  char CCcommand[1024];
  sprintf(CCcommand, "cleartool desc -fmt \"%%m;%%En;%%Rf;%%Sn;%%PVn\\n\" %s/*", directory.ascii());
  FILE* outputFile = popen(CCcommand, "r");

  char* line = NULL;
  size_t numRead;
  while (!feof(outputFile)) {
    getline(&line,&numRead,outputFile);

    if (numRead > 0) {
      int pos = 0;
      int lastPos = -1;

      TQStringList outputList;
      outputList = outputList.split(CT_DESC_SEPARATOR, TQString(line), true );
      outputList[Name] = TQString(basename((char*)outputList[Name].ascii()));

      VCSFileInfo::FileState state;
      if (outputList[ClearcaseManipulator::State] == "unreserved" || outputList[ClearcaseManipulator::State] == "reserved") {
	state = VCSFileInfo::Modified;
      }
      else if (outputList[ClearcaseManipulator::State] == "") {
	state = VCSFileInfo::Uptodate;
      }
      else {
	state = VCSFileInfo::Unknown;
      }


      (*fileInfoMap)[outputList[ClearcaseManipulator::Name]] = VCSFileInfo(outputList[ClearcaseManipulator::Name], outputList[ClearcaseManipulator::Version], outputList[ClearcaseManipulator::RepositoryVersion], state);
    }
  }

  pclose(outputFile);

  return fileInfoMap;
}