#ifndef CMPR_H
#define CMPR_H
#include <stdio.h>
#include <stdlib.h>
#define BUFSIZE 1024
#define ASCII_CHARS 126
#define PERROR(e, r)
return r;
typedef struct char_freq {
char character;
int frequency;
} Char_freq;
typedef struct freq_table {
Char_freq freqs[ASCII_CHARS];
size_t size;
} Freq_table;
// parses the file name from the list of command line arguments
// returns error
int parse_file_name(int argc, char *argv[], FILE **fd);
// reads a file into a buffer
// returns error
int read_file(char buf[BUFSIZE], FILE *fd);
// reads the text and adds the frequecy of each character to a table
// freq table: char **
int determine_char_freqs(FILE *fd);
// get frequency per each buffer iteration
void buf_freqs(char buf[], Freq_table *table);
#endif
-------------------------------------------------------------
#include <string.h>
#include <ctype.h>
#include "cmpr.h"
int main(int argc, char *argv[])
{
FILE *fd = NULL;
int err = 0;
if ((err = parse_file_name(argc, argv, &fd;)) == -1) {
PERROR("open file", EXIT_FAILURE);
}
determine_char_freqs(fd);
return 0;
}
int parse_file_name(int argc, char *argv[], FILE **fd)
{
*fd = fopen(argv[argc-1], "r");
if (*fd == NULL) {
return -1;
}
return 0;
}
int read_file(char buf[BUFSIZE], FILE *fd)
{
size_t read;
if ((read
= fread(buf
, sizeof(char), BUFSIZE
-1, fd
)) == EOF
|| read
== 0) {
return -1;
}
return 0;
}
int determine_char_freqs(FILE *fd)
{
char buf[BUFSIZE] = {0};
Freq_table ftable = {0};
ftable.size = ASCII_CHARS;
for (size_t i = 32; i <= ftable.size; i++)
ftable.freqs[i].character = (char)i;
// count text and fill table
while(read_file(buf, fd) != -1) {
buf_freqs(buf, &ftable;);
}
/**
for (size_t i = 32; i <= ftable.size; i++) {
if (isprint((unsigned char)ftable.freqs[i].character))
printf("%cn", ftable.freqs[i].character);
}
**/
for (size_t i = 32; i <= ftable.size; i++) {
printf("
;%c
: %dn
"
;, ftable.
freqs[i
].
character, ftable.
freqs[i
].
frequency);
}
return 0;
}
void buf_freqs(char buf[], Freq_table *table)
{
while(*buf != '�') {
for(size_t c = 32; c <= table->size; c++) {
if (*buf == table->freqs[c].character) {
table->freqs[c].frequency++;
break;
}
}
buf++;
}
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}