این برنامه هنوز تکمیل نشده. سوییچ های i, h کار میکندد. به احتمال زیاد به سوییچ های بیشتری نیاز پیدا بکنم چون میخوام این برنامه رو راهی AUR بکنم. هنوز خیلی کار داره ولی خب فعلا خوبه:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
typedef struct
{
char letter;
char* code;
}Morse_EN;
typedef struct
{
int number;
char* code;
}Morse_NUM;
Morse_EN morse_table []=
{
{'A', ".-"},
{'B', "-..."},
{'C', "-.-"},
{'D', "-.."},
{'E', "."},
{'F', "..-"},
{'G', "--."},
{'H', "...."},
{'I', ".."},
{'J', ".---"},
{'K', "-.-"},
{'L', ".-.."},
{'M', "--"},
{'N', "-."},
{'O', "---"},
{'P', ".--."},
{'Q', "--.-"},
{'R', ".-."},
{'S', "..."},
{'T', "-"},
{'U', "..-"},
{'V', "...-"},
{'W', ".--"},
{'X', "-..-"},
{'Y', "-.--"},
{'Z', "--.."},
};
Morse_NUM morse_numbers[] =
{
{1, ".----"},
{2, "..---"},
{3, "...--"},
{4, "....-"},
{5, "....."},
{6, "-...."},
{7, "--..."},
{8, "---.."},
{9, "----."},
{0, "-----"},
};
void Usage(FILE* stream, int exit_code, const char* prog_name);
void Collision_args(int next_option, const char* prog_name, const char* file_name);
void encode(const char* file_name);
unsigned long int file_size(const char* file_name);
const char* prog_name;
int main(int argc, char* argv[])
{
// entry point
prog_name = argv[0];
int next_option;
//define short options:
const char* const short_option = "ihd:e:";
//define long options:
const struct option long_options[] =
{
{"info", no_argument, NULL, 'i'},
{"help", no_argument, NULL, 'h'},
{"decode", required_argument, NULL, 'd'},
{"encode", required_argument, NULL, 'e'},
{NULL, no_argument, NULL, 0}
};
do {
next_option = getopt_long(argc, argv, short_option, long_options, NULL);
Collision_args(next_option, argv[0], optarg);
}while (next_option != -1);
return EXIT_SUCCESS;
}
void Usage(FILE* stream, int exit_code, const char* prog_name)
{
fprintf(stream, "usage: <%s> --options.\n", prog_name);
fprintf(stream,
"-i --info give a information and rules for Morse code: -i or --info\n"
"-h --help print this message: -h or --help.\n"
"-d --decode decode The Morse code file: -d or --deocde.\n"
"-e --encode encode The Text file: -e --encode.\n");
}
void Collision_args(int next_option, const char* prog_name, const char* file_name)
{
switch (next_option)
{
case 'i':
fprintf(stdout,
"1: The length of a dot is one unit.\n"
"2: a dash is tree units.\n"
"3: The space between parts of The same letter is one unit\n"
"4: The space between letters is three units.\n"
"5: The space between words is seven units.\n\n\n"
);
fprintf(stdout, "-------------------------------------------\n\n");
fprintf(stdout,
"A: .-\n"
"B: -...\n"
"C: -.-.\n"
"D: -..\n"
"E: .\n"
"F: ..-.\n"
"G: --.\n"
"H: ....\n"
"I: ..\n"
"J: .---\n"
"K: -.-\n"
"L: .-..\n"
"M: --\n"
"N: -.\n"
"O: ---\n"
"P: .--.\n"
"Q: --.-\n"
"R: .-.\n"
"S: ...\n"
"T: -\n"
"U: ..-\n"
"V: ...-\n"
"W: .--\n"
"X: -..-\n"
"Y: -.--\n"
"Z: --..\n\n\n"
);
fprintf(stdout, "Numbers: \n\n");
fprintf(stdout,
"1: .----\n"
"2: ..---\n"
"3: ...--\n"
"4: ....-\n"
"5: .....\n"
"6: -....\n"
"7: --...\n"
"8: ---..\n"
"9: ----.\n"
"0: -----\n"
);
break;
case -1:
//Usage(stdout, EXIT_FAILURE, prog_name);
break;
case '?':
Usage(stderr, EXIT_FAILURE, "morse");
break;
case 'h':
Usage(stdout, 0, prog_name);
break;
case 'd':
break;
case 'e':
encode(file_name);
break;
default:
Usage(stdout, EXIT_SUCCESS, prog_name);
abort();
}
}
void encode(const char* file_name)
{
char* date;
FILE* fptr = NULL;
fptr = fopen(file_name, "r");
if(!fptr)
{
perror("failed");
exit(EXIT_FAILURE);
}
fseek(fptr, 0, SEEK_SET);
char ch = fgetc(fptr);
if(ch != EOF)
printf("the char is %c", ch);
fclose(fptr);
}
unsigned long int file_size(const char* file_name)
{
FILE* fptr = fopen(file_name, "rb");
if(!fptr)
{
perror("failed");
exit(EXIT_FAILURE);
}
fseek(fptr, 0L, SEEK_END);
unsigned long int size = ftell(fptr);
return size;
}