-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.fivesixteensvgapi.c
49 lines (47 loc) · 2.59 KB
/
ai.fivesixteensvgapi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// I can write a simple C API to integrate functionality for files containing the string ‘fivesixteensvg’ in the filename and allow it to link Javascript. The API will consist of two functions: one to scan a directory for files matching the pattern, and another to execute a Javascript file using a library such as Duktape. Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include "duktape.h"
// A function to scan a directory for files containing the string 'fivesixteensvg' in the filename
// Returns a pointer to an array of strings with the file names, or NULL if none found
// The caller is responsible for freeing the memory allocated by this function
char **scan_files(const char *dir) {
char **files = NULL; // Initialize the array of file names
int count = 0; // Initialize the count of file names
DIR *d = opendir(dir); // Open the directory
if (d) { // If the directory is opened successfully
struct dirent *entry; // Declare a pointer to a directory entry
while ((entry = readdir(d)) != NULL) { // While there are entries to read
if (entry->d_type == DT_REG) { // If the entry is a regular file
char *name = entry->d_name; // Get the file name
if (strstr(name, "fivesixteensvg") != NULL) { // If the file name contains the string 'fivesixteensvg'
count++; // Increment the count
files = realloc(files, count * sizeof(char *)); // Reallocate the array of file names
files[count - 1] = strdup(name); // Copy the file name to the array
}
}
}
closedir(d); // Close the directory
}
return files; // Return the array of file names
}
// A function to execute a Javascript file using Duktape library
// Returns 0 on success, or -1 on error
int execute_js(const char *file) {
int result = 0; // Initialize the result
duk_context *ctx = duk_create_heap_default(); // Create a Duktape heap
if (ctx) { // If the heap is created successfully
if (duk_peval_file(ctx, file) != 0) { // If the evaluation of the file fails
printf("Error: %s\n", duk_safe_to_string(ctx, -1)); // Print the error message
result = -1; // Set the result to -1
}
duk_pop(ctx); // Pop the evaluation result
duk_destroy_heap(ctx); // Destroy the heap
} else { // If the heap is not created successfully
printf("Failed to create a Duktape heap.\n"); // Print a message
result = -1; // Set the result to -1
}
return result; // Return the result
}