Skip to content

Commit

Permalink
add sarg_iter
Browse files Browse the repository at this point in the history
  • Loading branch information
gitgjogh committed Jan 7, 2016
1 parent 2c5b5ca commit b7b84e4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
49 changes: 49 additions & 0 deletions libsim/sim_opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,55 @@ int ios_feof(ios_t *p, int ich)
return feof((FILE*)p[ich].fp);
}

sarg_iter_t sarg_iter_init(int argc, char **argv, int start)
{
sarg_iter_t iter = { argv, argc, start };
return iter;
}

char* sarg_get_ith(sarg_iter_t *iter, int i)
{
if (iter->argv && i >= 0 && i < iter->argc) {
return iter->argv[i];
}
return 0;
}

char* sarg_iter_1st (sarg_iter_t *iter)
{
return sarg_get_ith(iter, iter->idx = iter->start);
}

char* sarg_iter_next(sarg_iter_t *iter)
{
return sarg_get_ith(iter, ++ iter->idx);
}

char* sarg_iter_last(sarg_iter_t *iter)
{
return sarg_get_ith(iter, iter->idx = iter->argc - 1);
}

char* sarg_iter_prev(sarg_iter_t *iter)
{
return sarg_get_ith(iter, -- iter->idx);
}

char* sarg_iter_curr(sarg_iter_t *iter)
{
return sarg_get_ith(iter, iter->idx);
}

char* sarg_peek_next(sarg_iter_t *iter)
{
return sarg_get_ith(iter, 1 + iter->idx);
}

char* sarg_peek_ith (sarg_iter_t *iter, int ith)
{
return sarg_get_ith(iter, ith + iter->idx);
}

char *get_argv(int argc, char *argv[], int i, const char *name)
{
int s = (argv && i<argc) ? argv[i][0] : 0;
Expand Down
18 changes: 18 additions & 0 deletions libsim/sim_opt.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ int ios_close(ios_t *p, int nch);
int ios_feof(ios_t *p, int ich);


typedef struct sarg_iter {
char** argv;
int argc;
int start;
int idx;
} sarg_iter_t;

sarg_iter_t sarg_iter_init(int argc, char **argv, int start);
char* sarg_get_ith (sarg_iter_t *iter, int ith); /** argv[ith] */
char* sarg_iter_1st (sarg_iter_t *iter);
char* sarg_iter_next(sarg_iter_t *iter);
char* sarg_iter_last(sarg_iter_t *iter);
char* sarg_iter_prev(sarg_iter_t *iter);
char* sarg_iter_curr(sarg_iter_t *iter); /** argv[iter->idx] */
char* sarg_peek_next(sarg_iter_t *iter); /** argv[1 + iter->idx] */
char* sarg_peek_ith (sarg_iter_t *iter, int ith); /** argv[ith + iter->idx] */


#define GET_ARGV(idx, name) get_argv(argc, argv, idx, name);
char* get_argv(int argc, char *argv[], int i, const char *name);

Expand Down

0 comments on commit b7b84e4

Please sign in to comment.