Skip to content

Commit 560baed

Browse files
committed
another unique pointer
1 parent 9091213 commit 560baed

File tree

2 files changed

+39
-47
lines changed

2 files changed

+39
-47
lines changed

src/Audio.cpp

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -615,52 +615,52 @@ bool Audio::httpPrint(const char* host) {
615615
return false;
616616
}
617617

618-
char* h_host = NULL; // pointer of l_host without http:// or https://
618+
ps_ptr<char> h_host = nullptr; // copy of l_host without http:// or https://
619619

620620
if(startsWith(host, "https")) m_f_ssl = true;
621621
else m_f_ssl = false;
622622

623-
if(m_f_ssl) h_host = strdup(host + 8);
624-
else h_host = strdup(host + 7);
623+
if(m_f_ssl) h_host = audio_strdup(host + 8);
624+
else h_host = audio_strdup(host + 7);
625625

626626
int16_t pos_slash; // position of "/" in hostname
627627
int16_t pos_colon; // position of ":" in hostname
628628
int16_t pos_ampersand; // position of "&" in hostname
629629
uint16_t port = 80; // port number
630630

631631
// In the URL there may be an extension, like noisefm.ru:8000/play.m3u&t=.m3u
632-
pos_slash = indexOf(h_host, "/", 0);
633-
pos_colon = indexOf(h_host, ":", 0);
632+
pos_slash = indexOf(h_host.get(), "/", 0);
633+
pos_colon = indexOf(h_host.get(), ":", 0);
634634
if(isalpha(h_host[pos_colon + 1])) pos_colon = -1; // no portnumber follows
635-
pos_ampersand = indexOf(h_host, "&", 0);
635+
pos_ampersand = indexOf(h_host.get(), "&", 0);
636636

637-
char* hostwoext = NULL; // "skonto.ls.lv:8002" in "skonto.ls.lv:8002/mp3"
638-
ps_ptr<char> extension; // "/mp3" in "skonto.ls.lv:8002/mp3"
637+
ps_ptr<char> hostwoext = nullptr; // "skonto.ls.lv:8002" in "skonto.ls.lv:8002/mp3"
638+
ps_ptr<char> extension = nullptr; // "/mp3" in "skonto.ls.lv:8002/mp3"
639639

640640
if(pos_slash > 1) {
641-
hostwoext = (char*)x_ps_malloc(pos_slash + 1);
642-
memcpy(hostwoext, h_host, pos_slash);
641+
hostwoext = audio_malloc<char>(pos_slash + 1);
642+
memcpy(hostwoext.get(), h_host.get(), pos_slash);
643643
hostwoext[pos_slash] = '\0';
644-
extension = urlencode(h_host + pos_slash, true);
644+
extension = urlencode(h_host.get() + pos_slash, true);
645645
}
646646
else { // url has no extension
647-
hostwoext = strdup(h_host);
647+
hostwoext = audio_strdup(h_host.get());
648648
extension = audio_strdup("/");
649649
}
650650

651651
if((pos_colon >= 0) && ((pos_ampersand == -1) || (pos_ampersand > pos_colon))) {
652-
port = atoi(h_host + pos_colon + 1); // Get portnumber as integer
652+
port = atoi(h_host.get() + pos_colon + 1); // Get portnumber as integer
653653
hostwoext[pos_colon] = '\0'; // Host without portnumber
654654
}
655655

656-
char rqh[strlen(h_host) + 330]; // http request header
656+
char rqh[strlen(h_host.get()) + 330]; // http request header
657657
rqh[0] = '\0';
658658

659659
strcat(rqh, "GET ");
660660
strcat(rqh, extension.get());
661661
strcat(rqh, " HTTP/1.1\r\n");
662662
strcat(rqh, "Host: ");
663-
strcat(rqh, hostwoext);
663+
strcat(rqh, hostwoext.get());
664664
strcat(rqh, "\r\n");
665665
strcat(rqh, "Accept: */*\r\n");
666666
strcat(rqh, "User-Agent: VLC/3.0.21 LibVLC/3.0.21 AppleWebKit/537.36 (KHTML, like Gecko)\r\n");
@@ -673,7 +673,7 @@ bool Audio::httpPrint(const char* host) {
673673
if(m_f_ssl) { _client = static_cast<WiFiClient*>(&clientsecure); if(m_f_ssl && port == 80) port = 443;}
674674
else { _client = static_cast<WiFiClient*>(&client); }
675675
log_info("The host has disconnected, reconnecting");
676-
if(!_client->connect(hostwoext, port)) {
676+
if(!_client->connect(hostwoext.get(), port)) {
677677
log_e("connection lost");
678678
stopSong();
679679
return false;
@@ -699,9 +699,6 @@ bool Audio::httpPrint(const char* host) {
699699
m_contentlength = 0;
700700
m_f_chunked = false;
701701

702-
x_ps_free(&hostwoext);
703-
x_ps_free(&h_host);
704-
705702
return true;
706703
}
707704
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -713,45 +710,45 @@ bool Audio::httpRange(const char* host, uint32_t range){
713710
stopSong();
714711
return false;
715712
}
716-
char* h_host = NULL; // pointer of host without http:// or https://
713+
ps_ptr<char> h_host = nullptr; // copy of host without http:// or https://
717714

718715
if(startsWith(host, "https")) m_f_ssl = true;
719716
else m_f_ssl = false;
720717

721-
if(m_f_ssl) h_host = strdup(host + 8);
722-
else h_host = strdup(host + 7);
718+
if(m_f_ssl) h_host = audio_strdup(host + 8);
719+
else h_host = audio_strdup(host + 7);
723720

724721
int16_t pos_slash; // position of "/" in hostname
725722
int16_t pos_colon; // position of ":" in hostname
726723
int16_t pos_ampersand; // position of "&" in hostname
727724
uint16_t port = 80; // port number
728725

729726
// In the URL there may be an extension, like noisefm.ru:8000/play.m3u&t=.m3u
730-
pos_slash = indexOf(h_host, "/", 0);
731-
pos_colon = indexOf(h_host, ":", 0);
727+
pos_slash = indexOf(h_host.get(), "/", 0);
728+
pos_colon = indexOf(h_host.get(), ":", 0);
732729
if(isalpha(h_host[pos_colon + 1])) pos_colon = -1; // no portnumber follows
733-
pos_ampersand = indexOf(h_host, "&", 0);
730+
pos_ampersand = indexOf(h_host.get(), "&", 0);
734731

735-
char* hostwoext = NULL; // "skonto.ls.lv:8002" in "skonto.ls.lv:8002/mp3"
736-
ps_ptr<char> extension; // "/mp3" in "skonto.ls.lv:8002/mp3"
732+
ps_ptr<char> hostwoext = nullptr; // "skonto.ls.lv:8002" in "skonto.ls.lv:8002/mp3"
733+
ps_ptr<char> extension = nullptr; // "/mp3" in "skonto.ls.lv:8002/mp3"
737734

738735
if(pos_slash > 1) {
739-
hostwoext = (char*)x_ps_malloc(pos_slash + 1);
740-
memcpy(hostwoext, h_host, pos_slash);
736+
hostwoext = audio_malloc<char>(pos_slash + 1);
737+
memcpy(hostwoext.get(), h_host.get(), pos_slash);
741738
hostwoext[pos_slash] = '\0';
742-
extension = urlencode(h_host + pos_slash, true);
739+
extension = urlencode(h_host.get() + pos_slash, true);
743740
}
744741
else { // url has no extension
745-
hostwoext = strdup(h_host);
742+
hostwoext = audio_strdup(h_host.get());
746743
extension = audio_strdup("/");
747744
}
748745

749746
if((pos_colon >= 0) && ((pos_ampersand == -1) || (pos_ampersand > pos_colon))) {
750-
port = atoi(h_host + pos_colon + 1); // Get portnumber as integer
747+
port = atoi(h_host.get() + pos_colon + 1); // Get portnumber as integer
751748
hostwoext[pos_colon] = '\0'; // Host without portnumber
752749
}
753750

754-
char rqh[strlen(h_host) + strlen(host) + 300]; // http request header
751+
char rqh[strlen(h_host.get()) + strlen(host) + 300]; // http request header
755752
rqh[0] = '\0';
756753
char ch_range[12];
757754
ltoa(range, ch_range, 10);
@@ -760,7 +757,7 @@ bool Audio::httpRange(const char* host, uint32_t range){
760757
strcat(rqh, extension.get());
761758
strcat(rqh, " HTTP/1.1\r\n");
762759
strcat(rqh, "Host: ");
763-
strcat(rqh, hostwoext);
760+
strcat(rqh, hostwoext.get());
764761
strcat(rqh, "\r\n");
765762
strcat(rqh, "Range: bytes=");
766763
strcat(rqh, (const char*)ch_range);
@@ -777,7 +774,7 @@ log_e("%s", rqh);
777774
if(m_f_ssl) { _client = static_cast<WiFiClient*>(&clientsecure); if(m_f_ssl && port == 80) port = 443;}
778775
else { _client = static_cast<WiFiClient*>(&client); }
779776
log_info("The host has disconnected, reconnecting");
780-
if(!_client->connect(hostwoext, port)) {
777+
if(!_client->connect(hostwoext.get(), port)) {
781778
log_e("connection lost");
782779
stopSong();
783780
return false;
@@ -798,9 +795,6 @@ log_e("%s", rqh);
798795
m_contentlength = 0;
799796
m_f_chunked = false;
800797

801-
x_ps_free(&hostwoext);
802-
x_ps_free(&h_host);
803-
804798
return true;
805799
}
806800
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -845,7 +839,7 @@ bool Audio::connecttoFS(fs::FS& fs, const char* path, int32_t fileStartPos) {
845839
xSemaphoreTakeRecursive(mutex_playAudioData, 0.3 * configTICK_RATE_HZ);
846840
bool res = false;
847841
int16_t dotPos;
848-
char* audioPath = NULL;
842+
ps_ptr<char> audioPath = nullptr;
849843
m_fileStartPos = fileStartPos;
850844
uint8_t codec = CODEC_NONE;
851845

@@ -864,14 +858,14 @@ bool Audio::connecttoFS(fs::FS& fs, const char* path, int32_t fileStartPos) {
864858
if(endsWith(path, ".oga")) {codec = CODEC_OGG; m_f_ogg = true;}
865859
if(codec == CODEC_NONE) {log_info("The %s format is not supported", path + dotPos); goto exit;} // guard
866860

867-
audioPath = (char *)x_ps_calloc(strlen(path) + 2, sizeof(char));
861+
audioPath = audio_calloc<char>(strlen(path) + 2);
868862
if(!audioPath){printProcessLog(AUDIOLOG_OUT_OF_MEMORY); goto exit;};
869863
if(path[0] != '/')audioPath[0] = '/';
870-
strcat(audioPath, path);
864+
strcat(audioPath.get(), path);
871865

872-
if(!fs.exists(audioPath)) {printProcessLog(AUDIOLOG_FILE_NOT_FOUND, audioPath); goto exit;}
873-
log_info("Reading file: \"%s\"", audioPath);
874-
audiofile = fs.open(audioPath);
866+
if(!fs.exists(audioPath.get())) {printProcessLog(AUDIOLOG_FILE_NOT_FOUND, audioPath.get()); goto exit;}
867+
log_info("Reading file: \"%s\"", audioPath.get());
868+
audiofile = fs.open(audioPath.get());
875869
m_dataMode = AUDIO_LOCALFILE;
876870
m_fileSize = audiofile.size();
877871

@@ -881,7 +875,6 @@ bool Audio::connecttoFS(fs::FS& fs, const char* path, int32_t fileStartPos) {
881875
else audiofile.close();
882876

883877
exit:
884-
x_ps_free(&audioPath);
885878
xSemaphoreGiveRecursive(mutex_playAudioData);
886879
return res;
887880
}
@@ -1813,7 +1806,6 @@ int Audio::read_ID3_Header(uint8_t* data, size_t len) {
18131806
if(ID3Hdr.framesize == 0) return 0;
18141807

18151808
size_t fs = ID3Hdr.framesize;
1816-
log_w("framesize %i", ID3Hdr.framesize);
18171809
if(fs >= m_ibuffSize - 1) fs = m_ibuffSize - 1;
18181810
uint16_t dataLength = fs - 1;
18191811
for(int i = 0; i < dataLength; i++) { ID3Hdr.iBuff[i] = *(data + i + 1);} // without encodingByte

src/Audio.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ class Audio : private AudioBuffer{
500500
// Request memory for an array of T
501501
template <typename T>
502502
std::unique_ptr<T[], PsramDeleter> audio_calloc(std::size_t count) {
503-
T* raw = static_cast<T*>(ps_calloc(count, sizeof(T)));
503+
T* raw = static_cast<T*>(ps_calloc(sizeof(T) * count, sizeof(char)));
504504
if (!raw) {
505505
log_e("audio_malloc_array: OOM, no space for %zu bytes", sizeof(T) * count);
506506
}

0 commit comments

Comments
 (0)