summaryrefslogtreecommitdiff
path: root/visibility.c
blob: 35bdd9736b89121a467159aa5d4f654e536cca9d (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
/*
 * visibility.c: Visibility classes for duplicates plugin.
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */

#include "visibility.h"

// --- cVisibility -----------------------------------------------------------

#ifdef DEBUG_VISIBILITY
int cVisibility::getCount = 0;
int cVisibility::readCount = 0;
int cVisibility::accessCount = 0;
#endif

cVisibility::cVisibility(const char *fileName) {
  hiddenFileName = AddDirectory(fileName, "duplicates.hidden");
  visibility = UNKNOWN;
}

cVisibility::cVisibility(const cVisibility &Visibility) :
  hiddenFileName(Visibility.hiddenFileName),
  visibility(Visibility.visibility) {}

eVisibility cVisibility::Get(void) {
#ifdef DEBUG_VISIBILITY
  getCount++;
#endif
  return visibility;
}

void cVisibility::Set(bool visible) {
  visibility = visible ? VISIBLE : HIDDEN;
}

eVisibility cVisibility::Read(void) {
#ifdef DEBUG_VISIBILITY
  readCount++;
#endif
  if (visibility == UNKNOWN) {
#ifdef DEBUG_VISIBILITY
    accessCount++;
#endif
    visibility = access(hiddenFileName, F_OK) == 0 ? HIDDEN : VISIBLE;
  }
  return visibility;
}

bool cVisibility::Write(bool visible) {
  if (visible) {
    if (remove(hiddenFileName) == 0) {
      visibility = VISIBLE;
      return true;
    }
  } else {
    FILE *f = fopen(hiddenFileName, "w");
    if (f) {
      fclose(f);
      visibility = HIDDEN;
      return true;
    }
  }
  return false;
}

#ifdef DEBUG_VISIBILITY
void cVisibility::ClearCounters(void) {
  getCount = 0;
  readCount = 0;
  accessCount = 0;
}
#endif