codeview

A light and fast web based git repository viewer

Log | Files | Refs | README | LICENSE

commit 7d5c6def46d10105b2ed0e413eeb2dddccb5d8e6
parent 194ad32883683c868d3b4b72d364f3cfebd66864
Author: Abdul Rahim <abdul.rahim@myyahoo.com>
Date:   Mon, 30 Sep 2024 15:54:23 +0530

update

Diffstat:
D.get_lang.h.swp | 0
MMakefile | 2+-
Dget_lang.c | 87-------------------------------------------------------------------------------
Dget_lang.h | 1-
RcJSON.c -> include/cJSON.c | 0
RcJSON.h -> include/cJSON.h | 0
Rexts.json -> include/exts.json | 0
Ainclude/get_lang.c | 88+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ainclude/get_lang.h | 2++
Dreallocarray.o | 0
Dstagit | 0
Dstagit-index | 0
Dstagit-index.o | 0
Mstagit.c | 4++--
Dstagit.o | 0
Dstrlcat.o | 0
Dstrlcpy.o | 0
17 files changed, 93 insertions(+), 91 deletions(-)

diff --git a/.get_lang.h.swp b/.get_lang.h.swp Binary files differ. diff --git a/Makefile b/Makefile @@ -36,7 +36,7 @@ MAN1 = \ DOC = \ LICENSE\ README -HDR = compat.h cJSON.h +HDR = compat.h COMPATOBJ = \ reallocarray.o\ diff --git a/get_lang.c b/get_lang.c @@ -1,87 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include "cJSON.h" - -// Function to find language based on extension -const char* getLanguageByExtension(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; -} - -// Function to load and parse the JSON file -cJSON* loadLangMapFromFile(const char *filename) { - FILE *file = fopen(filename, "r"); - if (!file) { - printf("Unable to open file: %s\n", filename); - return NULL; - } - - // Get the file size - fseek(file, 0, SEEK_END); - long length = ftell(file); - fseek(file, 0, SEEK_SET); - - // Read the file into a string - char *data = (char*)malloc(length + 1); - if (!data) { - printf("Unable to allocate memory\n"); - fclose(file); - return NULL; - } - fread(data, 1, length, file); - data[length] = '\0'; // Null-terminate the string - - fclose(file); - - // Parse the JSON string - cJSON *langMap = cJSON_Parse(data); - free(data); // Free the memory allocated for the file content - - if (!langMap) { - printf("Error parsing JSON file\n"); - return NULL; - } - - return langMap; -} - -int main() { - const char *filename = "exts.json"; - - // Load the JSON data - cJSON *langMap = loadLangMapFromFile(filename); - if (!langMap) { - return 1; // Exit if there was an error loading the JSON - } - - // Find the language for a given file extension - const char *extensions[] = {"py", "rs", "c", "h", "js", "java", "cpp"}; - int len = 7; - - for (int i = 0; i < len; i++) { - const char *ext = extensions[i]; - const char *language = getLanguageByExtension(ext, langMap); - if (language) { - printf("The language for extension '%s' is: %s\n", ext, language); - } else { - printf("No language found for extension '%s'\n", ext); - } - } - - // Clean up and free the JSON structure - cJSON_Delete(langMap); - - return 0; -} diff --git a/get_lang.h b/get_lang.h @@ -1 +0,0 @@ -#include <stdio.h> diff --git a/cJSON.c b/include/cJSON.c diff --git a/cJSON.h b/include/cJSON.h diff --git a/exts.json b/include/exts.json diff --git a/include/get_lang.c b/include/get_lang.c @@ -0,0 +1,88 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <cJSON.h> + +// Function to find language based on extension +const char* getLanguageByExtension(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; +} + +// Function to load and parse the JSON file +cJSON* loadLangMapFromFile(const char *filename) { + FILE *file = fopen(filename, "r"); + if (!file) { + printf("Unable to open file: %s\n", filename); + return NULL; + } + + // Get the file size + fseek(file, 0, SEEK_END); + long length = ftell(file); + fseek(file, 0, SEEK_SET); + + // Read the file into a string + char *data = (char*)malloc(length + 1); + if (!data) { + printf("Unable to allocate memory\n"); + fclose(file); + return NULL; + } + fread(data, 1, length, file); + data[length] = '\0'; // Null-terminate the string + + fclose(file); + + // Parse the JSON string + cJSON *langMap = cJSON_Parse(data); + free(data); // Free the memory allocated for the file content + + if (!langMap) { + printf("Error parsing JSON file\n"); + return NULL; + } + + return langMap; +} + +int get_lang(const char* filename) +{ + const char *filename = "exts.json"; + + // Load the JSON data + cJSON *langMap = loadLangMapFromFile(filename); + if (!langMap) { + return 1; // Exit if there was an error loading the JSON + } + + // Find the language for a given file extension + const char *extensions[] = {"py", "rs", "c", "h", "js", "java", "cpp"}; + int len = 7; + + for (int i = 0; i < len; i++) { + const char *ext = extensions[i]; + const char *language = getLanguageByExtension(ext, langMap); + if (language) { + printf("The language for extension '%s' is: %s\n", ext, language); + } else { + printf("No language found for extension '%s'\n", ext); + } + } + + // Clean up and free the JSON structure + cJSON_Delete(langMap); + + return 0; +} diff --git a/include/get_lang.h b/include/get_lang.h @@ -0,0 +1,2 @@ +int get_lang(const char* filename); + diff --git a/reallocarray.o b/reallocarray.o Binary files differ. diff --git a/stagit b/stagit Binary files differ. diff --git a/stagit-index b/stagit-index Binary files differ. diff --git a/stagit-index.o b/stagit-index.o Binary files differ. diff --git a/stagit.c b/stagit.c @@ -15,7 +15,7 @@ #include <git2.h> #include "compat.h" -#include "get_lang.h" // for the get_lang function +#include <get_lang.h> // for the get_lang function #define LEN(s) (sizeof(s)/sizeof(*s)) @@ -595,7 +595,7 @@ writefooter(FILE *fp) * added parameter filename, so the <code class=?> can be determined */ size_t -writeblobhtml(FILE *fp, const git_blob *blob, char* filename) +writeblobhtml(FILE *fp, const git_blob *blob, const char* filename) { size_t n = 0, i, len, prev; const char *nfmt_old = "<a href=\"#l%zu\" class=\"line\" id=\"l%zu\">%7zu</a>"; //removed <code> here diff --git a/stagit.o b/stagit.o Binary files differ. diff --git a/strlcat.o b/strlcat.o Binary files differ. diff --git a/strlcpy.o b/strlcpy.o Binary files differ.