FreeCalypso > hg > freecalypso-tools
comparison ringtools/fc-ringlist-comp.c @ 874:f442156d31ad
ringtools: fc-ringlist-comp program written, compiles
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Tue, 29 Mar 2022 19:38:28 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
873:2b5f4736079c | 874:f442156d31ad |
---|---|
1 /* | |
2 * This program compiles a list of ringing tone or message alert tone | |
3 * melodies from ASCII source into the binary format (.mls) that will | |
4 * be uploaded into FreeCalypso device FFS and read by the UI layer | |
5 * of FC phone firmwares. | |
6 */ | |
7 | |
8 #include <stdio.h> | |
9 #include <stdlib.h> | |
10 #include <ctype.h> | |
11 #include <string.h> | |
12 #include <strings.h> | |
13 | |
14 #define MAX_FFS_PATHNAME 19 | |
15 #define MAX_FFS_PREFIX 5 | |
16 #define MAX_NAME_FIELD 59 | |
17 | |
18 struct bin_record { | |
19 char ffs_pathname[MAX_FFS_PATHNAME+1]; | |
20 char ui_name[MAX_NAME_FIELD+1]; | |
21 }; | |
22 | |
23 char *infname, *outfname; | |
24 FILE *inf, *outf; | |
25 char ffs_prefix[MAX_FFS_PREFIX+2]; | |
26 unsigned ffs_prefix_len; | |
27 char linebuf[256]; | |
28 int lineno; | |
29 | |
30 set_ffs_prefix(prefix_arg) | |
31 char *prefix_arg; | |
32 { | |
33 char *cp; | |
34 | |
35 if (prefix_arg[0] != '/') { | |
36 fprintf(stderr, | |
37 "error: given FFS prefix does not begin with \'/\'\n"); | |
38 exit(1); | |
39 } | |
40 ffs_prefix_len = strlen(prefix_arg); | |
41 if (ffs_prefix_len > MAX_FFS_PREFIX) { | |
42 fprintf(stderr, "error: given FFS prefix is too long\n"); | |
43 exit(1); | |
44 } | |
45 strcpy(ffs_prefix, prefix_arg); | |
46 cp = ffs_prefix + ffs_prefix_len; | |
47 if (cp[-1] != '/') { | |
48 cp[0] = '/'; | |
49 cp[1] = '\0'; | |
50 ffs_prefix_len++; | |
51 } | |
52 } | |
53 | |
54 enforce_and_strip_newline() | |
55 { | |
56 char *cp; | |
57 | |
58 cp = index(linebuf, '\n'); | |
59 if (!cp) { | |
60 fprintf(stderr, "%s line %d: too long or missing newline\n", | |
61 infname, lineno); | |
62 exit(1); | |
63 } | |
64 *cp = '\0'; | |
65 } | |
66 | |
67 emit_record(fname, uname) | |
68 char *fname, *uname; | |
69 { | |
70 struct bin_record rec; | |
71 | |
72 strcpy(rec.ffs_pathname, ffs_prefix); | |
73 strncpy(rec.ffs_pathname + ffs_prefix_len, fname, | |
74 MAX_FFS_PATHNAME + 1 - ffs_prefix_len); | |
75 strncpy(rec.ui_name, uname, MAX_NAME_FIELD + 1); | |
76 fwrite(&rec, sizeof rec, 1, outf); | |
77 } | |
78 | |
79 process_line() | |
80 { | |
81 char *cp, *fname, *uname; | |
82 | |
83 cp = linebuf; | |
84 while (isspace(*cp)) | |
85 cp++; | |
86 if (*cp == '\0' || *cp == '#') | |
87 return; | |
88 fname = cp; | |
89 while (*cp && !isspace(*cp)) | |
90 cp++; | |
91 if (!*cp) { | |
92 inv: fprintf(stderr, "%s line %d: invalid syntax\n", | |
93 infname, lineno); | |
94 exit(1); | |
95 } | |
96 *cp++ = '\0'; | |
97 while (isspace(*cp)) | |
98 cp++; | |
99 if (*cp == '\0' || *cp == '#') | |
100 goto inv; | |
101 uname = cp; | |
102 if (strlen(fname) + ffs_prefix_len > MAX_FFS_PATHNAME) { | |
103 fprintf(stderr, "%s line %d: melody filename exceeds limit\n", | |
104 infname, lineno); | |
105 exit(1); | |
106 } | |
107 if (strlen(uname) > MAX_NAME_FIELD) { | |
108 fprintf(stderr, "%s line %d: melody UI name exceeds limit\n", | |
109 infname, lineno); | |
110 exit(1); | |
111 } | |
112 emit_record(fname, uname); | |
113 } | |
114 | |
115 main(argc, argv) | |
116 char **argv; | |
117 { | |
118 if (argc != 4) { | |
119 fprintf(stderr, "usage: %s src-file bin-file ffs-prefix\n", | |
120 argv[0]); | |
121 exit(1); | |
122 } | |
123 if (strcmp(argv[1], "-")) { | |
124 infname = argv[1]; | |
125 inf = fopen(infname, "r"); | |
126 if (!inf) { | |
127 perror(infname); | |
128 exit(1); | |
129 } | |
130 } else { | |
131 infname = "stdin"; | |
132 inf = stdin; | |
133 } | |
134 set_ffs_prefix(argv[3]); | |
135 outfname = argv[2]; | |
136 outf = fopen(outfname, "w"); | |
137 if (!outf) { | |
138 perror(outfname); | |
139 exit(1); | |
140 } | |
141 | |
142 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) { | |
143 enforce_and_strip_newline(); | |
144 process_line(); | |
145 } | |
146 exit(0); | |
147 } |