patterncMinor
Reading messages with binary tree
Viewed 0 times
readingwithbinarymessagestree
Problem
I'm building a binary tree.
Example: key AAAA1.ETR, value 1.
I'm reading files with this structure:
Data can be compressed or uncompressed (this is saved in flag). Data contain more messages. Could you have a look and give me hints on what I can do better?
```
#include
#include
#include
#include "zlib.h"
#include
#include
#include
#include
#include
#include
#define MYMAXSIZE 10000000
#define MAXFILESIZE 6000000
#define OBJNAME "8"
#define MSGTYPE "1"
const uint8_t broadcast[] = { 0x36, 0x33 };
const uint8_t status[] = { 0x34 };
const char dir0[]= "/home/frog/reader/feed.0/";
const char dir1[]= "/home/frog/reader/feed.1/";
const char dir2[]= "/home/frog/reader/feed.2/";
const char dir3[]= "/home/frog/reader/feed.3/";
typedef struct mtmheader_t
{
unsigned char objName[20]; //8
unsigned char msgType[3]; //1
}mtmheader_t;
typedef struct analyzer_t
{
unsigned char *buff;
size_t s;
}analyzer_t;
typedef struct analyzers_t
{
analyzer_t anal1;
analyzer_t anal2;
analyzer_t anal3;
unsigned char* seq;
}analyzers_t;
int readFilec(GTree *tree)
{
FILE * fp = fopen("cfg/InstrumentList_FULL.csv", "rb" );
char * line = NULL;
size_t len = 0;
ssize_t read;
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1)
{
char *p1;
int *p2 = malloc(sizeof (int));
printf("%s", line);
p1 = strtok(line, "|");
*p2 = atoi(strtok(NULL, "|"));
g_tree_insert(tree, (gpointer) g_strdup(p1), (gpointer)p2);
//TrieAdd(&root, strdup(p1), p2);
printf("-%s%d ", p1, *p2);
}
if (line)
free(line);
//exit(EXIT_SUCCESS);
}
int readFile( char name,unsigned char buffp)
{
int fileSize = 0;
//int n, i, j;
printf("Opening file: \"%s\"\n", name);
FILE *pFile = fopen(name, "rb");
if(pFile == NULL)
{
printf("error: fopen()");
return -1;
}
fsee
Example: key AAAA1.ETR, value 1.
I'm reading files with this structure:
DataLength Block-SequenceNummer FLag Start Data Ende
4 Bytes 8 Bytes 1 Byte S Datalength EData can be compressed or uncompressed (this is saved in flag). Data contain more messages. Could you have a look and give me hints on what I can do better?
```
#include
#include
#include
#include "zlib.h"
#include
#include
#include
#include
#include
#include
#define MYMAXSIZE 10000000
#define MAXFILESIZE 6000000
#define OBJNAME "8"
#define MSGTYPE "1"
const uint8_t broadcast[] = { 0x36, 0x33 };
const uint8_t status[] = { 0x34 };
const char dir0[]= "/home/frog/reader/feed.0/";
const char dir1[]= "/home/frog/reader/feed.1/";
const char dir2[]= "/home/frog/reader/feed.2/";
const char dir3[]= "/home/frog/reader/feed.3/";
typedef struct mtmheader_t
{
unsigned char objName[20]; //8
unsigned char msgType[3]; //1
}mtmheader_t;
typedef struct analyzer_t
{
unsigned char *buff;
size_t s;
}analyzer_t;
typedef struct analyzers_t
{
analyzer_t anal1;
analyzer_t anal2;
analyzer_t anal3;
unsigned char* seq;
}analyzers_t;
int readFilec(GTree *tree)
{
FILE * fp = fopen("cfg/InstrumentList_FULL.csv", "rb" );
char * line = NULL;
size_t len = 0;
ssize_t read;
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1)
{
char *p1;
int *p2 = malloc(sizeof (int));
printf("%s", line);
p1 = strtok(line, "|");
*p2 = atoi(strtok(NULL, "|"));
g_tree_insert(tree, (gpointer) g_strdup(p1), (gpointer)p2);
//TrieAdd(&root, strdup(p1), p2);
printf("-%s%d ", p1, *p2);
}
if (line)
free(line);
//exit(EXIT_SUCCESS);
}
int readFile( char name,unsigned char buffp)
{
int fileSize = 0;
//int n, i, j;
printf("Opening file: \"%s\"\n", name);
FILE *pFile = fopen(name, "rb");
if(pFile == NULL)
{
printf("error: fopen()");
return -1;
}
fsee
Solution
Just a few tidbits (mainly about error handling):
Unfortunately, all of these are mere details and probably not the comprehensive answer your were expecting. Hope that helps you anyway :)
- Depending on the architecture, you may want to check whether your
mallocs returnNULL. It is generally not useful though.
- In the function
writeFile, you never check whetherfopensucceded before usingfwrite.
- You could use
perrormore often to improve your error messages.
- You made your program
return 0when then functionopendirfails. Is it normal? Generally, you return any value, but not 0 on a failure.
- Just to add some extra pedantry: you use
uint8_tanduint32_t. That's generally fine, unless the architecture where your program runs has acharof more than 8 bits (less than 8 bits is not allowed by the standard). While it is extremely uncommon, C is often used for embedded software, so you might want to useuint_least8_tanduint_least32_tinstead, whose presence is mandatory (as per the C99 standard).
Unfortunately, all of these are mere details and probably not the comprehensive answer your were expecting. Hope that helps you anyway :)
Context
StackExchange Code Review Q#42577, answer score: 5
Revisions (0)
No revisions yet.