projmr/projmr.h
2026-01-05 21:46:36 +04:00

270 lines
7.8 KiB
C

/*
* projmr.h
*
* Copyright 2025
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#if (!defined(__LIB_projmr_H_))
#define __LIB_projmr_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#define projmr_VERSION "0.1"
#define Makefile "Makefile"
#define LICENSE "LICENSE"
char* __YDSJS_user_home = NULL;
char* __YDSJS_files_dir = "/.projmr/project_files/";
static void projmr_error(char* _err) {
printf("\033[1mprojmr: \033[91m%s\033[0m\n", _err);
}
void __attach_project_mode(void) {
__YDSJS_user_home = getenv("HOME");
}
FILE* __open_project_file(char* name, char* mode) {
if (strlen(name) >= 32) return NULL;
char* file_text_name = malloc(strlen(__YDSJS_user_home) + strlen(__YDSJS_files_dir) + 34);
strcpy(file_text_name, __YDSJS_user_home);
strcat(file_text_name, __YDSJS_files_dir);
strcat(file_text_name, name);
FILE* fp = fopen(file_text_name, mode);
free(file_text_name);
return fp;
}
static int __create_makefile(char* name) {
FILE* makefile = fopen(Makefile, "w");
FILE* make_text = __open_project_file("make_license.txt", "r");
if (makefile == NULL || make_text == NULL) {
projmr_error("File error!");
return EXIT_FAILURE;
}
time_t mytime = time(NULL);
struct tm *now = localtime(&mytime);
int year = now->tm_year + 1900;
fprintf(makefile,
"#" "\n"
"# Makefile" "\n"
"# " "\n"
"# Copyright %d\n", year
);
int c;
while ((c = getc(make_text)) != EOF) {
fputc(c, makefile);
}
fclose(make_text);
make_text = __open_project_file("make_structure.txt", "r");
if (make_text == NULL) {
projmr_error("File error!");
return EXIT_FAILURE;
}
fprintf(makefile, "\nPROJECT_NAME=%s\n\n", name);
while ((c = getc(make_text)) != EOF) {
fputc(c, makefile);
}
fclose(make_text);
fclose(makefile);
return EXIT_SUCCESS;
}
static int __create_license(void) {
FILE* lic = __open_project_file("project_license.txt", "r");
FILE* wrt = fopen(LICENSE, "w");
if (lic == NULL || wrt == NULL) {
projmr_error("File error!");
return EXIT_FAILURE;
}
int c;
while ((c = getc(lic)) != EOF) {
fputc(c, wrt);
}
fclose(lic);
fclose(wrt);
return EXIT_SUCCESS;
}
static int __create_project_folder(void) {
if (mkdir("project_files", S_IRWXU | S_IRWXG | S_IRWXO) != 0) {
puts("\033[1mprojmr: \033[35mWarning: The project already exists!\033[0m");
return EXIT_SUCCESS;
};
FILE* fp = fopen("project_files/example.txt", "w");
if (fp == NULL) {
projmr_error("File error!");
return EXIT_FAILURE;
}
fputs("Lorem ipsum dolor sit amet, consectetur " "\n"
"adipiscing elit, sed do eiusmod tempor incididunt ut " "\n"
"labore et dolore magna aliqua. Ut enim ad minim veniam, " "\n"
"quis nostrud exercitation ullamco laboris nisi ut " "\n"
"aliquip ex ea commodo consequat. Duis aute irure dolor " "\n"
"in reprehenderit in voluptate velit esse cillum dolore " "\n"
"eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, " "\n"
"sunt in culpa qui officia deserunt mollit anim id est laborum" "\n", fp);
fclose(fp);
return EXIT_SUCCESS;
}
static int __create_config(char* name) {
FILE* make_text = __open_project_file("make_license.txt", "r");
FILE* conf = fopen("project.ini", "w");
if (conf == NULL || make_text == NULL) {
projmr_error("File error!");
return EXIT_FAILURE;
}
time_t mytime = time(NULL);
struct tm *now = localtime(&mytime);
int year = now->tm_year + 1900;
fprintf(conf,
"#" "\n"
"# project.ini" "\n"
"# " "\n"
"# Copyright %d\n", year
);
int c;
while ((c = getc(make_text)) != EOF) {
fputc(c, conf);
}
fprintf(conf, "\n[%d.%d.%d-%d:%d:%d]\n", now->tm_mday, now->tm_mon + 1, now->tm_year + 1900, now->tm_hour, now->tm_min, now->tm_sec);
fprintf(conf, "name=%s\n", name);
srand((unsigned long)(void*)name);
fprintf(conf, "id=%lu\n", (unsigned long int)rand());
fputs("project_files_count=1\n", conf);
return EXIT_SUCCESS;
}
static int __create_main(char* name) {
char file_name[35];
strcpy(file_name, name);
strcat(file_name, ".c");
FILE* main = fopen(file_name, "w");
FILE* main_ex = __open_project_file("project_main.txt", "r");
if (main == NULL || main_ex == NULL) {
projmr_error("File error!");
return EXIT_FAILURE;
}
time_t mytime = time(NULL);
struct tm *now = localtime(&mytime);
int year = now->tm_year + 1900;
fprintf(
main,
"/*" "\n"
" * %s" "\n"
" * " "\n"
" * Copyright %d " "\n",
file_name, year
);
int c;
while ((c = getc(main_ex)) != EOF) {
if (c != '%') fputc(c, main);
else fputs(name, main);
}
file_name[strlen(file_name) - 1] = 'h';
fclose(main);
fclose(main_ex);
main = fopen(file_name, "w");
main_ex = __open_project_file("project_lib.txt", "r");
if (main == NULL || main_ex == NULL) {
projmr_error("File error!");
return EXIT_FAILURE;
}
fprintf(
main,
"/*" "\n"
" * %s" "\n"
" * " "\n"
" * Copyright %d " "\n",
file_name, year
);
unsigned int line = 1;
while ((c = getc(main_ex)) != EOF) {
if (c != '%' || line == 28) fputc(c, main);
else fputs(name, main);
if (c == '\n') line ++;
}
fclose(main);
fclose(main_ex);
return EXIT_SUCCESS;
}
int __create_project(char* name) {
int ret = 0;
__attach_project_mode();
if (strlen(name) >= 32) {
projmr_error("The name cannot be longer than 31 characters.");
return EXIT_FAILURE;
}
ret += __create_makefile(name);
ret += __create_project_folder();
ret += __create_license();
ret += __create_main(name);
ret += __create_config(name);
if (ret != EXIT_SUCCESS) return EXIT_FAILURE;
return EXIT_SUCCESS;
}
int _lib_projmr_main(int argc, char** argv) {
size_t pos = 1;
char* project_name = NULL;
while (pos != argc) {
char* str = argv[pos];
if (str[0] == '-') {
unsigned char pos_loc = 0;
if (str[1] == '-') pos_loc ++;
switch (str[1 + pos_loc]) {
case 'h':
puts(
"Simple project manager witch GNU GPL V3" "\n"
" " "projmr NAME [--help / --version]" "\0"
);
return EXIT_SUCCESS;
break;
case 'v':
puts(projmr_VERSION);
return EXIT_SUCCESS;
break;
default:
projmr_error("Unknown option!");
return EXIT_FAILURE;
}
} else {
project_name = str;
}
pos ++;
}
if (project_name == NULL) {
projmr_error("Error! Call \"projmr --help\" for more information.");
return EXIT_FAILURE;
}
return __create_project(project_name);
}
#endif