I've used strcmp before and it worked as expected, but it's not working for me in my current code.
I'm reading a .csv file with the names of a bunch of famous people. "Mark Zuckerberg" is the key name that triggers things that my code will eventually do (once I get past this bump in the road and this has nothing to do with what he's been in the news for lately). I'm using a counter (queue_size) to count the number of lines in the .csv file. My goal is to save the value of the counter when strcmp(temp_name, key) == 0 but I'm not entering that if statement and I can't see why.
The key appears in the .csv file as "Mark,Zuckerberg". I've tried using strtok to eliminate the comma. I was successful in doing that but strcmp() still isn't working (I adjusted the key to be "MarkZuckerberg"). I also added memset to clean the slate with each iteration but that didn't resolve the issue either.
Commenting the line, temp_name[strlen(temp_name) - 1] = '\0'; doesn't appear to change anything either. I know that my struct is getting all of the names because printf (I've since deleted) and my print_list function prints as expected.
I really need help finding out why I'm not entering that if statement.
Thanks in advance for any help that anyone can provide.
I think that it's something dumb that I'm overlooking but I just can't find it.
Here's my code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct char_node {
char name[40];
struct char_node *next;
} char_node;
typedef struct char_queue {
char_node *q_head;
char_node *q_tail;
int q_size;
} char_queue;
void enqueue(char_queue *q_ptr, char new_name[40]);
//int dequeue(char_queue *q_ptr);
void print_list(char_queue *queue);
int main() {
int queue_size = 0;
int m_z_position;
char_queue queue;
char temp_name[40];
char key[] = "Mark,Zuckerberg";
queue.q_head = NULL;
queue.q_tail = NULL;
queue.q_size = 0;
FILE *file_input;
file_input = fopen("party.csv", "r");
memset(temp_name, '\0', sizeof(temp_name));
while(fgets(temp_name, sizeof(temp_name), file_input)) {
temp_name[strlen(temp_name) - 1] = '\0';
if(strcmp(temp_name, key) == 0) {
printf("test\n");
m_z_position = queue_size;
}
enqueue(&queue, temp_name);
memset(temp_name, '\0', sizeof(temp_name));
queue_size++;
}
fclose(file_input);
//print_list(&queue);
printf("m_z_position = %d\n", m_z_position);
return 0;
}
void enqueue(char_queue *q_ptr, char new_name[40]) {
char_node *new_node = (char_node*)malloc(sizeof(char_node));
strcpy(new_node->name, new_name);
new_node->next = NULL;
int num;
if(q_ptr->q_size == 0) {
q_ptr->q_tail = new_node;
q_ptr->q_head = new_node;
} else {
q_ptr->q_tail->next = new_node;
q_ptr->q_tail = new_node;
}
(q_ptr->q_size)++;
return;
}
void print_list(char_queue *queue) {
char_node *temp_list;
if(queue->q_head != NULL) {
temp_list = queue->q_head;
while(temp_list != NULL) {
printf("%s\n", temp_list->name);
temp_list = temp_list->next;
}
}
printf("\n");
return;
}
I can't figure out how to add the file but here are the contents of the .csv file
Jeff,Bezo
Bill,Gates
Warren,Buffett
Berkshire,Hathaway
Bernard,Arnault
Amancio,Ortega
Carlos,Slim
Charles,Koch
David,Koch
Larry,Ellison
Michael,Bloomberg
Larry,Page
Sergey,Brin
Jim,Walton
S,Robson
Alice,Walton
Ma,Huateng
Francoise,Bettencourt
Mukesh,Ambani
Jack,Ma
Sheldon,Adelson
Steve,Ballmer
Li,Ka-shing
Hui,Ka
Lee,Shau
Wang,Jianlin
Beate,Heister
Phil,Knight
Jorge,Paulo
Francois,Pinault
Georg,Schaeffler
Susanne,Klatten
David,Thomson
Jacqueline,Mars
John,Mars
Joseph,Safra
Giovanni,Ferrero
Dietrich,Mateschitz
Michael,Dell
Masayoshi,Son
Serge,Dassault
Stefan,Quandt
Yang,Huiyan
Paul,Allen
Leonardo,Del
Dieter,Schwarz
Thomas,Peterffy
Theo,Albrecht
Len,Blavatnik
He,Xiangjian
Lui,Che
James,Simons
Henry,Sy
Elon,Musk
Hinduja,family
Tadashi,Yanai
Vladimir,Lisin
Laurene,Powell
Azim,Premji
Alexey,Mordashov
Lee,Kun-Hee
Lakshmi,Mittal
Wang,Wei
Leonid,Mikhelson
Charoen,Sirivadhanabhakdi
Pallonji,Mistry
Ray,Dalio
Takemitsu,Takizaki
William,Ding
R,Budi
Gina,Rinehart
German,Larrea
Carl,Icahn
Stefan,Persson
Michael,Hartono
Joseph,Lau
Thomas,A
Vagit,Alekperov
James,Ratcliffe
Donald,Bren
Iris,Fontbona
Gennady,Timchenko
Abigail,Johnson
Vladimir,Potanin
Lukas,Walton
Charlene,de
Zhang,Zhidong
Petr,Kellner
Andrey,Melnichenko
David,A
Klaus-Michael,Kuehne
Li,Shufu
Mikhail,Fridman
Rupert,Murdoch
Dhanin,Chearavanont
Robert,Kuok
Emmanuel,Besnier
Shiv,Nadar
Viktor,Vekselberg
Aliko,Dangote
Harold,Hamm
Steve,Cohen
Dustin,Moskovitz
Marcel,Herrmann
Reinhold,Wuerth
Charles,Ergen
Eric,Schmidt
Philip,Anschutz
Jim,Kennedy
Blair,Parry-Okeden
Alain,Wertheimer
Gerard,Wertheimer
Leonard,Lauder
Heinz,Hermann
Dilip,Shanghvi
Hasso,Plattner
Stephen,Schwarzman
Lei,Jun
Hans,Rausing
Alisher,Usmanov
Donald,Newhouse
Peter,Woo
Luis,Carlos
Robin,Li
Carlos,Alberto
Seo,Jung-Jin
Kumar,Birla
Alexander,Otto
Stefano,Pessina
Udo,A
Wang,Wenyin
Andrew,Beal
Lee,Man
John,Menard
Xu,Shihui
Zhou,Hongyi
Gong,Hongjia
Michael,Otto
David,Tepper
Roman,Abramovich
Liu,Qiangdong
Robert,A
Alberto,Bailleres
Uday,Kotak
Pierre,Omidyar
Walter,PJ
Dietmar,Hopp
Graeme,Hart
Eduardo,Saverin
Yan,Zhi
Radhakishan,Damani
German,Khan
Ronald,Perelman
Gautam,Adani
Micky,Arison
Pan,Zhengmin
Joseph,Tsai
Thomas,Frist
Mikhail,Prokhorov
Galen,Weston
Zong,Qinghou
Eyal,Ofer
Charles,Schwab
Gianluigi,A
Herbert,Kohler
Viktor,Rashnikov
Harry,Triguboff
August,von
Yao,Zhenhua
Jan,Koum
Cyrus,Poonawalla
James,Goodnight
Ken,Griffin
Giorgio,Armani
Ernesto,Bertarelli
Savitri,Jindal
Sunil,Mittal
James,Chambers
Katharine,Rayner
Margaretta,Taylor
Terry,Gou
Gordon,Moore
James,Irving
Stanley,Kroenke
Melker,Schorling
Johann,Graf
Guo,Guangchang
John,Malone
Xavier,Niel
Silvio,Berlusconi
Carl,Cook
David,Geffen
Hui,Wing
Walter,Kwok
George,Soros
Edward,Johnson
Massimiliana,Landini
David,Duffield
George,Kaiser
Patrick,Soon-Shiong
Zhou,Qunfei
Nicky,Oppenheimer
Sun,Piaoyang
Wu,Yajun
Alexei,Kuzmichev
Stephen,Ross
Vincent,Bollore
Pauline,MacMillan
Jay,Y
Anders,Holch
Eli,Broad
Michael,Kadoorie
Iskander,Makhmudov
Frederik,Paulsen
Sun,Hongbin
Christy,Walton
Shahid,Khan
Ananda,Krishnan
Carrie,Perrodo
Quek,Leng
Wang,Wenxue
John,Doerr
Patrick,Drahi
Eva,Gonda
Willi,A
Ricardo,Salinas
Suh,Kyung-Bae
Pollyanna,Chu
John,Fredriksen
Goh,Cheng
Sri,Prakash
Lu,Zhiqiang
Jorn,Rausing
Johann,Rupert
Jacques,Saade
Wu,Shaoxun
Leonid,Fedun
Kim,Jung-Ju
Sandra,Ortega
Jim,Pattison
Michael,Platt
Chan,Laiwa
David,Green
Hank,A
Dmitry,Rybolovlev
Tsai,Eng-Meng
Andreas,von
Oleg,Deripaska
Liu,Yongxing
Ludwig,Merckle
Brian,Acton
John,Grayken
Ann,Walton
Augusto,A
Finn,Rausing
Mark,Zuckerberg
Kirsten,Rausing
Odd,Reitan
Nassef,Sawiris
Wee,Cho
Aloys,Wobben
Leon,Black
Ivan,Glasenberg
John,Paulson
Wei,Jianjun
Francis,Choi
Erivan,Haub
Jason,Jiang
Suleiman,Kerimov
Ian,A
Pang,Kang
David,Shaw
Kushal,Pal
John,A
Acharya,Balkrishna
Guenther,Fielmann
Daniel,Gilbert
Antonia,Johnson
Vikram,Lal
Akira,Mori
Maria-Elisabeth,Schaeffler-Thumann
Albert,Frere
Richard,Kinder
Robert,Kraft
Ralph,Lauren
Bruno,Schroder
Nusli,Wadia
Pierre,Bellon
Les,Wexner
Benu,Gopal
David,Cheriton
Ma,Jianrong
Whitney,MacMillan
Dan,Olsson
Vivek,Chaand
Teh,Hong
Abdulla,bin
Maria,Asuncion
Ralph,Dommermuth
Frank,Lowy
Wolfgang,Marguerre
Marijke,Mars
Pamela,Mars
Valerie,Mars
Victoria,Mars
David,A
John,Gokongwei
Kwon,Hyuk-Bin
Nancy,Walton
Lin,Yu-Ling
Tom,A
Robert,Rowling
Dennis,Washington
Yao,Liangsong
Zhang,Jindong
Juan,Francisco
David,Sun
John,Tu
Martin,Viessmann
Stef,Wertheimer
Hansjoerg,Wyss
James,Dyson
Laurence,Graff
Jen-Hsun,Huang
Charles,Johnson
Jerry,Jones
Kei,Hoi
Kwee,family
Lee,Shin
Richard,LeFrak
Shigenobu,Nagamori
Steven,Rales
Friede,Springer
Yeung,Kin-man
Rinat,Akhmetov
Shari,Arison
Dannine,Avara
Rahel,Blocher
Andrew,Currie
Scott,Duncan
Milane,Frantz
Diane,Hendricks
Magdalena,Martullo-Blocher
Hiroshi,Mikitani
Gabe,Newell
Pan,Sutong
Anthony,Pratt
John,Reece
Randa,Williams
Zhang,Bangxin
I was fixing your code, but as this comment already state, your file use "\r\n" as end line code, can be fixed with str[strcspn(str, "\r\n")] = '\0'; just after your read.
But here, an other exemple of implementation of your code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct char_node {
struct char_node *next;
char name[];
} char_node;
typedef struct char_queue {
char_node *q_head;
char_node *q_tail;
size_t q_size;
} char_queue;
char_node *enqueue(char_queue *q_ptr, char const *new_name);
void print_list(char_queue const *queue);
int main(void) {
char_queue queue = { .q_head = NULL, .q_tail = NULL, .q_size = 0 };
char const key[] = "Mark,Zuckerberg";
FILE *file_input = fopen("party.csv", "r");
if (file_input == NULL) {
file_input = stdin;
}
char str[40];
size_t m_z_position = 0;
while (fgets(str, sizeof str, file_input)) {
str[strcspn(str, "\r\n")] = '\0';
if (strcmp(str, key) == 0) {
m_z_position = queue.q_size;
}
enqueue(&queue, str);
}
fclose(file_input);
print_list(&queue);
printf("m_z_position = %zu\n", m_z_position);
}
char_node *enqueue(char_queue *q_ptr, char const *name) {
size_t i = strlen(name) + 1;
char_node *node = malloc(sizeof *node + i);
if (!node) {
return NULL;
}
strcpy(node->name, name);
node->next = NULL;
if (q_ptr->q_size++ == 0) {
q_ptr->q_tail = q_ptr->q_head = node;
} else {
q_ptr->q_tail = q_ptr->q_tail->next = node;
}
return node;
}
void print_list(char_queue const *queue) {
for (char_node const *list = queue->q_head; list; list = list->next) {
printf("%s\n", list->name);
}
printf("\n");
}
I am afraid that .csv file contains "Mark,Zuckerberg" not Mark,Zuckerberg.
In if(strcmp(temp_name, key) == 0){ key is compared with temp_name.
Here key is Mark,Zuckerberg.
int strcmp(const char *s1, const char *s2);
The strcmp() and strncmp()
functions return an integer greater than, equal to, or less than 0,
according as the string s1 is greater than, equal to, or less than the
string s2.
strcmp will return positive number if temp_name is "Mark,Zuckerberg" because it contains additional 2 characters and, 0 if temp_name is Mark,Zuckerberg as key here is Mark,Zuckerberg clearly.
I stuck to send a stasis_message for a self made module to the ARI.
I try to use the code example from the documentation :
https://wiki.asterisk.org/wiki/display/AST/Stasis+Message+Bus
I use asterisk 13 instead example (who use the 12), and some signature are changed.
Here is the initialisation :
struct stasis_topic *foo_topic;
static int load_module(void)
{
// Register to stasis.
stasis_app_register(app, callback_stasis, 0);
// Create a bridge on witch ARI can conenct.
stasis_app_bridge_create("mixing", app, "11000");
// Create the topic
foo_topic = stasis_topic_create(app);
return ast_register_application_xml(app, exec);
}
And the code method who is calling when phone arrive :
static int exec()
{
publish_foo();
}
static void publish_foo()
{
printf("Trace 1\n");
char* test = "dataToSend";
RAII_VAR(struct stasis_message_type*, foo_type, NULL, ao2_cleanup);
stasis_message_type_create(app, NULL, &foo_type);
RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
printf("Trace 3\n");
msg = stasis_message_create(type, test);
if (!msg)
return;
stasis_publish(foo_topic, msg);
printf("PASSING MESSAGE 4\n");
}
I always get message like :
bad magic number 0x332065 for object 0x7f2ea5ab8ec5
And this error appends in the method stasis_create_message().
[Edit]
I do not understand the error and the cause any help is appreciated.
As suggest by arheops, there is the function who are created problem. Apparently my object cannot be convert to an Asterisk object. Probably the structure I need to send to the create_message_function must be on a astobj2 type.
static struct astobj2 *INTERNAL_OBJ(void *user_data)
{
struct astobj2 *p;
if (!user_data) {
ast_log(LOG_ERROR, "user_data is NULL\n");
return NULL;
}
p = (struct astobj2 *) ((char *) user_data - sizeof(*p));
if (AO2_MAGIC != p->priv_data.magic) {
if (p->priv_data.magic) {
ast_log(LOG_ERROR, "bad magic number 0x%x for object %p\n",
p->priv_data.magic, user_data);
} else {
ast_log(LOG_ERROR,
"bad magic number for object %p. Object is likely destroyed.\n",
user_data);
}
ast_assert(0);
return NULL;
}
return p;
}
And the struct of astobj2 definition :
struct astobj2
{
struct __priv_data priv_data;
void *user_data[0];
};
I tried to create a a2object like describe here, and I get an error :
*** Error in `asterisk': free(): invalid pointer:
Thanks
To send a stasis message, you need to create an a2object, normally you can perform this part with the macro :
RAII_VAR
But I cannot get a working example with this, so I create my-self the object with the following methods :
typedef struct ast_foo
{
int n;
} ast_foo;
// Destructor is automatically called when the message is not referenced anymore.
static void foo_dtor(void *obj)
{
struct foo *obj_foo = obj;
// Free all resources you have reserve here.
}
/**
* #return a ast_foo struct, with destructor setted.
*/
static struct ast_foo* make_me_a_foo(void)
{
struct ast_foo *obj_foo;
obj_foo = ao2_alloc(sizeof(ast_foo), foo_dtor);
// if char* do malloc for them.
if (!obj_foo) {
ast_log(LOG_NOTICE, "make foo failed... 2\n");
return NULL;
}
return obj_foo;
}
There is a full example for send and subscribe a stasis message :
static const char app[] = "StasisTest";
struct stasis_topic *foo_topic;
typedef struct ast_foo
{
int n;
} ast_foo;
// Destructor automatically call when message is not referenced anymore.
static void foo_dtor(void *obj)
{
struct foo *obj_foo = obj;
// Free all resources you have reserve here.
}
/**
* #return a ast_foo struct, with destructor setted.
*/
static struct ast_foo* make_me_a_foo(void)
{
struct ast_foo *obj_foo;
obj_foo = ao2_alloc(sizeof(ast_foo), foo_dtor);
// if char* do malloc for them.
if (!obj_foo) {
ast_log(LOG_NOTICE, "make foo failed... 2\n");
return NULL;
}
return obj_foo;
}
/**
* Send a stasis message, with the long way...
*/
static void publish_foo()
{
ast_log(LOG_NOTICE, "Enter publish message\n");
RAII_VAR(struct stasis_message_type*, foo_type, NULL, ao2_cleanup);
ast_log(LOG_NOTICE, "Create data to send\n");
ast_foo* foo_data = make_me_a_foo();
foo_data->n = 12;
ast_log(LOG_NOTICE, "Create the message to send.\n");
stasis_message_type_create(app, NULL, &foo_type);
if (!foo_type)
{
ast_log(LOG_NOTICE, "Oh no my type is NULL \n");
}
else
{
ast_log(LOG_NOTICE, "Ok foo type \n");
}
RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
msg = stasis_message_create(foo_type, foo_data);
if (!msg)
{
ast_log(LOG_NOTICE, "Fail to send message\n");
sleep(1);
return;
}
stasis_publish(foo_topic, msg);
}
static int exec()
{
// First method.
publish_foo();
return 0;
}
static int unload_module(void) {
stasis_app_unregister(app);
ao2_cleanup(foo_topic);
foo_topic = NULL;
return ast_unregister_application(app);
}
void bar_callback(void *data, struct stasis_subscription *sub, struct stasis_message *message)
{
ast_log(LOG_NOTICE, "Test stasis received a message from topic\n");
}
static int load_module(void) {
stasis_init();
// Register.
ast_foo* foo_data2 = make_me_a_foo();
foo_topic = stasis_topic_create("StasisTest");
stasis_subscribe(foo_topic, bar_callback, foo_data2);
return ast_register_application_xml(app, exec);
}
But there is a really more simpler way with the send of json object tought
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/astobj2.h"
#include "asterisk/module.h"
#include "asterisk/stasis.h"
#include "asterisk/json.h"
#include "asterisk/stasis_app.h"
#include "StasisTest.h"
#define AST_MODULE "stasis_test"
static const char app[] = "StasisTest";
static int exec()
{
// Second simpler method.
struct ast_json* inte = ast_json_integer_create(51);
int result = stasis_app_send("StasisTest", inte);
ast_log(LOG_NOTICE, "Stasis send %d\n", result);
return 0;
}
static int unload_module(void)
{
stasis_app_unregister(app);
return ast_unregister_application(app);
}
//Test stasis
void callback_stasis(void* data, const char* app_name, struct ast_json* message)
{
ast_log(LOG_NOTICE, "Receive a stasis message from json\n");
int json_res = ast_json_integer_get(message);
ast_log(LOG_NOTICE, "Integer get : %d\n", json_res);
}
static int load_module(void) {
stasis_init();
// Register for the short way.
stasis_app_register(app, callback_stasis, 0);
return ast_register_application_xml(app, exec);
}
AST_MODULE_INFO(ASTERISK_GPL_KEY, 0, "The wonders of foo", .load = load_module, .unload = unload_module);