arduino-audio-tools
HttpLineReader.h
1 #pragma once
2 
3 #include "AudioLogger.h"
4 
5 namespace audio_tools {
6 
15  public:
16  HttpLineReader() {}
17  // reads up the the next CR LF - but never more then the indicated len.
18  // returns the number of characters read including crlf
19  virtual int readlnInternal(Stream& client, uint8_t* str, int len,
20  bool incl_nl = true) {
21  int result = 0;
22  LOGD("HttpLineReader %s", "readlnInternal");
23  // wait for first character
24  for (int w = 0; w < 20 && client.available() == 0; w++) {
25  delay(100);
26  }
27  // if we do not have any data we stop
28  if (client.available() == 0) {
29  LOGW("HttpLineReader %s", "readlnInternal->no Data");
30  str[0] = 0;
31  return 0;
32  }
33 
34  // process characters until we find a new line
35  bool is_buffer_owerflow = false;
36  int j = 0;
37  while (true) {
38  int c = client.read();
39  if (c == -1) {
40  break;
41  }
42 
43  if (j < len) {
44  result++;
45  } else {
46  is_buffer_owerflow = true;
47  }
48 
49  if (c == '\n') {
50  if (incl_nl) {
51  str[j] = c;
52  break;
53  } else {
54  // remove cr lf
55  if (j >= 1) {
56  if (str[j - 1] == '\r') {
57  // remove cr
58  str[j - 1] = 0;
59  break;
60  } else {
61  // remove nl
62  str[j] = 0;
63  break;
64  }
65  }
66  }
67  }
68  if (!is_buffer_owerflow) {
69  str[j] = c;
70  }
71  j++;
72  }
73  str[result - 1] = 0;
74  if (is_buffer_owerflow) {
75  LOGE("Line cut off: %s", str);
76  }
77 
78  return result;
79  }
80 };
81 
82 } // namespace audio_tools
We read a single line. A terminating 0 is added to the string to make it compliant for c string funct...
Definition: HttpLineReader.h:14
Definition: NoArduino.h:125
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823