Skip to content

Commit 3058fb0

Browse files
committed
Add xuseq_to_str, useq_to_str functions and use in random-seq program
1 parent 2f0d861 commit 3058fb0

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

main-random-seq.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
int main(int argc, char **argv)
88
{
99
uint i, n, *pairs, *useq;
10-
char *vienna;
10+
char *vienna, *str;
1111

1212
if (argc != 2 && argc != 3) {
1313
printf("ERROR: wrong number of arguments\n"
@@ -18,6 +18,7 @@ int main(int argc, char **argv)
1818
n = strlen(vienna);
1919
pairs = xmalloc(n * sizeof(*pairs));
2020
useq = xmalloc(n * sizeof(*useq));
21+
str = xmalloc((n + 1) * sizeof(*str));
2122

2223
if (argc == 3) {
2324
random_seed(atoi(argv[2]));
@@ -27,11 +28,10 @@ int main(int argc, char **argv)
2728

2829
xvienna_to_pairs(n, vienna, pairs);
2930
random_useq(n, pairs, useq);
30-
/* TODO: move this to useq_to_str function */
31-
for (i = 0; i < n; i++)
32-
printf("%c", NA_BASE_NAMES[useq[i]]);
33-
printf("\n");
31+
xuseq_to_str(n, useq, str);
32+
printf("%s\n", str);
3433

34+
free(str);
3535
free(pairs);
3636
free(useq);
3737
return EXIT_SUCCESS;

na.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,37 @@ xstr_to_pseq(uint n, uint ndim, const char *str, double **p)
244244
}
245245
}
246246

247+
/* translate from uint sequence representation to string, exiting on
248+
errors */
249+
void
250+
xuseq_to_str(uint n, const uint *useq, char *str)
251+
{
252+
bool verbose = true;
253+
int retcode = useq_to_str(n, useq, verbose, str);
254+
if (retcode != EXIT_SUCCESS) {
255+
exit(EXIT_FAILURE);
256+
}
257+
}
258+
259+
/* translate from uint sequence representation to string, return
260+
EXIT_SUCCESS on success */
261+
int
262+
useq_to_str(uint n, const uint *useq, bool verbose, char *str)
263+
{
264+
uint i;
265+
str[n] = '\0';
266+
for (i = 0; i < n; i++) {
267+
if (useq[i] >= NA_NBASES) {
268+
if (verbose) {
269+
printf("ERROR: illegal base at position %u, base id = %u\n", i, useq[i]);
270+
}
271+
return EXIT_FAILURE;
272+
}
273+
str[i] = NA_BASE_NAMES[useq[i]];
274+
}
275+
return EXIT_SUCCESS;
276+
}
277+
247278
void
248279
nn_multiloop_xalloc(struct nn_multiloop *ml, uint nstems, uint ndangle5,
249280
uint ndangle3)

na.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ char * xpairs_to_vienna(uint n, const uint *pairs, char *vienna);
6868
void xstr_to_useq(uint n, const char *str, uint *useq);
6969
void xstr_to_pseq(uint n, uint ndim, const char *str, double **p);
7070
void pseq_to_str(double **p, uint n, uint ndim, char *str);
71+
void xuseq_to_str(uint n, const uint *useq, char *str);
72+
int useq_to_str(uint n, const uint *useq, bool verbose, char *str);
7173

7274
struct nn_inter * nn_inter_xnew(uint n);
7375
void nn_inter_delete(struct nn_inter *inter);

0 commit comments

Comments
 (0)