commit aa03c6f161767a09a88e441bbe6b0e030f3a7f53
parent 7d5c6def46d10105b2ed0e413eeb2dddccb5d8e6
Author: Abdul Rahim <abdul.rahim@myyahoo.com>
Date: Mon, 30 Sep 2024 19:27:30 +0530
checkpoint
Diffstat:
A | get_lang.c | | | 197 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 197 insertions(+), 0 deletions(-)
diff --git a/get_lang.c b/get_lang.c
@@ -0,0 +1,197 @@
+/*
+ * Author: Abdul Rahim(C) 2024
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include "cJSON.h"
+
+char* to_lower(const char *upper)
+{
+ char* str = (char *) malloc(strlen(upper) + 1 );
+ if (!str)
+ return NULL;
+
+ int i;
+ for (i = 0; upper[i] ; i++) {
+ str[i] = tolower(upper[i]);
+ }
+ str[i] = '\0';
+ printf("lowercased: %s\n", str);
+ return str;
+}
+
+char *get_file_extension(const char *filename_arg)
+{
+ char *filename = to_lower(filename_arg);
+ if (!filename)
+ strcpy(filename, filename_arg);
+ char *dot = strrchr(filename, '.');
+ if (dot && dot != filename) {
+ /* dot is the pointer to last occurence of '.'
+ */
+ return dot+1;
+ }
+ else {
+ return NULL;
+ }
+}
+
+const char* get_language_by_extension(const char *extension, cJSON *langMap)
+{
+ cJSON *languageItem;
+ cJSON_ArrayForEach(languageItem, langMap) {
+ const char *language = languageItem->string;
+ cJSON *extensions = cJSON_GetObjectItemCaseSensitive(langMap, language);
+ cJSON *extItem;
+ cJSON_ArrayForEach(extItem, extensions) {
+ if (strcmp(extension, extItem->valuestring) == 0)
+ return language;
+ }
+ }
+
+ return NULL;
+}
+
+cJSON* loadLangMapFromFile()
+{
+ /*
+ * c string that contains JSON is in json_string
+ */
+ cJSON *langMap = cJSON_Parse(json_string);
+ if (!langMap) {
+ printf("string_json is maybe not in scope. Please ensure compiling with cJSON.h\n");
+ return NULL;
+ }
+
+ return langMap;
+}
+
+const char *get_lang(const char* filename)
+{
+ cJSON *langMap = loadLangMapFromFile();
+ if (!langMap)
+ return NULL;
+
+ char *extension = get_file_extension(filename);
+ if (!extension) {
+ cJSON_Delete(langMap);
+ return NULL;
+ }
+
+ const char *language = get_language_by_extension(extension, langMap);
+ if (language) {
+ cJSON_Delete(langMap);
+ return language;
+ } else {
+ cJSON_Delete(langMap);
+ return NULL;
+ }
+}
+
+int main()
+{
+ // Test Case 1: Standard Extensions
+ printf("Input: program.c\n");
+ get_lang("program.c"); // Expected output: C
+printf("\n");
+
+ printf("Input: script.py\n");
+ get_lang("script.py"); // Expected output: Python
+printf("\n");
+
+ printf("Input: document.java\n");
+ get_lang("document.java"); // Expected output: Java
+printf("\n");
+
+ printf("Input: webpage.html\n");
+ get_lang("webpage.html"); // Expected output: HTML
+printf("\n");
+
+ // Test Case 2: No Extension
+ printf("Input: file_without_extension\n");
+ get_lang("file_without_extension"); // Expected output: No language found
+printf("\n");
+
+ printf("Input: path/to/directory/file\n");
+ get_lang("path/to/directory/file"); // Expected output: No language found
+printf("\n");
+
+ // Test Case 3: Dot Files (Hidden Files on UNIX Systems)
+ printf("Input: .hiddenfile\n");
+ get_lang(".hiddenfile"); // Expected output: No language found
+printf("\n");
+
+ printf("Input: /path/.config\n");
+ get_lang("/path/.config"); // Expected output: No language found
+printf("\n");
+
+ // Test Case 4: Files with Multiple Dots
+ printf("Input: archive.tar.gz\n");
+ get_lang("archive.tar.gz"); // Expected output: GZip (or Tar if multiple extensions are supported)
+printf("\n");
+
+ printf("Input: backup.data.sql\n");
+ get_lang("backup.data.sql"); // Expected output: SQL
+printf("\n");
+
+ printf("Input: path/to/file.min.js\n");
+ get_lang("path/to/file.min.js"); // Expected output: JavaScript
+printf("\n");
+
+ // Test Case 5: Files with Double Dots
+ printf("Input: file..py\n");
+ get_lang("file..py"); // Expected output: Python
+printf("\n");
+
+ printf("Input: weird..ext..c\n");
+ get_lang("weird..ext..c"); // Expected output: C
+printf("\n");
+
+ // Test Case 6: Trailing Dots (No Extension After)
+ printf("Input: file_with_trailing_dot.\n");
+ get_lang("file_with_trailing_dot."); // Expected output: No language found
+printf("\n");
+
+ printf("Input: file_with..trailing_dots..\n");
+ get_lang("file_with..trailing_dots.."); // Expected output: No language found
+printf("\n");
+
+ // Test Case 7: Case Insensitivity
+ printf("Input: script.PY\n");
+ get_lang("script.PY"); // Expected output: Python
+printf("\n");
+
+ printf("Input: program.JAVA\n");
+ get_lang("program.JAVA"); // Expected output: Java
+printf("\n");
+
+ printf("Input: index.HTML\n");
+ get_lang("index.HTML"); // Expected output: HTML
+printf("\n");
+
+ // Test Case 8: Non-Programming Extensions
+ printf("Input: photo.jpg\n");
+ get_lang("photo.jpg"); // Expected output: No language found
+printf("\n");
+
+ printf("Input: document.pdf\n");
+ get_lang("document.pdf"); // Expected output: No language found
+printf("\n");
+
+ // Test Case 9: Invalid File Path
+ printf("Input: /invalid/path/file\n");
+ get_lang("/invalid/path/file"); // Expected output: No language found
+printf("\n");
+
+ // Test Case 10: Language Map File Missing
+ // Rename or remove exts.json and run the program
+ printf("Input: file.py\n");
+ get_lang("file.py"); // Expected output: Unable to open file: exts.json
+printf("\n");
+
+ return 0;
+}
+