Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Bin/Demangler/Main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ int main(void) {
Strs lines = StrSplit(&file, "\n");

// Use the fixed VecForeachPtr macro
VecForeachPtr(&lines, line, {
VecForeachPtr(&lines, line) {
if (StrStartsWithZstr(line, "[.") && StrEndsWithZstr(line, "]")) {
Str rule_name = StrInit();
StrReadFmt(line->data, "[.{}]", rule_name);
Expand All @@ -16,7 +16,7 @@ int main(void) {
StrDeinit(&rule_name);
}
}
});
}

VecDeinit(&lines);
VecDeinit(&file);
Expand Down
5 changes: 5 additions & 0 deletions Bin/ElfInfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,5 +411,10 @@ int main(int argc, char** argv) {
eh.string_table_index
);

Vec(int) vi = VecInit();
VecForeachIdx(&vi, val, i) {
WriteFmtLn("{}", val);
}

return 0;
}
152 changes: 74 additions & 78 deletions Bin/MisraDoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ int main(int argc, char** argv) {
VecMerge(&dir_paths, &project.test_directories);

// recursively explore directories and get filenames
VecForeach(&dir_paths, dir_name, {
VecForeach(&dir_paths, dir_name) {
// keep track of current path we're exploring
Str current_path = StrInit();
StrMerge(&current_path, &dir_name);

SysDirContents dir_contents = SysGetDirContents(dir_name.data);
Scope(&dir_contents, VecDeinit, {
VecForeach(&dir_contents, dir_entry, {
VecForeach(&dir_contents, dir_entry) {
// if it's a directory then store it for exploration later on
if (dir_entry.type == SYS_DIR_ENTRY_TYPE_DIRECTORY) {
// create new directory path relative to current directory search path
Expand All @@ -144,84 +144,80 @@ int main(int argc, char** argv) {

// store the directory name, ownersip transferred
VecPushBack(&dir_paths, path);
} else if (dir_entry.type == SYS_DIR_ENTRY_TYPE_REGULAR_FILE) {
// create complete relative file path
Str path = StrInit();
StrMerge(&path, &current_path);
StrPushBack(&path, '/');
StrMerge(&path, &dir_entry.name);

// store discovered file name, ownersip transferred
VecPushBack(&file_paths, path);
}
// any other file type is not documented
});
});
});
});

// go over each file and generate corresponding markdown
VecForeach(&file_paths, file_path, {
Str file_contents = StrInit();
Scope(&file_contents, StrDeinit, {
if (!ReadCompleteFile(
file_path.data,
&file_contents.data,
&file_contents.length,
&file_contents.capacity
)) {
LOG_ERROR("Failed to read \"{}\" source file.", file_path.data);
continue;
}

Str output_path = StrInit();
Scope(&output_path, StrDeinit, {
StrMerge(&output_path, &file_path);
LOG_INFO("{}", output_path);
StrReplaceZstr(&output_path, "/", "-", -1);
LOG_INFO("{}", output_path);

Str md_code = StrInit();
Scope(&md_code, StrDeinit, {
// Create template strings for StrWriteFmt with escaped braces
const char* mdHeader =
"---\n"
"title: \"{}\"\n"
"meta_title: \"{}\"\n"
"description: \"Documentation for {}\"\n"
"date: 2025-05-12T05:00:00Z\n"
"# image: \"/images/image-placeholder.png\"\n"
"categories: [\"Vec\", \"Macro\", \"Generic\"]\n"
"author: \"Siddharth Mishra\"\n"
"tags: [\"vec\", \"macro\", \"generic\"]\n"
"draft: false\n"
"---\n"
"```c\n";

StrWriteFmt(&md_code, mdHeader, output_path.data, output_path.data, output_path.data);
StrMerge(&md_code, &file_contents);
StrWriteFmt(&md_code, "\n```");

// complete relative file path
StrPushFront(&output_path, '/');
LOG_INFO("{}", output_path);
StrPushFrontCstr(&output_path, project.build_dir.data, project.build_dir.length);
LOG_INFO("{}", output_path);
StrReplaceZstr(&output_path, ".c", ".md", 1);
StrReplaceZstr(&output_path, ".h", ".md", 1);
LOG_INFO("{}\n\n", output_path);


// dump code to output path
FILE* f = fopen(output_path.data, "w");
Scope(f, fclose, { fwrite(md_code.data, 1, md_code.length, f); });
});
});
});
}
else if (dir_entry.type == SYS_DIR_ENTRY_TYPE_REGULAR_FILE) {
// create complete relative file path
Str path = StrInit();
StrMerge(&path, &current_path);
StrPushBack(&path, '/');
StrMerge(&path, &dir_entry.name);

// store discovered file name, ownersip transferred
VecPushBack(&file_paths, path);
}
// any other file type is not documented
}
});
}
});

// go over each file and generate corresponding markdown
VecForeach(&file_paths, file_path) {
Str file_contents = StrInit();
Scope(&file_contents, StrDeinit, {
if (!ReadCompleteFile(file_path.data, &file_contents.data, &file_contents.length, &file_contents.capacity)) {
LOG_ERROR("Failed to read \"{}\" source file.", file_path.data);
continue;
}

Str output_path = StrInit();
Scope(&output_path, StrDeinit, {
StrMerge(&output_path, &file_path);
LOG_INFO("{}", output_path);
StrReplaceZstr(&output_path, "/", "-", -1);
LOG_INFO("{}", output_path);

Str md_code = StrInit();
Scope(&md_code, StrDeinit, {
// Create template strings for StrWriteFmt with escaped braces
const char* mdHeader =
"---\n"
"title: \"{}\"\n"
"meta_title: \"{}\"\n"
"description: \"Documentation for {}\"\n"
"date: 2025-05-12T05:00:00Z\n"
"# image: \"/images/image-placeholder.png\"\n"
"categories: [\"Vec\", \"Macro\", \"Generic\"]\n"
"author: \"Siddharth Mishra\"\n"
"tags: [\"vec\", \"macro\", \"generic\"]\n"
"draft: false\n"
"---\n"
"```c\n";

StrWriteFmt(&md_code, mdHeader, output_path.data, output_path.data, output_path.data);
StrMerge(&md_code, &file_contents);
StrWriteFmt(&md_code, "\n```");

// complete relative file path
StrPushFront(&output_path, '/');
LOG_INFO("{}", output_path);
StrPushFrontCstr(&output_path, project.build_dir.data, project.build_dir.length);
LOG_INFO("{}", output_path);
StrReplaceZstr(&output_path, ".c", ".md", 1);
StrReplaceZstr(&output_path, ".h", ".md", 1);
LOG_INFO("{}\n\n", output_path);


// dump code to output path
FILE* f = fopen(output_path.data, "w");
Scope(f, fclose, { fwrite(md_code.data, 1, md_code.length, f); });
});
});
});
};
});
});

LogDeinit();
return 0;
LogDeinit();
return 0;
}
16 changes: 8 additions & 8 deletions Bin/MisraEnum.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ int main(int argc, char** argv) {
}

// Use VecForeach for iterating over entries
VecForeach(&entries, e, {
VecForeach(&entries, e) {
if (last_value == e.value - 1) {
StrWriteFmt(&code, " {},\n", e.name.data);
} else {
StrWriteFmt(&code, " {} = {},\n", e.name.data, e.value);
}
last_value = e.value;
});
};

StrWriteFmt(&code, "}} {};\n", enum_name.data);

Expand Down Expand Up @@ -150,12 +150,12 @@ int main(int argc, char** argv) {
StrWriteFmt(&code, funcHeader, enum_name.data, enum_name.data, enum_name.data, invalidEnumName);

// Use VecForeach for iterating over entries
VecForeach(&entries, e, {
VecForeach(&entries, e) {
const char* compareTemplate = " if(ZstrCompareN(\"{}\", zstr, {}) == 0) {{return {};}}\n";
// Store the length in a variable to avoid taking address of rvalue
unsigned long long strLength = (unsigned long long)e.str.length;
StrWriteFmt(&code, compareTemplate, e.str.data, strLength, e.name.data);
});
};

const char* returnTemplate = " return {};\n}}\n";
StrWriteFmt(&code, returnTemplate, invalidEnumName);
Expand All @@ -175,10 +175,10 @@ int main(int argc, char** argv) {
StrWriteFmt(&code, toZstrHeader, enum_name.data, enum_name.data, enum_name.data);

// Use VecForeach for iterating over entries
VecForeach(&entries, e, {
VecForeach(&entries, e) {
const char* caseTemplate = " case {} : {{return \"{}\";}}\n";
StrWriteFmt(&code, caseTemplate, e.name.data, e.str.data);
});
};

const char* defaultTemplate =
" default: break;\n"
Expand Down Expand Up @@ -209,10 +209,10 @@ int main(int argc, char** argv) {
StrDeinit(&enum_name);
StrDeinit(&code);

VecForeach(&entries, e, {
VecForeach(&entries, e) {
StrDeinit(&e.name);
StrDeinit(&e.str);
});
};
VecDeinit(&entries);

LogDeinit();
Expand Down
6 changes: 3 additions & 3 deletions Include/Misra/Parsers/JSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
WriteFmtLn("X : {}", obj.data.y);
WriteFmtLn("N : {}", obj.data.n);
WriteFmt("strs : [");
VecForeach(&obj.strs, str, { WriteFmt("{}, ", str); });
VecForeach(&obj.strs, str) { WriteFmt("{}, ", str); }
WriteFmtLn("]");

StrClear(&json);
Expand Down Expand Up @@ -730,14 +730,14 @@ StrIter JSkipValue(StrIter si);
bool ___is_first___ = true; \
(void)___is_first___; \
StrPushBack(&(j), '['); \
VecForeach(&(arr), item, { \
VecForeach(&(arr), item) { \
if (___is_first___) { \
___is_first___ = false; \
} else { \
StrPushBack(&(j), ','); \
} \
{ writer } \
}); \
} \
StrPushBack(&(j), ']'); \
} while (0)

Expand Down
Loading
Loading