MCPcopy Create free account
hub / github.com/NetHack/NetHack / add_sound_mapping

Function add_sound_mapping

src/sounds.c:1555–1626  ·  view source on GitHub ↗

adds a sound file mapping, returns 0 on failure, 1 on success */

Source from the content-addressed store, hash-verified

1553
1554/* adds a sound file mapping, returns 0 on failure, 1 on success */
1555int
1556add_sound_mapping(const char *mapping)
1557{
1558 char text[256];
1559 char filename[256];
1560 char filespec[256];
1561 char msgtyp[11];
1562 int volume, idx = -1;
1563
1564 msgtyp[0] = '\0';
1565 filename[sizeof filename - 1] = '\0';
1566 filespec[sizeof filespec - 1] = '\0';
1567 text[sizeof text - 1] = '\0';
1568 if (sscanf(mapping, "MESG \"%255[^\"]\"%*[\t ]\"%255[^\"]\" %d %d",
1569 text, filename, &volume, &idx) == 4
1570 || sscanf(mapping,
1571 "MESG %10[^\"] \"%255[^\"]\"%*[\t ]\"%255[^\"]\" %d %d",
1572 msgtyp, text, filename, &volume, &idx) == 5
1573 || sscanf(mapping,
1574 "MESG %10[^\"] \"%255[^\"]\"%*[\t ]\"%255[^\"]\" %d",
1575 msgtyp, text, filename, &volume) == 4
1576 || sscanf(mapping, "MESG \"%255[^\"]\"%*[\t ]\"%255[^\"]\" %d",
1577 text, filename, &volume) == 3) {
1578 audio_mapping *new_map;
1579
1580 if (!sounddir)
1581 sounddir = dupstr(".");
1582 if (strlen(sounddir) + 1 + strlen(filename) >= sizeof filespec) {
1583 raw_print("sound file name too long");
1584 return 0;
1585 }
1586 Snprintf(filespec, sizeof filespec, "%s/%s", sounddir, filename);
1587
1588 if (idx >= 0 || can_read_file(filespec)) {
1589 new_map = (audio_mapping *) alloc(sizeof *new_map);
1590 new_map->regex = regex_init();
1591 new_map->filename = dupstr(filespec);
1592 new_map->volume = volume;
1593 new_map->idx = idx;
1594 new_map->next = soundmap;
1595
1596 if (!regex_compile(text, new_map->regex)) {
1597 char errbuf[BUFSZ];
1598 char *re_error_desc
1599 = regex_error_desc(new_map->regex, errbuf);
1600
1601 regex_free(new_map->regex);
1602 free((genericptr_t) new_map->filename);
1603 free((genericptr_t) new_map);
1604 raw_print(re_error_desc);
1605 return 0;
1606 } else {
1607 if (*msgtyp) {
1608 char tmpbuf[BUFSZ];
1609
1610 Sprintf(tmpbuf, "%.10s \"%.230s\"", msgtyp, text);
1611 (void) msgtype_parse_add(tmpbuf);
1612 }

Callers 1

cnf_line_SOUNDFunction · 0.85

Calls 9

dupstrFunction · 0.85
raw_printFunction · 0.85
can_read_fileFunction · 0.85
msgtype_parse_addFunction · 0.85
allocFunction · 0.70
regex_initFunction · 0.50
regex_compileFunction · 0.50
regex_error_descFunction · 0.50
regex_freeFunction · 0.50

Tested by

no test coverage detected