CoolFace
Datasetpublic

hschumann2/TempleOS-Source-Code

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes838downloads
TimeClk.txt153 linesDownload Raw Back to TimeClock
1 2#define TIME_FILENAME   "~/TimeClock/TimeFile.DATA.Z"3 4#define TET_EOF         05#define TET_PUNCH_IN    16#define TET_PUNCH_OUT   27 8class TimeEntry9{10  TimeEntry *next,*last;11  U8 type;12  CDate datetime;13  U8 *desc;14};15 16U0 TimeFileRead(TimeEntry *header)17{18  U8 *buf,*ptr;19  I64 type;20  TimeEntry *tmpt;21  buf=FileRead(TIME_FILENAME);22  if (!buf)23    buf=CAlloc(1);24  QueInit(header);25  ptr=buf;26  while (type=*ptr++) {27    tmpt=CAlloc(sizeof(TimeEntry));28    tmpt->type=type;29    tmpt->datetime=*ptr(CDate *)++;30    tmpt->desc=StrNew(ptr);31    ptr+=StrLen(ptr)+1;32    QueIns(tmpt,header->last);33  }34  Free(buf);35}36 37U0 TimeFileWrite(TimeEntry *header)38{39  U8 *buf,*ptr;40  TimeEntry *tmpt;41  I64 size=1; //for EOF42 43  tmpt=header->next;44  while (tmpt!=header) {45    size+=sizeof(U8)+sizeof(CDate)+StrLen(tmpt->desc)+1;46    tmpt=tmpt->next;47  }48  buf=MAlloc(size);49 50  ptr=buf;51  tmpt=header->next;52  while (tmpt!=header) {53    *ptr++=tmpt->type;54    *ptr(CDate *)++=tmpt->datetime;55    StrCpy(ptr,tmpt->desc);56    ptr+=StrLen(tmpt->desc)+1;57    tmpt=tmpt->next;58  }59  *ptr=TET_EOF;60  FileWrite(TIME_FILENAME,buf,size);61  Free(buf);62}63 64U0 TimeEntriesDel(TimeEntry *header)65{66  TimeEntry *tmpt=header->next,*tmpt1;67  while (tmpt!=header) {68    tmpt1=tmpt->next;69    Free(tmpt->desc);70    Free(tmpt);71    tmpt=tmpt1;72  }73  Free(header);74}75 76public Bool TimeRep(TimeEntry **_header=NULL)77{78  Bool is_in=FALSE,first=TRUE;79  I64 week,cur_week=-1,week_total;80  TimeEntry *tmpt,*header=MAlloc(sizeof(TimeEntry));81  if (_header) *_header=header;82  TimeFileRead(header);83  tmpt=header->next;84  while (tmpt!=header) {85    week=tmpt->datetime.date/7;  //TODO86    if (week!=cur_week) {87      if (!first) {88        if (is_in)89          week_total+=Now;90        "Week Total:%T\n",week_total-local_time_offset;91      } else92        first=FALSE;93      cur_week=week;94      week_total=0;95    }96    if (tmpt->type==TET_PUNCH_IN) {97      "$RED$IN ";98      if (!is_in)99        week_total-=tmpt->datetime;100      is_in=TRUE;101    } else {102      "$RED$OUT";103      if (is_in)104        week_total+=tmpt->datetime;105      is_in=FALSE;106    }107    " %D %T:$FG$\n%s\n",tmpt->datetime,tmpt->datetime,tmpt->desc;108    tmpt=tmpt->next;109  }110  if (is_in)111    week_total+=Now;112  "$RED$Week Total:%T$FG$\n",week_total-local_time_offset;113  if (!_header)114    TimeEntriesDel(header);115  return is_in;116}117 118public U0 PunchOut()119{120  TimeEntry *tmpt,*header;121  if (!TimeRep(&header))122    "$BK,1$Already Punched-Out$BK,0$\n";123  else {124    tmpt=MAlloc(sizeof(TimeEntry));125    tmpt->type=TET_PUNCH_OUT;126    tmpt->datetime=Now;127    "\nEnter Description.\nPress <ESC> when done.\n";128    if (!(tmpt->desc=GetStr(,,GSF_WITH_NEW_LINE)))129      tmpt->desc=CAlloc(1);130    QueIns(tmpt,header->last);131    TimeFileWrite(header);132  }133  TimeEntriesDel(header);134}135 136public U0 PunchIn()137{138  TimeEntry *tmpt,*header;139  if (TimeRep(&header))140    "$BK,1$Already Punched-In$BK,0$\n";141  else {142    tmpt=MAlloc(sizeof(TimeEntry));143    tmpt->type=TET_PUNCH_IN;144    tmpt->datetime=Now;145    "\nEnter Description.\nPress <ESC> when done.\n";146    if (!(tmpt->desc=GetStr(,,GSF_WITH_NEW_LINE)))147      tmpt->desc=CAlloc(1);148    QueIns(tmpt,header->last);149    TimeFileWrite(header);150  }151  TimeEntriesDel(header);152}153