Skip to content

Commit

Permalink
adapted code to use only UTF-16 internally
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanide0081 committed Jan 31, 2023
1 parent 8c04a4a commit 2c94696
Show file tree
Hide file tree
Showing 14 changed files with 219 additions and 259 deletions.
40 changes: 20 additions & 20 deletions include/constants.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
#ifndef H_CONSTS
#define H_CONSTS

#define PROGRAM_NAME "RFF"
#define PROGRAM_NAME_FULL "RFFMPEG"
#define PROGRAM_VERSION "v0.6.1-beta"
#define PROGRAM_NAME u"RFF"
#define PROGRAM_NAME_FULL u"RFFMPEG"
#define PROGRAM_VERSION u"v0.6.2-beta"

static const char *fullTitle = PROGRAM_NAME_FULL " " PROGRAM_VERSION " (ユニコード)";
static const wchar_t *consoleWindowTitle = PROGRAM_NAME u" " PROGRAM_VERSION;
static const char16_t *fullTitle = PROGRAM_NAME_FULL u" " PROGRAM_VERSION u" (ユニコード)";
static const char16_t *consoleWindowTitle = PROGRAM_NAME u" " PROGRAM_VERSION;

/* ANSI escape chars for colored shell output (Virtual Terminal Sequences) */
#define CHARCOLOR_WHITE "\x1b[37m"
#define CHARCOLOR_WHITE_BOLD "\x1b[97m"
#define CHARCOLOR_RED "\x1b[31m"
#define CHARCOLOR_RED_BOLD "\x1b[91m"
#define COLOR_DEFAULT "\x1b[0m"
#define CHARCOLOR_WHITE u"\x1b[37m"
#define CHARCOLOR_WHITE_BOLD u"\x1b[97m"
#define CHARCOLOR_RED u"\x1b[31m"
#define CHARCOLOR_RED_BOLD u"\x1b[91m"
#define COLOR_DEFAULT u"\x1b[0m"

/* Buffers and size limitations */
#define BUFFER (512)
#define BUFFER (512 * sizeof(char16_t))
#define SHORTBUF (BUFFER / 16)
#define LONGBUF (BUFFER * 8)
#define PATHBUF (MAX_PATH)
#define PATHBUF (MAX_PATH * sizeof(char16_t))

#define MAX_ARGS 5
#define MAX_OPTS 6
Expand All @@ -38,15 +38,15 @@ static const wchar_t *consoleWindowTitle = PROGRAM_NAME u" " PROGRAM_VERSION;
#define OPT_FORCEOVERWRITE 4
#define OPT_CUSTOMFOLDERNAME 5

/* Option string constants */
#define OPT_DISPLAYHELP_STRING "--help"
#define OPT_MAKENEWFOLDER_STRING "--newfolder"
#define OPT_DELETEOLDFILES_STRING "--delete"
#define OPT_DISABLERECURSION_STRING "--norecursion"
#define OPT_FORCEOVERWRITE_STRING "--overwrite"
/* Option wcsing constants */
#define OPT_DISPLAYHELP_STRING u"--help"
#define OPT_MAKENEWFOLDER_STRING u"--newfolder"
#define OPT_DELETEOLDFILES_STRING u"--delete"
#define OPT_DISABLERECURSION_STRING u"--norecursion"
#define OPT_FORCEOVERWRITE_STRING u"--overwrite"

/* Misc functinality identifiers */
#define IDENTIFIER_NO_PATH "__ident_no_path"
#define IDENTIFIER_NO_FORMAT "__identifier_no_format"
#define IDENTIFIER_NO_PATH u"__ident_no_path"
#define IDENTIFIER_NO_FORMAT u"__identifier_no_format"

#endif
4 changes: 2 additions & 2 deletions include/handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include "libs.h"

int preventFilenameOverwrites(char *pureFilename, const char *outputFormat, const char *path);
int preventFilenameOverwrites(char16_t *pureFilename, const char16_t *outputFormat, const char16_t *path);

errorCode_t handleErrors(char *arguments[]);
errorCode_t handleErrors(char16_t *arguments[]);

#endif
8 changes: 6 additions & 2 deletions include/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@

#include "libs.h"

int parseArgumentsFromTerminal(char *arguments[], bool *options);
int parseArgumentsFromTerminal(char16_t *arguments[], bool *options);

void parseArguments(const int count, const char *rawArguments[], char *parsedArguments[], bool parsedOptions[], bool parseArguments, bool parseOptions);
void parseArguments(const int count, const char16_t *rawArguments[], char16_t *parsedArguments[], bool parsedOptions[], bool parseArguments, bool parseOptions);

int allocateArgumentBuffers(char16_t *arguments[]);

int freeArgumentBuffers(char16_t *arguments[]);

#endif
13 changes: 9 additions & 4 deletions include/libs.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdbool.h>
#include <uchar.h>
#include <time.h>
#include <locale.h>
#include <windows.h>

/* Libraries for enabling sketchy UTF-16 console input */
#include <fcntl.h>
#include <io.h>

#include "constants.h"
#include "types.h"
#include "man/help.h"
#include "mainloop.h"
#include "handlers.h"
#include "input.h"
#include "mainloop.h"
#include "terminal.h"
#include "man/help.h"

#endif
2 changes: 1 addition & 1 deletion include/mainloop.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

#include "libs.h"

errorCode_t searchDirectory(const char *directory, char *arguments[], const bool *options, processInfo_t *runtimeData);
errorCode_t searchDirectory(const char16_t *directory, char16_t *arguments[], const bool *options, processInfo_t *runtimeData);

#endif
36 changes: 18 additions & 18 deletions include/man/help.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@
#include "../libs.h"

static void displayHelp(void) {
printf_s(" Use me to automate an ffmpeg conversion inside a directory and its subfolders\n");
printf_s("\n");
printf_s(" Arguments:\n");
printf_s("\t%-25sthe directory containing your input files (defaults to current working path if left empty)\n", "-path <path>");
printf_s("\t%-25sthe file format(s) you want to find and convert (e.g.: mp4,mov)\n", "-fmt <format(s)>");
printf_s("\t%-25sthe ffmpeg conversion-related options (e.g.: -c:v libx264 -c:a aac -f matroska)\n", "-opts <options>");
printf_s("\t%-25sthe output extension (e.g.: mkv)\n", "-ext <extension>");
printf_s("\n");
printf_s(" Additional flags:\n");
printf_s("\t%-25sstore converted files in a new folder - you can choose a folder name with '--newfolder=foldername'\n", OPT_MAKENEWFOLDER_STRING);
printf_s("\t%-25sauto-overwrite existing output files (default behaviour appends an index)\n", OPT_FORCEOVERWRITE_STRING);
printf_s("\t%-25sdelete original files after conversion (PERMANENT) \n", OPT_DELETEOLDFILES_STRING);
printf_s("\t%-25sdisable recursive subfolder search\n", OPT_DISABLERECURSION_STRING);
printf_s("\n");
printf_s(" Example: rffmpeg -path C:\\Users\\Music -fmt wav,aif -opts \"-c:a libmp3lame\" -ext mp3 --newfolder\n");
printf_s("\n");
printf_s(" (p.s.: you can run this as a console application by simply running it with no arguments)\n");
printf_s("\n");
wprintf_s(u" Use me to automate an ffmpeg conversion inside a directory and its subfolders\n");
wprintf_s(u"\n");
wprintf_s(u" Arguments:\n");
wprintf_s(u"\t%-25Sthe directory containing your input files (defaults to current working path if left empty)\n", u"-path <path>");
wprintf_s(u"\t%-25Sthe file format(s) you want to find and convert (e.g.: mp4,mov)\n", "-fmt <format(s)>");
wprintf_s(u"\t%-25Sthe ffmpeg conversion-related options (e.g.: -c:v libx264 -c:a aac -f matroska)\n", u"-opts <options>");
wprintf_s(u"\t%-25Sthe output extension (e.g.: mkv)\n", u"-ext <extension>");
wprintf_s(u"\n");
wprintf_s(u" Additional flags:\n");
wprintf_s(u"\t%-25Sstore converted files in a new folder - you can choose a folder name with '--newfolder=foldername'\n", OPT_MAKENEWFOLDER_STRING);
wprintf_s(u"\t%-25Sauto-overwrite existing output files (default behaviour appends an index)\n", OPT_FORCEOVERWRITE_STRING);
wprintf_s(u"\t%-25Sdelete original files after conversion (PERMANENT) \n", OPT_DELETEOLDFILES_STRING);
wprintf_s(u"\t%-25Sdisable recursive subfolder search\n", OPT_DISABLERECURSION_STRING);
wprintf_s(u"\n");
wprintf_s(u" Example: rffmpeg -path C:\\Users\\Music -fmt wav,aif -opts \"-c:a libmp3lame\" -ext mp3 --newfolder\n");
wprintf_s(u"\n");
wprintf_s(u" (p.s.: you can run this as a console application by simply running it with no arguments)\n");
wprintf_s(u"\n");
}

#endif
4 changes: 1 addition & 3 deletions include/terminal.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

#include "libs.h"

void printError(const char *msg);

errno_t clearConsoleWindow(void);
void printError(const char16_t *msg);

errno_t restoreConsoleMode(DWORD originalConsoleMode);

Expand Down
2 changes: 0 additions & 2 deletions include/types.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef H_TYPES
#define H_TYPES

#include "../include/libs.h"

typedef struct processInformation {
uint64_t convertedFiles;
uint64_t deletedFiles;
Expand Down
15 changes: 9 additions & 6 deletions src/makefile → makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
CC=gcc
CFLAGS=-fdiagnostics-color=always -Os -std=c17
CFLAGS=-fdiagnostics-color=always $(OPTIMIZATIONFLAG) -Wall -Wno-unused-variable -Wno-unused-function -std=c17 -municode

IDIR=../include
ODIR=obj
LDIR=../lib
IDIR=./include
ODIR=src/obj
SDIR=src
LDIR=lib
OPTIMIZATIONFLAG=-Os

LIBS=-lm

Expand All @@ -13,12 +15,13 @@ DEPS=$(patsubst %,$(IDIR)/%,$(_DEPS))
_OBJ=main.o mainloop.o handlers.o input.o terminal.o
OBJ=$(patsubst %,$(ODIR)/%,$(_OBJ))

$(ODIR)/%.o: %.c $(DEPS)
$(ODIR)/%.o: $(SDIR)/%.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)

rffmpeg: $(OBJ)
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)

.PHONY: clean

clean: rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~
clean: $(OBJ)
rm -f $(OBJ)/*.o
35 changes: 15 additions & 20 deletions src/handlers.c
Original file line number Diff line number Diff line change
@@ -1,46 +1,41 @@
#include "../include/handlers.h"

int preventFilenameOverwrites(char *pureFilename, const char *outputFormat, const char *path) {
char fileMask[PATHBUF];
wchar_t fileMaskWide[PATHBUF];
char fileNameNew[PATHBUF];
int preventFilenameOverwrites(char16_t *pureFilename, const char16_t *outputFormat, const char16_t *path) {
char16_t fileMask[PATHBUF];
char16_t fileNameNew[PATHBUF];

sprintf_s(fileMask, PATHBUF, "%s\\%s.%s", path, pureFilename, outputFormat);
MultiByteToWideChar(CP_UTF8, 0, fileMask, -1, fileMaskWide, PATHBUF);
swprintf_s(fileMask, PATHBUF, u"%ls\\%ls.%ls", path, pureFilename, outputFormat);

HANDLE fileHandle = INVALID_HANDLE_VALUE;
WIN32_FIND_DATAW fileData;

if ((fileHandle = FindFirstFileW(fileMaskWide, &fileData)) != INVALID_HANDLE_VALUE) {
if ((fileHandle = FindFirstFileW(fileMask, &fileData)) != INVALID_HANDLE_VALUE) {
size_t index = 0;

do {
sprintf_s(fileMask, BUFFER, "%s\\%s-%03d.%s", path, pureFilename, ++index, outputFormat);
MultiByteToWideChar(CP_UTF8, 0, fileMask, -1, fileMaskWide, PATHBUF);
} while ((fileHandle = FindFirstFileW(fileMaskWide, &fileData)) != INVALID_HANDLE_VALUE);
swprintf_s(fileMask, BUFFER, u"%ls\\%ls-%03d.%ls", path, pureFilename, ++index, outputFormat);
} while ((fileHandle = FindFirstFileW(fileMask, &fileData)) != INVALID_HANDLE_VALUE);

sprintf_s(fileNameNew, PATHBUF, "%s-%03d", pureFilename, index);
strcpy_s(pureFilename, PATHBUF, fileNameNew);
swprintf_s(fileNameNew, PATHBUF, u"%ls-%03d", pureFilename, index);
wcscpy_s(pureFilename, PATHBUF, fileNameNew);
}

return EXIT_SUCCESS;
}

errorCode_t handleErrors(char *arguments[]) {
errorCode_t handleErrors(char16_t *arguments[]) {
/* Set current working directory as input path if none is provided */
if (*arguments[ARG_INPATH] == 0) {
wchar_t currentDirectory[PATHBUF];
GetCurrentDirectoryW(PATHBUF, currentDirectory);
WideCharToMultiByte(CP_UTF8, 0, currentDirectory, -1, arguments[ARG_INPATH], PATHBUF, NULL, NULL);
GetCurrentDirectoryW(PATHBUF, arguments[ARG_INPATH]);
}

if (*arguments[ARG_INFORMAT] == '\0') {
printError("no input format (null)");
if (*arguments[ARG_INFORMAT] == u'\0') {
printError(u"no input format (null)");
return ERROR_NO_INPUT_FORMAT;
}

if (*arguments[ARG_OUTFORMAT] == '\0') {
printError("no output format (null)");
if (*arguments[ARG_OUTFORMAT] == u'\0') {
printError(u"no output format (null)");
return ERROR_NO_OUTPUT_FORMAT;
}

Expand Down
Loading

0 comments on commit 2c94696

Please sign in to comment.