📄 q.c
字号:
#include <sys/types.h>#include <sys/time.h>#include <assert.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include "queue.h"#include "shqueue.h"typedef enum { FORWARD_WALK_FAILED = 1, FOREACH_WALK_FAILED, LIST_END_NOT_MARKED_FAILURE, PREV_WALK_FAILED, REVERSE_FOREACH_WALK_FAILED, EXPECTED_HEAD_FAILED} FAILURE_REASON;const char *failure_reason_names[] = { "", "walking the list using the _NEXT forward failed", "walking the list using the _FOREACH macro failed", "what was expected to be the last element wasn't marked as such", "walking the list using the _PREV macro failed", "walking the list using the _REVERSE_FOREACH macro failed", "expected to be at the head of the list"};SH_LIST_HEAD(sh_lq);struct sh_le { char content; SH_LIST_ENTRY sh_les;};/* create a string from the content of a list queue */char *sh_l_as_string(l) struct sh_lq *l;{ static char buf[1024]; struct sh_le *ele = SH_LIST_FIRST(l, sh_le); int i = 1; buf[0] = '"'; while (ele != NULL) { buf[i] = ele->content; ele = SH_LIST_NEXT(ele, sh_les, sh_le); if (ele != NULL) buf[++i] = ' '; i++; } buf[i++] = '"'; buf[i] = '\0'; return buf;}/* init a list queue */struct sh_lq *sh_l_init(items) const char *items;{ const char *c = items; struct sh_le *ele = NULL, *last_ele = (struct sh_le*)-1; struct sh_lq *l = calloc(1, sizeof(struct sh_lq)); SH_LIST_INIT(l); while (*c != '\0') { if (c[0] != ' ') { last_ele = ele; ele = calloc(1, sizeof(struct sh_le)); ele->content = c[0]; if (SH_LIST_EMPTY(l)) SH_LIST_INSERT_HEAD(l, ele, sh_les, sh_le); else SH_LIST_INSERT_AFTER( last_ele, ele, sh_les, sh_le); } c++; } return (l);}struct sh_lq *sh_l_remove_head(l) struct sh_lq *l;{ struct sh_le *ele = SH_LIST_FIRST(l, sh_le); SH_LIST_REMOVE_HEAD(l, sh_les, sh_le); if (ele != NULL) free(ele); return (l);}struct sh_lq *sh_l_remove_tail(l) struct sh_lq *l;{ struct sh_le *ele = SH_LIST_FIRST(l, sh_le); if (SH_LIST_EMPTY(l)) return (l); while (SH_LIST_NEXT(ele, sh_les, sh_le) != NULL) ele = SH_LIST_NEXT(ele, sh_les, sh_le); if (ele) { SH_LIST_REMOVE(ele, sh_les, sh_le); free(ele); } return (l);}struct sh_lq *sh_l_remove_item(l, item) struct sh_lq *l; const char *item;{ struct sh_le *ele = SH_LIST_FIRST(l, sh_le); while (ele != NULL) { if (ele->content == item[0]) break; ele = SH_LIST_NEXT(ele, sh_les, sh_le); } if (ele) SH_LIST_REMOVE(ele, sh_les, sh_le); return (l);}struct sh_lq *sh_l_insert_head(l, item) struct sh_lq *l; const char *item;{ struct sh_le *ele = calloc(1, sizeof(struct sh_le)); ele->content = item[0]; SH_LIST_INSERT_HEAD(l, ele, sh_les, sh_le); return (l);}struct sh_lq *sh_l_insert_tail(l, item) struct sh_lq *l; const char *item;{ struct sh_le *ele = NULL; struct sh_le *last_ele = SH_LIST_FIRST(l, sh_le); if (last_ele != NULL) while (SH_LIST_NEXT(last_ele, sh_les, sh_le) != NULL) last_ele = SH_LIST_NEXT(last_ele, sh_les, sh_le); if (last_ele == NULL) { ele = calloc(1, sizeof(struct sh_le)); ele->content = item[0]; SH_LIST_INSERT_HEAD(l, ele, sh_les, sh_le); } else { ele = calloc(1, sizeof(struct sh_le)); ele->content = item[0]; SH_LIST_INSERT_AFTER(last_ele, ele, sh_les, sh_le); } return (l);}struct sh_lq *sh_l_insert_before(l, item, before_item) struct sh_lq *l; const char *item; const char *before_item;{ struct sh_le *ele = NULL; struct sh_le *before_ele = SH_LIST_FIRST(l, sh_le); while (before_ele != NULL) { if (before_ele->content == before_item[0]) break; before_ele = SH_LIST_NEXT(before_ele, sh_les, sh_le); } if (before_ele != NULL) { ele = calloc(1, sizeof(struct sh_le)); ele->content = item[0]; SH_LIST_INSERT_BEFORE(l, before_ele, ele, sh_les, sh_le); } return (l);}struct sh_lq *sh_l_insert_after(l, item, after_item) struct sh_lq *l; const char *item; const char *after_item;{ struct sh_le *ele = NULL; struct sh_le *after_ele = SH_LIST_FIRST(l, sh_le); while (after_ele != NULL) { if (after_ele->content == after_item[0]) break; after_ele = SH_LIST_NEXT(after_ele, sh_les, sh_le); } if (after_ele != NULL) { ele = calloc(1, sizeof(struct sh_le)); ele->content = item[0]; SH_LIST_INSERT_AFTER(after_ele, ele, sh_les, sh_le); } return (l);}voidsh_l_discard(l) struct sh_lq *l;{ struct sh_le *ele = NULL; while ((ele = SH_LIST_FIRST(l, sh_le)) != NULL) { SH_LIST_REMOVE(ele, sh_les, sh_le); free(ele); } free(l);}intsh_l_verify(l, items) struct sh_lq *l; const char *items;{ const char *c = items; struct sh_le *ele = NULL, *lele = NULL; int i = 0, nele = 0; while (*c != '\0') { if (c[0] != ' ') nele++; c++; } /* use the FOREACH macro to walk the list */ c = items; i = 0; SH_LIST_FOREACH(ele, l, sh_les, sh_le) { if (ele->content != c[0]) return (FOREACH_WALK_FAILED); i++; c +=2; } if (i != nele) return (FOREACH_WALK_FAILED); i = 0; if (items[0] != '\0') { /* walk the list forward */ c = items; ele = SH_LIST_FIRST(l, sh_le); while (*c != '\0') { lele = ele; if (c[0] != ' ') { if (ele->content != c[0]) return (FORWARD_WALK_FAILED); i++; ele = SH_LIST_NEXT(ele, sh_les, sh_le); } c++; } ele = lele; if (i != nele) return (FOREACH_WALK_FAILED); /* ele should be the last element in the list... */ /* ... so sle_next should be -1 */ if (ele->sh_les.sle_next != -1) return (LIST_END_NOT_MARKED_FAILURE); /* and NEXT needs to be NULL */ if (SH_LIST_NEXT(ele, sh_les, sh_le) != NULL) return (LIST_END_NOT_MARKED_FAILURE); /* * walk the list backwards using PREV macro, first move c * back a bit */ c--; i = 0; while (c >= items) { if (c[0] != ' ') { lele = ele; if (ele->content != c[0]) return (PREV_WALK_FAILED); ele = SH_LIST_PREV(ele, sh_les, sh_le); i++; } c--; } ele = lele; if (i != nele) return (PREV_WALK_FAILED); if (ele != SH_LIST_FIRST(l, sh_le)) return (EXPECTED_HEAD_FAILED); } return (0);}SH_TAILQ_HEAD(sh_tq);struct sh_te { char content; SH_TAILQ_ENTRY sh_tes;};/* create a string from the content of a list queue */char *sh_t_as_string(l) struct sh_tq *l;{ static char buf[1024]; struct sh_te *ele = SH_TAILQ_FIRST(l, sh_te); int i = 1; buf[0] = '"'; while (ele != NULL) { buf[i] = ele->content; ele = SH_TAILQ_NEXT(ele, sh_tes, sh_te); if (ele != NULL) buf[++i] = ' '; i++; } buf[i++] = '"'; buf[i] = '\0'; return (buf);}/* init a tail queue */struct sh_tq *sh_t_init(items) const char *items;{ const char *c = items; struct sh_te *ele = NULL, *last_ele = (struct sh_te*)-1; struct sh_tq *l = calloc(1, sizeof(struct sh_tq)); SH_TAILQ_INIT(l); while (*c != '\0') { if (c[0] != ' ') { ele = calloc(1, sizeof(struct sh_te)); ele->content = c[0]; if (SH_TAILQ_EMPTY(l)) SH_TAILQ_INSERT_HEAD(l, ele, sh_tes, sh_te); else SH_TAILQ_INSERT_AFTER( l, last_ele, ele, sh_tes, sh_te); last_ele = ele; } c++; } return (l);}struct sh_tq *sh_t_remove_head(l) struct sh_tq *l;{ struct sh_te *ele = SH_TAILQ_FIRST(l, sh_te); if (ele != NULL) SH_TAILQ_REMOVE(l, ele, sh_tes, sh_te); free(ele); return (l);}struct sh_tq *sh_t_remove_tail(l) struct sh_tq *l;{ struct sh_te *ele = SH_TAILQ_FIRST(l, sh_te); if (SH_TAILQ_EMPTY(l)) return (l); while (SH_TAILQ_NEXT(ele, sh_tes, sh_te) != NULL) ele = SH_TAILQ_NEXT(ele, sh_tes, sh_te); if (ele != NULL) { SH_TAILQ_REMOVE(l, ele, sh_tes, sh_te); free(ele); } return (l);}struct sh_tq *sh_t_remove_item(l, item) struct sh_tq *l; const char *item;{ struct sh_te *ele = SH_TAILQ_FIRST(l, sh_te); while (ele != NULL) { if (ele->content == item[0]) break; ele = SH_TAILQ_NEXT(ele, sh_tes, sh_te); } if (ele != NULL) SH_TAILQ_REMOVE(l, ele, sh_tes, sh_te);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -