Kajetan Johannes Hammerle пре 11 месеци
родитељ
комит
754220e89f
3 измењених фајлова са 29 додато и 24 уклоњено
  1. 6 0
      include/core/ReadLine.h
  2. 18 19
      src/ReadLine.c
  3. 5 5
      test/modules/ReadLineTests.c

+ 6 - 0
include/core/ReadLine.h

@@ -7,4 +7,10 @@ bool coreStartReadLine(void);
 bool coreReadLine(char* buffer, size_t n);
 void coreStopReadLine(void);
 
+#ifdef IMPORT_CORE
+#define startReadLine coreStartReadLine
+#define readLine coreReadLine
+#define stopReadLine coreStopReadLine
+#endif
+
 #endif

+ 18 - 19
src/ReadLine.c

@@ -27,7 +27,7 @@ static atomic_bool running = true;
 static thrd_t readThread = {0};
 
 static struct termios original;
-static CoreQueue buffer = {0};
+static Queue buffer = {0};
 static ConsoleLine currentBuffer = {0};
 static int move = 0;
 static int cursorMove = 0;
@@ -40,14 +40,13 @@ static int historyLength = 0;
 
 static void getAttributes(struct termios* t) {
     if(tcgetattr(STDIN_FILENO, t)) {
-        CORE_LOG_WARNING("cannot get terminal attributes");
+        LOG_WARNING("cannot get terminal attributes");
     }
 }
 
 static void restoreAttributes() {
     if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &original)) {
-        CORE_LOG_WARNING("cannot restore terminal attributes: %s",
-                         strerror(errno));
+        LOG_WARNING("cannot restore terminal attributes: %s", strerror(errno));
     }
 }
 
@@ -103,20 +102,20 @@ static void addToHistory() {
 
 static void lock() {
     if(mtx_lock(&bufferMutex) != thrd_success || MUTEX_LOCK_FAIL) {
-        CORE_LOG_WARNING("could not lock buffer mutex");
+        LOG_WARNING("could not lock buffer mutex");
     }
 }
 
 static void unlock() {
     if(mtx_unlock(&bufferMutex) != thrd_success || MUTEX_UNLOCK_FAIL) {
-        CORE_LOG_WARNING("could not unlock buffer mutex");
+        LOG_WARNING("could not unlock buffer mutex");
     }
 }
 
 static void addLine() {
     addToHistory();
     lock();
-    corePushQueueData(&buffer, &currentBuffer);
+    pushQueueData(&buffer, &currentBuffer);
     unlock();
     clear();
 }
@@ -247,7 +246,7 @@ static int loop(void* data) {
     return 0;
 }
 
-bool coreStartReadLine(void) {
+bool startReadLine(void) {
     // https://viewsourcecode.org/snaptoken/kilo/02.enteringRawMode.html
     getAttributes(&original);
     struct termios raw = original;
@@ -257,40 +256,40 @@ bool coreStartReadLine(void) {
     raw.c_cc[VTIME] = 0;
 
     if(tcsetattr(STDIN_FILENO, TCSANOW, &raw)) {
-        CORE_LOG_WARNING("cannot set terminal attributes");
+        LOG_WARNING("cannot set terminal attributes");
     }
 
-    coreInitQueue(&buffer, 10, sizeof(ConsoleLine));
+    initQueue(&buffer, 10, sizeof(ConsoleLine));
     atomic_store(&running, true);
     if(MUTEX_INIT_FAIL || mtx_init(&bufferMutex, mtx_plain) != thrd_success) {
-        CORE_LOG_ERROR("cannot init buffer mutex");
-        coreStopReadLine();
+        LOG_ERROR("cannot init buffer mutex");
+        stopReadLine();
         return true;
     } else if(THREAD_INIT_FAIL ||
               thrd_create(&readThread, loop, nullptr) != thrd_success) {
-        CORE_LOG_ERROR("cannot start read thread");
-        coreStopReadLine();
+        LOG_ERROR("cannot start read thread");
+        stopReadLine();
         return true;
     }
     return false;
 }
 
-bool coreReadLine(char* buffer_, size_t n) {
+bool readLine(char* buffer_, size_t n) {
     if(buffer.length == 0) {
         return false;
     }
     lock();
-    ConsoleLine* line = coreGetQueueIndex(&buffer, 0);
+    ConsoleLine* line = getQueueIndex(&buffer, 0);
     snprintf(buffer_, n, "%s", line->data);
-    corePopQueueData(&buffer);
+    popQueueData(&buffer);
     unlock();
     return true;
 }
 
-void coreStopReadLine() {
+void stopReadLine() {
     atomic_store(&running, false);
     thrd_join(readThread, nullptr);
     restoreAttributes();
-    coreDestroyQueue(&buffer);
+    destroyQueue(&buffer);
     mtx_destroy(&bufferMutex);
 }

+ 5 - 5
test/modules/ReadLineTests.c

@@ -13,7 +13,7 @@ static void sleepMillis(int millis) {
 static void testString(int line, const char* s) {
     char buffer[256];
     for(int i = 0; i < 200; i++) {
-        if(coreReadLine(buffer, sizeof(buffer))) {
+        if(readLine(buffer, sizeof(buffer))) {
             break;
         }
         sleepMillis(10);
@@ -39,19 +39,19 @@ static void testString(int line, const char* s) {
 void testReadLine(void) {
 #ifdef ERROR_SIMULATOR
     failMutexInit = true;
-    if(!CORE_TEST_TRUE(coreStartReadLine())) {
+    if(!TEST_TRUE(startReadLine())) {
         return;
     }
     failMutexInit = false;
     failThreadInit = true;
-    if(!CORE_TEST_TRUE(coreStartReadLine())) {
+    if(!TEST_TRUE(startReadLine())) {
         return;
     }
     failThreadInit = false;
     failMutexLock = true;
     failMutexUnlock = true;
 #endif
-    if(!CORE_TEST_FALSE(coreStartReadLine())) {
+    if(!TEST_FALSE(startReadLine())) {
         return;
     }
     testString(__LINE__, "wusi");
@@ -77,5 +77,5 @@ void testReadLine(void) {
         "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbb");
     testString(__LINE__, "abäo");
     testString(__LINE__, "bäöo");
-    coreStopReadLine();
+    stopReadLine();
 }