summaryrefslogtreecommitdiff
path: root/locks.c
diff options
context:
space:
mode:
authorhorchi <vdr@jwendel.de>2017-03-05 16:48:30 +0100
committerhorchi <vdr@jwendel.de>2017-03-05 16:48:30 +0100
commitbf558fd824c7ab8c794448f718b364ad403a706a (patch)
treee0ba2269c08ccc9c9bd9c336df06b1fa6fce5ec6 /locks.c
downloadvdr-plugin-pin-0.1.16.tar.gz
vdr-plugin-pin-0.1.16.tar.bz2
git init0.1.16
Diffstat (limited to 'locks.c')
-rw-r--r--locks.c274
1 files changed, 274 insertions, 0 deletions
diff --git a/locks.c b/locks.c
new file mode 100644
index 0000000..bda84c6
--- /dev/null
+++ b/locks.c
@@ -0,0 +1,274 @@
+/*
+ * pin.c: A plugin for the Video Disk Recorder
+ *
+ * See the README file for copyright information and how to reach the author.
+ *
+ * Date: 11.04.05 - 02.02.07, horchi
+ */
+
+//***************************************************************************
+// Includes
+//***************************************************************************
+
+#include "locks.h"
+#include "def.h"
+
+//***************************************************************************
+// Statics
+//***************************************************************************
+
+const char* cLockItem::searchModes[] =
+{
+ "regular expression",
+ "includes",
+ "exact",
+ "start with",
+ 0
+};
+
+//***************************************************************************
+// Object
+//***************************************************************************
+
+cLockItem::cLockItem(int aActive)
+{
+ name = 0;
+ title = 0;
+ active = aActive;
+ hasRange = yes;
+ start = 0;
+ end = 0;
+ pattern = 0;
+ searchMode = smExact;
+}
+
+cLockItem::cLockItem(const char* aName, int aActive, const char* aTitle)
+{
+ int res;
+ res = asprintf(&name, "%s", aName);
+ res = asprintf(&pattern, "%s", aName);
+ if (res < 0) ; // memory problems :o
+ active = aActive;
+ hasRange = yes;
+ title = 0;
+ start = DT::hhmm2Int("0000");
+ end = DT::hhmm2Int("2359");
+ searchMode = smExact;
+
+ if (aTitle)
+ res = asprintf(&title, "%s", aTitle);
+}
+
+cLockItem::~cLockItem()
+{
+ if (name) free(name);
+ if (title) free(title);
+ if (pattern) free(pattern);
+}
+
+//***************************************************************************
+// Name
+//***************************************************************************
+
+void cLockItem::SetName(const char* aName)
+{
+ int res;
+
+ if (!aName)
+ return;
+
+ if (name)
+ free(name);
+
+ res = asprintf(&name, "%s", aName);
+ if (res < 0) ; // memory problems :o
+}
+
+//***************************************************************************
+// Pattern
+//***************************************************************************
+
+void cLockItem::SetPattern(const char* aPattern)
+{
+ int res;
+
+ if (pattern)
+ free(pattern);
+
+ res = asprintf(&pattern, "%s", aPattern);
+ if (res < 0) ; // memory problems :o
+}
+
+//***************************************************************************
+// Title
+//***************************************************************************
+
+void cLockItem::SetTitle(const char* aTitle)
+{
+ int res;
+
+ if (!aTitle)
+ return;
+
+ if (title)
+ free(title);
+
+ res = asprintf(&title, "%s", aTitle);
+
+ if (res < 0) ; // memory problems :o
+}
+
+//***************************************************************************
+// Parse
+//***************************************************************************
+
+bool cLockItem::Parse(char* line)
+{
+ int fields;
+
+ char* aName = 0;
+ char* aActive = 0;
+ char* aStart = 0;
+ char* aEnd = 0;
+ char* aPattern = 0;
+ char* aSearchMode = 0;
+
+ fields = sscanf(line, "%a[^:]:%a[^:]:%a[^:]:%a[^:]:%a[^:]:%a[^\n]",
+ &aName, &aActive, &aStart, &aEnd,
+ &aPattern, &aSearchMode);
+
+ if (fields >= 4)
+ {
+ if (name) free(name);
+ name = aName;
+ active = strcmp(aActive, "yes") == 0;
+ start = DT::hhmm2Int(aStart);
+ end = DT::hhmm2Int(aEnd);
+ }
+
+ if (fields == 6)
+ {
+ if (pattern) free(pattern);
+ pattern = aPattern;
+
+ for (int i = 0; i < smCount; i++)
+ {
+ if (strcmp(searchModes[i], aSearchMode) == 0)
+ {
+ searchMode = i;
+ break;
+ }
+ }
+ }
+ else if (fields == 4)
+ {
+ SetPattern(name);
+ searchMode = smExact;
+ }
+
+ if (aActive) free(aActive);
+ if (aStart) free(aStart);
+ if (aEnd) free(aEnd);
+ if (aSearchMode) free(aSearchMode);
+
+ return fields == 4 || fields == 6;
+}
+
+//***************************************************************************
+// Save
+//***************************************************************************
+
+bool cLockItem::Save(FILE* file)
+{
+ char aStart[sizeHHMM+TB];
+ char aEnd[sizeHHMM+TB];
+
+ DT::int2Hhmm(start, aStart);
+ DT::int2Hhmm(end, aEnd);
+
+ // Format: "<name>:<active>:<start>:<end>:<pattern>:<searchMode>"
+
+ return fprintf(file, "%s:%s:%s:%s:%s:%s\n",
+ name,
+ active ? "yes" : "no",
+ aStart, aEnd, pattern,
+ searchModes[searchMode]) > 0;
+}
+
+//***************************************************************************
+// Locked
+//***************************************************************************
+
+int cLockItem::Locked(long startTime)
+{
+ long sec;
+ char ct[sizeHHMM+TB];
+ long theTime;
+
+ if (startTime)
+ theTime = startTime;
+ else
+ theTime = DT::lNow(); // get now in seconds
+
+ // get seconds from today 00:00 in actual timezone
+
+ tell(eloDebug, "Checking protection at '%s'", DT::int2Hhmm(theTime, ct));
+
+ sec = DT::hhmm2Int(DT::int2Hhmm(theTime, ct));
+
+ if (sec >= start && sec <= end && active)
+ return yes;
+
+ return no;
+}
+
+//***************************************************************************
+// Match Pattern
+//***************************************************************************
+
+bool cLockItem::MatchPattern(const char* aName)
+{
+ if (searchMode == smRegexp)
+ return rep(aName, pattern) == success;
+ else if (searchMode == smExact)
+ return strcmp(pattern, aName) == 0;
+ else if (searchMode == smInclude)
+ return strstr(aName, pattern) > 0;
+ else if (searchMode == smStartWith)
+ return strstr(aName, pattern) == aName;
+
+ return no;
+}
+
+//***************************************************************************
+// Has Name
+//***************************************************************************
+
+bool cLockItem::HasName(const char* aName)
+{
+ return strcmp(name, aName) == 0;
+}
+
+//***************************************************************************
+// Class cLockItems
+//***************************************************************************
+//***************************************************************************
+// Locked
+//***************************************************************************
+
+int cLockItems::Locked(const char* aName, long startTime)
+{
+ cLockItem* item;
+
+ if (!aName)
+ return no;
+
+ for (item = First(); item; item = Next(item))
+ {
+ if (item->MatchPattern(aName))
+ if (item->Locked(startTime))
+ return yes;
+ }
+
+ return no;
+}