Using classes to get and set data buffers with arduino in C - c

I am having a problem storing data it seems in some buffers that I have made. Here is how the code looks:
Here are the definitions for each function:
WriteKeyBrdNum:
char Write_Keyboard::WriteKeybrdNum(TSPoint pos, Data_Buffers dB, int selection, int count)
{
char data;
switch (selection)
{
case EXERCISE:
{
if (pos.y > GRABCHARSIZE * 9) //0
{
//dspWord[count] = '0';
dB.SetDataBufferChar('0', selection, count);
data = dB.GetDataBufferChar(selection, count);
}
else if (pos.y > GRABCHARSIZE * 8) //1
{
//dspWord[count] = '1';
dB.SetDataBufferChar('1', selection, count);
data = dB.GetDataBufferChar(selection, count);
//data = '1';
}
else if (pos.y > GRABCHARSIZE * 7) //2
{
//dspWord[count] = '2';
dB.SetDataBufferChar('2', selection, count);
data = dB.GetDataBufferChar(selection, count);
//data = '2';
}
else if (pos.y > GRABCHARSIZE * 6) //3
{
//dspWord[count] = '3';
dB.SetDataBufferChar('3', selection, count);
data = dB.GetDataBufferChar(selection, count);
//data = '3';
}
else if (pos.y > GRABCHARSIZE * 5) //4
{
//dspWord[count] = '4';
dB.SetDataBufferChar('4', selection, count);
data = dB.GetDataBufferChar(selection, count);
//data = '4';
}
else if (pos.y > GRABCHARSIZE * 4) //5
{
//dspWord[count] = '5';
dB.SetDataBufferChar('5', selection, count);
data = dB.GetDataBufferChar(selection, count);
//data = '5';
}
else if (pos.y > GRABCHARSIZE * 3) //6
{
//dspWord[count] = '6';
dB.SetDataBufferChar('6', selection, count);
data = dB.GetDataBufferChar(selection, count);
//data = '6';
}
else if (pos.y > GRABCHARSIZE * 2) //7
{
//dspWord[count] = '7';
dB.SetDataBufferChar('7', selection, count);
data = dB.GetDataBufferChar(selection, count);
//data = '7';
}
else if (pos.y > GRABCHARSIZE) //8
{
//dspWord[count] = '8';
dB.SetDataBufferChar('8', selection, count);
data = dB.GetDataBufferChar(selection, count);
//data = '8';
}
else if (pos.y > 0) //9
{
//dspWord[count] = '9';
dB.SetDataBufferChar('9', selection, count);
data = dB.GetDataBufferChar(selection, count);
//data = '9';
}
return data;
break;
}
GetDataBufferChar:
char Data_Buffers::GetDataBufferChar(int bufferSelect, int count)
{
char letter;
switch (bufferSelect)
{
case EXERCISE:
{
letter = dspWord[count];
break;
}
case SETS:
{
letter = setsNum[count];
break;
}
case REPS:
{
letter = repsNum[count];
break;
}
case WEIGHT:
{
letter = weightNum[count];
break;
}
default:
{
letter = '\0';
break;
}
}
return letter;
}
SetDataBufferChar:
void Data_Buffers::SetDataBufferChar(char data, int bufferSelect, int count)
{
switch (bufferSelect)
{
case EXERCISE:
{
dspWord[count] = data;
break;
}
case SETS:
{
setsNum[count] = data;
break;
}
case REPS:
{
repsNum[count] = data;
break;
}
case WEIGHT:
{
weightNum[count] = data;
break;
}
default:
break;
}
}
DrawBufferChar:
char Draw_Stuff::DrawBufferChar(SWTFT tft, Data_Buffers dB, int selection, int count, int charSpace)
{
char data;
data = dB.GetDataBufferChar(selection, count);
tft.drawChar(TEXTXPOS*charSpace, TEXTYPOS + 30, data, BLACK, WHITE, EXTTEXTSIZE);
return data;
}
Main loop:
dspWord[wordCount] = writeKeyboard.WriteKeybrdNum(p, dataBuffer, selection, wordCount);
Serial.print("data\t"); Serial.println(dspWord[wordCount]);// Serial.print("\n");
dspWord[wordCount] = drawStuff.DrawBufferChar(tft, dataBuffer, selection, wordCount, wordSpace);
Serial.print("data1\t"); Serial.println(dspWord[wordCount]); //Serial.print("\n");
The Get and Set functions are storing data and getting data from buffers that are established as private buffers in Data_buffers class. The whole point is to be able to set these data buffers in one function, and get them using another function. For the first serial print, the data that is being set in the WriteKeybrdNum function is actually displayed. Then when I call DrawBufferChar, the second serial port print has no data. Am I missing something here. Both DrawBufferChar and WriteKeybrdNum are calling the exact same GetDataBufferChar function with the same "selection" and same "wordCount" yet data is being returned in one function and not the other. Doesn't make sense to me.
Any Help would be greatly appreciated.
Thanks.

Related

how can I read socket by blocking&none-multiplexing&sync mode?

I want to repeatedly request one specific url by maximum performance. I copied below source from github, but I think it isn't optimized for my case, so I have to tune it.
Which Option is best in my case?
block vs non-block
sync vs async
I think non-multiplexing is right, so I've to change select() function to the other one. Is this right?
char response[RESPONSE_SIZE + 1];
char *p = response, *q;
char *end = response + RESPONSE_SIZE;
char *body = 0;
enum {
length, chunked, connection
};
int encoding = 0;
int remaining = 0;
while (1) {
fd_set reads;
FD_ZERO(&reads);
FD_SET(server, &reads);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 200;
if (FD_ISSET(server, &reads)) {
int bytes_received = SSL_read(ssl, p, end - p);
p += bytes_received;
*p = 0;
if (!body && (body = strstr(response, "\r\n\r\n"))) {
*body = 0;
body += 4;
printf("Received Headers:\n%s\n", response);
q = strstr(response, "\nContent-Length: ");
if (q) {
encoding = length;
q = strchr(q, ' ');
q += 1;
remaining = strtol(q, 0, 10);
} else {
q = strstr(response, "\nTransfer-Encoding: chunked");
if (q) {
encoding = chunked;
remaining = 0;
} else {
encoding = connection;
}
}
printf("\nReceived Body:\n");
}
if (body) {
if (encoding == length) {
if (p - body >= remaining) {
printf("%.*s", remaining, body);
break;
}
} else if (encoding == chunked) {
do {
if (remaining == 0) {
if ((q = strstr(body, "\r\n"))) {
remaining = strtol(body, 0, 16);
if (!remaining) goto finish;
body = q + 2;
} else {
break;
}
}
if (p - body >= remaining) {
printf("%.*s", remaining, body);
body += remaining + 2;
remaining = 0;
}
} while (!remaining);
}
} //if (body)
} //if FDSET
} //end while(1)
finish:
buffer[0] = '\0';
printf("\nClosing socket...\n");

Why strcmp does not return 0?

I have a small program to handle a list of rabbits (name, district, participation count) stored in an array of pointers (Rabbit** iterator). I'd like to implement the following methods: add, delete and modify a rabbit, list all the rabbits or list by district.
When I compare for example the name of the rabbits in the list with strcmp() it doesn't return 0 when the names are equal. How can I solve this problem?
The Rabbit struct:
typedef struct Rabbit {
char* name;
char* district;
unsigned part_count;
} Rabbit;
The delete method:
bool delete_rabbit(char* delete_name)
{
for (unsigned i = 0; i < size; ++i) {
if (iterator[i] != NULL && strcmp(iterator[i]->name, delete_name) == 0) {
free(iterator[i]);
iterator[i] = NULL;
count--;
return true;
}
}
return false;
}
The list by district method:
void list_by_district(char* district)
{
unsigned counter = 0;
for (unsigned i = 0; i < size; ++i) {
if (iterator[i] != NULL && strcmp(iterator[i]->district, district) == 0) {
counter++;
printf("\n%u. Rabbit:\n", counter);
printf("Name: %s\nDistrict: %s\nParticipation count: %u\n", iterator[i]->name, iterator[i]->district, iterator[i]->part_count);
}
}
}
The modify method is similar to the delete method except it only changes the values.
The corresponding code snippets from main:
Rabbit** iterator;
unsigned size = 10, count = 0;
int main()
{
iterator = (Rabbit**)malloc(sizeof(Rabbit*) * 10);
...
do {
...
switch (input) {
case 'a':
if (count == size) iterator = allocate_more_memory();
...
iterator[count++] = add_rabbit(new_name, new_district, new_part_count);
break;
case 'd':
if (size == count + 6) iterator = allocate_less_memory();
do {
printf("Enter name to be deleted: ");
scanf("%[^\n]", delete_name);
getchar();
if (strlen(delete_name) >= 30) printf("Name only has 30 or less characters!\n");
} while (strlen(delete_name) >= 30);
if (!delete_rabbit(delete_name)) printf("\nThere's no rabbit in the list with this name.\n");
break;
...
}
} while (input != 'x');
...
free(iterator);
return 0;
}
EDIT:
The add method:
Rabbit* add_rabbit(char* new_name, char* new_district, unsigned new_part_count)
{
Rabbit* new_rabbit = (Rabbit*)malloc(sizeof(Rabbit));
if (new_rabbit) {
new_rabbit->name = (char*)malloc((strlen(new_name) + 1) * sizeof(char));
new_rabbit->district = (char*)malloc((strlen(new_district) + 1) * sizeof(char));
strcpy(new_rabbit->name, new_name);
strcpy(new_rabbit->district, district);
new_rabbit->part_count = new_part_count;
}
return new_rabbit;
}
The allocate less memory method:
Rabbit** allocate_less_memory()
{
Rabbit** new_iterator = (Rabbit**)malloc(sizeof(Rabbit*) * (size - 5));
if (new_iterator) {
unsigned counter = 0;
for (unsigned i = 0; i < size; ++i) {
if (iterator[i] != NULL) {
new_iterator[counter++] = iterator[i];
}
}
size -= 5;
free(iterator);
return new_iterator;
}
return NULL;
}

No output from the program

In our student class we are building a small program to process the data from Oil & Gas Industry. The program at a rough condition, but we learn a lot through the project.
The program is compiled this way
In the below sections you can find the whole code of the source files. Here is the compilation routine:
gcc -fPIC -c parser.c -o parser.o
gcc -shared parser.c -o parser.dll
gcc -c well.c - o well.o
gcc -c main.c -o main.o
gcc main.o well.o -L. -lparser -o well.exe
Then we execute it this way: well.exe "WELL_DATA.csv" "NUM OF WELL TO FIND" >> well.log
The problem the execution starts, but the output from fprintf appears just sometimes, but usually not... We hope you can help us resolve our problem
main.c file content
#include"well.h"
FILE* input;
struct WELL* head = NULL; //the head of the list
struct TNode* tree = NULL;
int main(int argc, char *argv[3]) {
CBUFFER* buffer = malloc(sizeof(CBUFFER));
buffer->len = BUFFER_SIZE;
input = fopen(argv[1], "r");
struct WELL* instance = NULL;
while(buffer->len != 0){
flushBuffer(buffer);
getNextLine(input, buffer);
if (buffer->len == 0)
break;
parseData(buffer);
instance = createWell(buffer);
addWell(instance, &head);
}
instance = getWell(argv[2], head);
if (instance != NULL)
fprintf(stdout,"NAME = %s\tLIFT_TYPE = %i\tMONTHLY_OIL_FLOW_RATE = %.2f\n", instance->name, instance->prodMethod, instance->oil.monthly);
else fprintf(stdout,"No such well\n");
freeList(head);
fclose(input);
return 0;
}
In the following sections you can find the rest of the code. Here is the parser.c which processes the data:
#include "parser.h"
unsigned short getNextLine(FILE* input, CBUFFER* buffer){
//printf("Getting next line\n");
unsigned short len = 0;
char symbol = fgetc(input);
if (!input || !buffer)
return 0;
while ((symbol != '\n' && (symbol != EOF))){
//
*((buffer->data) + len) = symbol;
symbol = fgetc(input);
len++;
}
*((buffer->data) + len) = '\n';
buffer->len = len;
buffer->index = 0;
return buffer->len;
}
unsigned short parseData(CBUFFER* buffer){
char *temp = malloc(sizeof(char) * (buffer->len + 1));
unsigned short i = 0;
unsigned short j = 0;
while (*((buffer->data) + i) != '\n'){
if (*((buffer->data) + i) == '\0' || *((buffer->data) + i) == '\r' || *((buffer->data) + i) == '\v' ||
*((buffer->data) + i) == '\t' || *((buffer->data) + i) == '\f')
i++;
else{
if (*((buffer->data) + i) == ';')
*(temp + j) = '\0';
else
*(temp + j) = *((buffer->data) + i);
j++;
}
i++;
}
*(temp + j) = '\n';
flushBuffer(buffer);
i = 0;
while (*(temp + i) != '\n'){
*((buffer->data) + i) = *(temp + i);
i++;
}
*((buffer->data) + i) = '\0';
free(temp);
buffer->len = i;
return buffer->len;
}
char* getValue(CBUFFER* buffer)
{
int i = buffer->index;
int j = buffer->index;
char c;
for (; i <= buffer->len; i++)
{
c = buffer->data[i];
if (c == '\0')
{
char* newValue = (char*)malloc(sizeof(char) * (i - buffer->index + 1));
for (; j <= i ; j++)
{
newValue[j - buffer->index] = buffer->data[j];
}
newValue[j - buffer->index] = '\0';
buffer->index = i + 1;
return newValue;
}
}
return NULL;
}
DATE getDate(char* input, char delimeter){
DATE date;
unsigned short number = 0;
char c = input[0];
for(unsigned short i = 1, j = 0; i < 12; i++){
number = number * 10 + atoi(&c);
c = input[i];
if((c == delimeter) || (c == '\0')){
if (j == 0) date.day = number;
if (j == 1) date.month = number;
if (j == 2){
date.year = number;
break;
}
number = 0;
j++;
}
}
return date;
}
int flushBuffer(CBUFFER* buffer) {
unsigned short i = 0;
while( i < buffer->len){
*((buffer->data)+i) = '\0';
i++;
}
buffer->len = 0;
return 0;
}
And the part which defines the well-object we work with well.c:
#include "well.h"
struct WELL* createWell(CBUFFER* pbuffer) {
struct WELL* instance = malloc(sizeof(struct WELL));
char * nextValue = getValue(pbuffer);
unsigned i = 0;
while ( nextValue != NULL){
setProperty(instance, i, nextValue);
i++;
nextValue = getValue(pbuffer);
}
instance->next = NULL;
return instance;
}
unsigned setProperty(struct WELL* instance, unsigned prop_index, char* value) {
switch(prop_index){
case 0:
setWellName(instance, value);
break;
case 1:
setWellDate(instance, getDate(value, '/'));
break;
case 2:
setProductionMethod(instance, value);
break;
case 3:
setFormation(instance, value);
break;
case 4:
setPump(instance, value);
break;
case 5:
setProduction(instance, OIL, MONTH, atof(value));
break;
case 6:
setProduction(instance, OIL, YEAR, atof(value));
break;
case 7:
setProduction(instance, OIL, COMMULATIVE, atof(value));
break;
case 8:
setProduction(instance, WATER, MONTH, atof(value));
break;
case 9:
setProduction(instance, WATER, YEAR, atof(value));
break;
case 10:
setProduction(instance, WATER, COMMULATIVE, atof(value));
break;
// PAY ATTENTION THAT cases from 11 to 13 are skipped!
// THIS IS BECAUSE WE DON'T NEED m3 PARAMETERS as it is
// AND CAN CALCULATE THEM from tons using density parameter
case 14:
setProduction(instance, MIX, MONTH, atof(value));
break;
case 15:
setProduction(instance, MIX, YEAR, atof(value));
break;
case 16:
setProduction(instance, MIX, COMMULATIVE, atof(value));
break;
case 17:
setProduction(instance, MIX, MONTH, atof(value));
break;
case 18:
setProduction(instance, MIX, YEAR, atof(value));
break;
case 19:
setProduction(instance, MIX, COMMULATIVE, atof(value));
break;
}
return 0;
}
unsigned addWell(struct WELL* instance, struct WELL** head) {
if (*head == NULL) {
*head = instance;
} else {
struct WELL* tmp = *head;
while (tmp->next){
tmp = tmp->next;
}
tmp->next = instance;
}
return 0;
}
struct WELL* getWell(char* key, struct WELL* head) {
if (head == NULL) return NULL;
struct WELL* currentWell = head;
while (strcmp(currentWell->name, key) != 0) {
if (currentWell->next == NULL) return NULL;
currentWell = currentWell->next;
}
return currentWell;
}
char* setWellName(struct WELL* instance, char* value) {
strcpy(instance->name, value);
return instance->name;
}
char* setFormation(struct WELL* instance, char* value) {
strcpy(instance->devFormation, value);
return instance->devFormation;
}
char* setPump(struct WELL* instance, char* value) {
strcpy(instance->pump, value);
return instance->pump;
}
DATE setWellDate(struct WELL* instance, DATE date) {
instance->startDate.day = date.day;
instance->startDate.month = date.month;
instance->startDate.year = date.year;
return instance->startDate;
}
WLIFT_TYPE setProductionMethod(struct WELL* instance, char* value) {
if (!strcmp(value, "flush"))
instance->prodMethod = FLUSH;
if (!strcmp(value, "gaslift"))
instance->prodMethod = GAS_LIFT;
if (!strcmp(value, "pump"))
instance->prodMethod = PUMP;
return instance->prodMethod;
}
double setProduction(struct WELL* instance, WFLUID fluid, WPERIOD period , double value) {
if (fluid == OIL) {
if (period == MONTH) {
instance->oil.monthly = value;
return instance->oil.monthly;
} else if (period == YEAR) {
instance->oil.year = value;
return instance->oil.year;
} else if (period == COMMULATIVE) {
instance->oil.commulative = value;
return instance->oil.commulative;
}
} else if (fluid == GAS) {
if (period == MONTH) {
instance->gas.monthly = value;
return instance->gas.monthly;
} else if (period == YEAR) {
instance->gas.year = value;
return instance->gas.year;
} else if (period == COMMULATIVE) {
instance->gas.commulative = value;
return instance->gas.commulative;
}
} else if (fluid == WATER) {
if (period == MONTH) {
instance->water.monthly = value;
return instance->water.monthly;
} else if (period == YEAR) {
instance->water.year = value;
return instance->water.year;
} else if (period == COMMULATIVE) {
instance->water.commulative = value;
return instance->water.commulative;
}
} else if (fluid == MIX) {
if (period == MONTH) {
instance->liquid.monthly = value;
return instance->liquid.monthly;
} else if (period == YEAR) {
instance->liquid.year = value;
return instance->liquid.year;
} else if (period == COMMULATIVE) {
instance->liquid.commulative = value;
return instance->liquid.commulative;
}
}
}

How to store a buffer of single characters and print them out as a string?

I posted a few questions about my project already and its all been helpful but I'm still unsure of one part of it.
I'm creating a translator that converts ASCII to ASH and ASH to ASCII. I have now successfully converted ASCII to ASH but can't convert from ASH to ASCII. I've created a function called 'ASCIIstring2ASHstring' which works fine, but I now have to create another function called 'ASHstring2ASCIIstring'. However, I've been told that for this function I need to save each character the user enters into a temporary buffer until a '/' appears, at which point I need to print out all the characters in the temporary buffer as a full string by calling the 'ASHstring2ASCIIstring' function (which I'm asking for help creating now) . However, I have no idea how I'd do this and I hoped someone here could lend me a hand :)
Thanks!
UPDATE: I have now created the function with some help, but for some reason its not printing out ASH to ASCII translations as expected, I have posted the updated code below.
char ASH_a[] = "*";
char ASH_b[] = "!!*";
char ASH_c[] = "!!#";
char ASH_d[] = "*!";
char ASH_e[] = "!";
char ASH_f[] = "!*!";
char ASH_g[] = "**";
char ASH_h[] = "!**";
char ASH_i[] = "!!";
char ASH_j[] = "*!!";
char ASH_k[] = "*#";
char ASH_l[] = "!*#";
char ASH_m[] = "!*";
char ASH_n[] = "!#";
char ASH_o[] = "#*";
char ASH_p[] = "!#!";
char ASH_q[] = "*!#";
char ASH_r[] = "!#*";
char ASH_s[] = "#!";
char ASH_t[] = "#";
char ASH_u[] = "##";
char ASH_v[] = "**!";
char ASH_w[] = "***";
char ASH_x[] = "*#!";
char ASH_y[] = "!##";
char ASH_z[] = "**#";
//char ASH_1[] = "#!!*";
//char ASH_2[] = "#!!#";
//char ASH_3[] = "#!*!";
//char ASH_4[] = "#!**";
//char ASH_5[] = "#!*#";
//char ASH_6[] = "#!#!";
//char ASH_7[] = "#!#*";
//char ASH_8[] = "#!##";
//char ASH_9[] = "#*!!";
//char ASH_0[] = "#!!!";
//char ASH_.[] = "#!!";
//char ASH_,[] = "#!*";
//char ASH_?[] = "#**";
//char ASH_![] = "#*#";
//char ASH_+[] = "##!";
//char ASH_-[] = "##*";
//char ASH_*[] = "###";
//char ASH_/[] = "#!#";
char t[] = "ash code";
char buffer1[100];
const int oblen = 100;
char ob [oblen];
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// Serial.println(ASH2ASCII("!**")); //These are the ASH characters I want to convert to ASCII using the ASH2ASCII function
//Serial.println(ASH2ASCII("!"));
//Serial.println(ASH2ASCII("!*#"));
//Serial.println(ASH2ASCII("!*#"));
//Serial.println(ASH2ASCII("#*"));
//ASCIIstring2ASHstring (t, ob, oblen);
//Serial.println(ob);
usinput(buffer1);
Serial.print(buffer1);
chardecide(buffer1);
}
void chardecide(char * buffer1) { //char decide which acts upon the result of isASH using the true and false returns
if (isASH (buffer1)) {
Serial.println(" has been recognized as ASH - ");
ASHstring2ASCIIstring(buffer1, ob); //passes function with buffer1 and ob included
Serial.println(ob);
} else {
Serial.println(" has been recognized as ASCII - ");
ASCIIstring2ASHstring (buffer1, ob, oblen);
Serial.println(ob);
}
}
void usinput(char * ib ) {
char inChar;
int i = 0;
do {
while (!Serial.available()) {};
inChar = Serial.read();
if (inChar == '\n') {
break;
} else {
ib[i] = inChar;
i++;
}
ib[i] = '\0';
} while (true);
}
bool isASH(char * buffer1)
{
if (buffer1[0] != '*' && buffer1[0] != '!' && buffer1[0] != '#') return false;
return true;
}
int ASHstring2ASCIIstring(char *buffer, char *ob) //converts ash to ascii
{
char str[10];
int j = 0;
int l = 0;
while (*buffer) {
if (*buffer == '/') { //hit slash
str[j] = '\0'; //empty buffer
ob[l++] = ASH2ASCII(str);
j = 0;
} else {
if (j + 1 < sizeof(str)) {
str[j++] = *buffer;
}
}
buffer++;
}
ob[l] = '\0';
return l;
}
void ASCIIstring2ASHstring (char * ip, char * op, int oplen) { //converts ascii to ash
op[0] = '\0';
int bp = 0;
int n;
char m[9];
int l = strlen(ip);
for (int i = 0; i < l; i++) {
m[0] = '\0';
strcpy(m, ASCII2ASH(ip[i]));
n = strlen(m);
if ((bp + n + l) < oplen) {
strcat(op , m);
bp = bp + n;
}
if (ip[i] != ' ' && ip[i + l] != ' ') {
op[bp] = '/';
bp++;
}
op[bp] = '\0';
}
}
char ASH2ASCII(char * m) { //Using the char variables impmented globally, ASH2ASCII searches through specific variables until it finds a match for the conversion, at which point it will capture and reuturn the ASCII string
if (strcmp(ASH_a, m) == 0) { //if string captured return a
return 'a';
}
else if (strcmp(ASH_b, m) == 0) { //else if b string is captured return
return 'b';
}
else if (strcmp(ASH_c, m) == 0) {
return 'c';
}
else if (strcmp(ASH_d, m) == 0) {
return 'd';
}
else if (strcmp(ASH_e, m) == 0) {
return 'e';
}
else if (strcmp(ASH_f, m) == 0) {
return 'f';
}
else if (strcmp(ASH_g, m) == 0) {
return 'g';
}
else if (strcmp(ASH_h, m) == 0) {
return 'h';
}
else if (strcmp(ASH_i, m) == 0) {
return 'i';
}
else if (strcmp(ASH_j, m) == 0) {
return 'j';
}
else if (strcmp(ASH_k, m) == 0) {
return 'k';
}
else if (strcmp(ASH_l, m) == 0) {
return 'l';
}
else if (strcmp(ASH_m, m) == 0) {
return 'm';
}
else if (strcmp(ASH_n, m) == 0) {
return 'n';
}
else if (strcmp(ASH_o, m) == 0) {
return 'o';
}
else if (strcmp(ASH_p, m) == 0) {
return 'p';
}
else if (strcmp(ASH_q, m) == 0) {
return 'q';
}
else if (strcmp(ASH_r, m) == 0) {
return 'r';
}
else if (strcmp(ASH_s, m) == 0) {
return 's';
}
else if (strcmp(ASH_t, m) == 0) {
return 't';
}
else if (strcmp(ASH_u, m) == 0) {
return 'u';
}
else if (strcmp(ASH_v, m) == 0) {
return 'v';
}
else if (strcmp(ASH_w, m) == 0) {
return 'w';
}
else if (strcmp(ASH_x, m) == 0) {
return 'x';
}
else if (strcmp(ASH_y, m) == 0) {
return 'y';
}
else if (strcmp(ASH_z, m) == 0) {
return 'z';
}
}
void ASCIIstring2ASH (char * buf) {
Serial.println(ASCII2ASH(*t));
}
char * ASCII2ASH (char c) { //This is the opposire of ASH2ASCII, it uses the globally defined variables to search through ASCII characters, and returns the ASH version of that character
switch (c) {
case 'a':
return ASH_a;//returns ASH version of a if matched
case 'b':
return ASH_b;
case 'c':
return ASH_c;
case 'd':
return ASH_d;
case 'e':
return ASH_e;
case 'f':
return ASH_f;
case 'g':
return ASH_g;
case 'h':
return ASH_h;
case 'i':
return ASH_i;
case 'j':
return ASH_j;
case 'k':
return ASH_k;
case 'l':
return ASH_l;
case 'm':
return ASH_m;
case 'n':
return ASH_n;
case 'o':
return ASH_o;
case 'p':
return ASH_p;
case 'q':
return ASH_q;
case 'r':
return ASH_r;
case 's':
return ASH_s;
case 't':
return ASH_t;
case 'u':
return ASH_u;
case 'v':
return ASH_v;
case 'w':
return ASH_w;
case 'x':
return ASH_x;
case 'y':
return ASH_y;
case 'z':
return ASH_z;
case ' ':
return " ";
default:
Serial.println("switching went wrong!");
break;
}
}
void loop() {
}
In <string.h>, there is a function strtok that splits a string on a range of given characters. You could use it like this:
int ASHstring2ASCIIstring(char *buffer, char *ob)
{
char *token;
int l = 0;
token = strtok(buffer, "/");
while (token) {
println(token);
ob[l++] = ASH2ASCII(token); // Note: No overflow check!
token = strtok(NULL, "/");
}
ob[l] = '\0';
return l;
}
On the first call, you supply the string to split, on subsequent calls you pass NULL to tell strtok to keep working on the same string. This code destroys the original string in the process, because it places end markers at the end of the tokens.
You can also code that behaviour yourself by filling an auxiliary buffer. When you hit a slash, process the buffer and empty it:
int ASHstring2ASCIIstring(char *buffer, char *ob)
{
char str[10];
int j = 0;
int l = 0;
while (*buffer) {
if (*buffer == '/') {
str[j] = '\0';
ob[l++] = ASH2ASCII(str);
j = 0;
} else {
if (j + 1 < sizeof(str)) {
str[j++] = *buffer;
}
}
buffer++;
}
ob[l] = '\0';
return l;
}
This code leaves the string intact. It also requires that there is a slash after the last token. That may not be what you want, but it might be a good starting point.

error using structs, C

i am getting the error "a3.c:221:20: error: storage size of ‘gold’ isn’t known" for all 4 of my item structs. My code is as follows:
void parser(int argc, char **argv)
{
FILE * rooms;
char * theString;
char * theToken;
int numberElements;
int side;
int k;
int placeInt;
int posX;
int posY;
char a[ROOM_STRING_LENGTH];
numberElements = 0;
rooms = fopen(argv[1], "r");
if(rooms == NULL)
{
printf("error opening file\n");
}
while(fgets(a, ROOM_STRING_LENGTH, rooms) != NULL)
{
theString = malloc((sizeof(char)*(strlen(a)+1)));
strcpy(theString, a);
for(theToken = strtok(theString, " "); theToken; theToken = strtok(NULL, " "))
{
printf("the next token: %s\n", theToken);
if(theToken[0] == '1')
{
}
if(theToken[0] == 'd')
{
switch(theToken[1])
{
case 'e':
{
side = 1;
placeInt = theToken[2] - '0';
printf("the side: %d, the place: %d\n", side, placeInt);
break;
}
case 'w':
{
side = 2;
placeInt = theToken[2] - '0';
printf("the side: %d, the place: %d\n", side, placeInt);
break;
}
case 's':
{
side = 3;
placeInt = theToken[2] - '0';
printf("the side: %d, the place: %d\n", side, placeInt);
break;
}
case 'n':
{
side = 4;
placeInt = theToken[2] - '0';
printf("the side: %d, the place: %d\n", side, placeInt);
break;
}
default:
{
break;
}
}
}
else if(theToken[0] == 'g' || theToken[0] == 'm' || theToken[0] == 'p' || theToken[0] == 'h')
{
k = 0;
while(k <= (strlen(theToken)))
{
switch(theToken[k])
{
case 'g':
posY = theToken[1] - '0';
posX = theToken[3] - '0';
struct item gold;
gold.Xposition = posX;
gold.Yposition = posY;
printf("the y position: %d, the x position: %d\n", posY, posX);
break;
case 'm':
posY = theToken[1] - '0';
posX = theToken[3] - '0';
struct item monster;
monster.Xposition = posX;
monster.Yposition = posY;
printf("the y position: %d, the x position: %d\n", posY, posX);
break;
case 'p':
posY = theToken[1] - '0';
posX = theToken[3] - '0';
struct item potion;
potion.Xposition = posX;
potion.Yposition = posY;
printf("the y position: %d, the x position: %d\n", posY, posX);
break;
case 'h':
posY = theToken[1] - '0';
posX = theToken[3] - '0';
struct item hero;
hero.Xposition = posX;
hero.Yposition = posY;
printf("the y position: %d, the x position: %d\n", posY, posX);
break;
}
k++;
}
}
else if(theToken == NULL)
{
printf("end of file");
}
numberElements++;
}
if(theToken == NULL)
{
printf("You've reached the end of the line\n");
}
printf("%d\n", numberElements);
}
free(theString);
fclose(rooms);
}
struct item
{
int Xposition;
int Yposition;
};
Also, I was wondering how i would go about accessing the information i just stored into those structs in a different function.
As keltar and nonsensickle already mentioned, you have to define struct item before you can use an instance of it:
struct item { int x; int y; }; // this must come first
// ...
struct item item1 {4, 2};
You could, however, use a pointer before the definition, as long as you have already declared the struct:
struct item; // declaration, no definition
// ...
struct item *pitem1;
// ...
struct item { int x; int y; }; // defined later
To use a struct's members in another function, you could pass either a struct or a struct* to that function:
void use_struct (struct item i)
{
int a = i.x, b = i.y;
}
void use_struct_pointer (struct item *pi)
{
int a = pi->x, b = pi->y;
}
int main()
{
struct item i {4, 2};
use_struct(i);
use_struct_pointer(&i);
return 0;
}

Resources