Skip to content

Commit

Permalink
Added r_str_trunc_ellipsis() to truncate node titles too long
Browse files Browse the repository at this point in the history
Removed comments and fixed r_agraph_get_node()
  • Loading branch information
cyanpencil authored and radare committed May 22, 2018
1 parent 4c040ae commit fc82e86
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
16 changes: 11 additions & 5 deletions libr/core/graph.c
Expand Up @@ -3317,15 +3317,16 @@ R_API RANode *r_agraph_add_node(const RAGraph *g, const char *title, const char
if (!res) {
return NULL;
}
res->title = title? strdup (title): strdup ("");

res->title = title? r_str_trunc_ellipsis (title, 255) : strdup ("");
res->body = body? strdup (body): strdup ("");
res->layer = -1;
res->pos_in_layer = -1;
res->is_dummy = false;
res->is_reversed = false;
res->klass = -1;
res->gnode = r_graph_add_node (g->graph, res);
sdb_num_set (g->nodes, title, (ut64) (size_t) res, 0);
sdb_num_set (g->nodes, res->title, (ut64) (size_t) res, 0);
if (res->title) {
char *s, *estr, *b;
size_t len;
Expand All @@ -3345,15 +3346,17 @@ R_API RANode *r_agraph_add_node(const RAGraph *g, const char *title, const char
}

R_API bool r_agraph_del_node(const RAGraph *g, const char *title) {
RANode *an, *res = r_agraph_get_node (g, title);
char *title_trunc = r_str_trunc_ellipsis (title, 255);
RANode *an, *res = r_agraph_get_node (g, title_trunc);
free (title_trunc);
const RList *innodes;
RGraphNode *gn;
RListIter *it;

if (!res) {
return false;
}
sdb_set (g->nodes, title, NULL, 0);
sdb_set (g->nodes, res->title, NULL, 0);
sdb_array_remove (g->db, "agraph.nodes", res->title, 0);
sdb_set (g->db, sdb_fmt ("agraph.nodes.%s", res->title), NULL, 0);
sdb_set (g->db, sdb_fmt ("agraph.nodes.%s.body", res->title), 0, 0);
Expand Down Expand Up @@ -3426,7 +3429,10 @@ R_API RANode *r_agraph_get_first_node(const RAGraph *g) {
}

R_API RANode *r_agraph_get_node(const RAGraph *g, const char *title) {
return (RANode *) (size_t) sdb_num_get (g->nodes, title, NULL);
char *title_trunc = r_str_trunc_ellipsis (title, 255);
RANode *node = (RANode *) (size_t) sdb_num_get (g->nodes, title_trunc, NULL);
free (title_trunc);
return node;
}

R_API void r_agraph_add_edge(const RAGraph *g, RANode *a, RANode *b) {
Expand Down
1 change: 1 addition & 0 deletions libr/include/r_util/r_str.h
Expand Up @@ -67,6 +67,7 @@ R_API bool r_str_is_printable_incl_newlines(const char *str);
R_API char *r_str_appendlen(char *ptr, const char *string, int slen);
R_API char *r_str_newf(const char *fmt, ...);
R_API char *r_str_newlen(const char *str, int len);
R_API char *r_str_trunc_ellipsis(const char *str, int len);
R_API const char *r_str_bool(int b);
R_API const char *r_str_ansi_chrn(const char *str, int n);
R_API int r_str_ansi_len(const char *str);
Expand Down
11 changes: 11 additions & 0 deletions libr/util/str.c
Expand Up @@ -652,6 +652,17 @@ R_API char *r_str_newlen(const char *str, int len) {
return buf;
}

R_API char *r_str_trunc_ellipsis(const char *str, int len) {
char *buf;
if (strlen (str) < len) {
buf = strdup (str);
} else {
buf = r_str_newlen (str, len);
strcpy (buf + len - 4, "...");
}
return buf;
}

// Returns a new heap-allocated string that matches the format-string
// specification.
R_API char *r_str_newf(const char *fmt, ...) {
Expand Down

0 comments on commit fc82e86

Please sign in to comment.