File size: 3,230 Bytes
806d4b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

#define TIME_FILENAME   "~/TimeClock/TimeFile.DATA.Z"

#define TET_EOF         0
#define TET_PUNCH_IN    1
#define TET_PUNCH_OUT   2

class TimeEntry
{
  TimeEntry *next,*last;
  U8 type;
  CDate datetime;
  U8 *desc;
};

U0 TimeFileRead(TimeEntry *header)
{
  U8 *buf,*ptr;
  I64 type;
  TimeEntry *tmpt;
  buf=FileRead(TIME_FILENAME);
  if (!buf)
    buf=CAlloc(1);
  QueInit(header);
  ptr=buf;
  while (type=*ptr++) {
    tmpt=CAlloc(sizeof(TimeEntry));
    tmpt->type=type;
    tmpt->datetime=*ptr(CDate *)++;
    tmpt->desc=StrNew(ptr);
    ptr+=StrLen(ptr)+1;
    QueIns(tmpt,header->last);
  }
  Free(buf);
}

U0 TimeFileWrite(TimeEntry *header)
{
  U8 *buf,*ptr;
  TimeEntry *tmpt;
  I64 size=1; //for EOF

  tmpt=header->next;
  while (tmpt!=header) {
    size+=sizeof(U8)+sizeof(CDate)+StrLen(tmpt->desc)+1;
    tmpt=tmpt->next;
  }
  buf=MAlloc(size);

  ptr=buf;
  tmpt=header->next;
  while (tmpt!=header) {
    *ptr++=tmpt->type;
    *ptr(CDate *)++=tmpt->datetime;
    StrCpy(ptr,tmpt->desc);
    ptr+=StrLen(tmpt->desc)+1;
    tmpt=tmpt->next;
  }
  *ptr=TET_EOF;
  FileWrite(TIME_FILENAME,buf,size);
  Free(buf);
}

U0 TimeEntriesDel(TimeEntry *header)
{
  TimeEntry *tmpt=header->next,*tmpt1;
  while (tmpt!=header) {
    tmpt1=tmpt->next;
    Free(tmpt->desc);
    Free(tmpt);
    tmpt=tmpt1;
  }
  Free(header);
}

public Bool TimeRep(TimeEntry **_header=NULL)
{
  Bool is_in=FALSE,first=TRUE;
  I64 week,cur_week=-1,week_total;
  TimeEntry *tmpt,*header=MAlloc(sizeof(TimeEntry));
  if (_header) *_header=header;
  TimeFileRead(header);
  tmpt=header->next;
  while (tmpt!=header) {
    week=tmpt->datetime.date/7;  //TODO
    if (week!=cur_week) {
      if (!first) {
        if (is_in)
          week_total+=Now;
        "Week Total:%T\n",week_total-local_time_offset;
      } else
        first=FALSE;
      cur_week=week;
      week_total=0;
    }
    if (tmpt->type==TET_PUNCH_IN) {
      "$RED$IN ";
      if (!is_in)
        week_total-=tmpt->datetime;
      is_in=TRUE;
    } else {
      "$RED$OUT";
      if (is_in)
        week_total+=tmpt->datetime;
      is_in=FALSE;
    }
    " %D %T:$FG$\n%s\n",tmpt->datetime,tmpt->datetime,tmpt->desc;
    tmpt=tmpt->next;
  }
  if (is_in)
    week_total+=Now;
  "$RED$Week Total:%T$FG$\n",week_total-local_time_offset;
  if (!_header)
    TimeEntriesDel(header);
  return is_in;
}

public U0 PunchOut()
{
  TimeEntry *tmpt,*header;
  if (!TimeRep(&header))
    "$BK,1$Already Punched-Out$BK,0$\n";
  else {
    tmpt=MAlloc(sizeof(TimeEntry));
    tmpt->type=TET_PUNCH_OUT;
    tmpt->datetime=Now;
    "\nEnter Description.\nPress <ESC> when done.\n";
    if (!(tmpt->desc=GetStr(,,GSF_WITH_NEW_LINE)))
      tmpt->desc=CAlloc(1);
    QueIns(tmpt,header->last);
    TimeFileWrite(header);
  }
  TimeEntriesDel(header);
}

public U0 PunchIn()
{
  TimeEntry *tmpt,*header;
  if (TimeRep(&header))
    "$BK,1$Already Punched-In$BK,0$\n";
  else {
    tmpt=MAlloc(sizeof(TimeEntry));
    tmpt->type=TET_PUNCH_IN;
    tmpt->datetime=Now;
    "\nEnter Description.\nPress <ESC> when done.\n";
    if (!(tmpt->desc=GetStr(,,GSF_WITH_NEW_LINE)))
      tmpt->desc=CAlloc(1);
    QueIns(tmpt,header->last);
    TimeFileWrite(header);
  }
  TimeEntriesDel(header);
}