Browse Source

Bit of tidying up in linux code

tags/2021-05-28
jules 5 years ago
parent
commit
fa577fc4ef
3 changed files with 59 additions and 53 deletions
  1. +37
    -31
      modules/juce_core/native/juce_linux_Network.cpp
  2. +17
    -17
      modules/juce_core/native/juce_linux_SystemStats.cpp
  3. +5
    -5
      modules/juce_core/native/juce_linux_Threads.cpp

+ 37
- 31
modules/juce_core/native/juce_linux_Network.cpp View File

@@ -25,14 +25,15 @@ namespace juce
void MACAddress::findAllAddresses (Array<MACAddress>& result)
{
const int s = socket (AF_INET, SOCK_DGRAM, 0);
auto s = socket (AF_INET, SOCK_DGRAM, 0);
if (s != -1)
{
struct ifaddrs* addrs = nullptr;
if (getifaddrs (&addrs) != -1)
{
for (struct ifaddrs* i = addrs; i != nullptr; i = i->ifa_next)
for (auto* i = addrs; i != nullptr; i = i->ifa_next)
{
struct ifreq ifr;
strcpy (ifr.ifr_name, i->ifa_name);
@@ -69,7 +70,7 @@ bool JUCE_CALLTYPE Process::openEmailWithAttachments (const String& /* targetEma
class WebInputStream::Pimpl
{
public:
Pimpl (WebInputStream& pimplOwner, const URL& urlToCopy, const bool shouldUsePost)
Pimpl (WebInputStream& pimplOwner, const URL& urlToCopy, bool shouldUsePost)
: owner (pimplOwner), url (urlToCopy),
isPost (shouldUsePost), httpRequestCmd (shouldUsePost ? "POST" : "GET")
{}
@@ -95,19 +96,21 @@ public:
void withCustomRequestCommand (const String& customRequestCommand) { httpRequestCmd = customRequestCommand; }
void withConnectionTimeout (int timeoutInMs) { timeOutMs = timeoutInMs; }
void withNumRedirectsToFollow (int maxRedirectsToFollow) { numRedirectsToFollow = maxRedirectsToFollow; }
int getStatusCode() const { return statusCode; }
StringPairArray getRequestHeaders() const { return WebInputStream::parseHttpHeaders (headers); }
StringPairArray getResponseHeaders() const
{
StringPairArray responseHeaders;
if (! isError())
{
for (int i = 0; i < headerLines.size(); ++i)
{
const String& headersEntry = headerLines[i];
const String key (headersEntry.upToFirstOccurrenceOf (": ", false, false));
const String value (headersEntry.fromFirstOccurrenceOf (": ", false, false));
const String previousValue (responseHeaders [key]);
auto& headersEntry = headerLines[i];
auto key = headersEntry.upToFirstOccurrenceOf (": ", false, false);
auto value = headersEntry.fromFirstOccurrenceOf (": ", false, false);
auto previousValue = responseHeaders[key];
responseHeaders.set (key, previousValue.isEmpty() ? value : (previousValue + "," + value));
}
}
@@ -115,8 +118,6 @@ public:
return responseHeaders;
}
int getStatusCode() const { return statusCode; }
bool connect (WebInputStream::Listener* listener)
{
{
@@ -129,7 +130,7 @@ public:
address = url.toString (! isPost);
statusCode = createConnection (listener, numRedirectsToFollow);
return (statusCode != 0);
return statusCode != 0;
}
void cancel()
@@ -137,7 +138,6 @@ public:
const ScopedLock lock (createSocketLock);
hasBeenCancelled = true;
statusCode = -1;
finished = true;
@@ -190,7 +190,7 @@ public:
chunkLengthBuffer.writeByte (c);
}
const int64 chunkSize = chunkLengthBuffer.toString().trimStart().getHexValue64();
auto chunkSize = chunkLengthBuffer.toString().trimStart().getHexValue64();
if (chunkSize == 0)
{
@@ -216,7 +216,8 @@ public:
if (select (socketHandle + 1, &readbits, 0, 0, &tv) <= 0)
return 0; // (timeout)
const int bytesRead = jmax (0, (int) recv (socketHandle, buffer, (size_t) bytesToRead, MSG_WAITALL));
auto bytesRead = jmax (0, (int) recv (socketHandle, buffer, (size_t) bytesToRead, MSG_WAITALL));
if (bytesRead == 0)
finished = true;
@@ -238,7 +239,7 @@ public:
if (wantedPos < position)
return false;
int64 numBytesToSkip = wantedPos - position;
auto numBytesToSkip = wantedPos - position;
auto skipBufferSize = (int) jmin (numBytesToSkip, (int64) 16384);
HeapBlock<char> temp (skipBufferSize);
@@ -286,14 +287,14 @@ private:
levelsOfRedirection = 0;
}
int createConnection (WebInputStream::Listener* listener, const int numRedirects)
int createConnection (WebInputStream::Listener* listener, int numRedirects)
{
closeSocket (false);
if (isPost)
WebInputStream::createHeadersAndPostData (url, headers, postData);
uint32 timeOutTime = Time::getMillisecondCounter();
auto timeOutTime = Time::getMillisecondCounter();
if (timeOutMs == 0)
timeOutMs = 30000;
@@ -305,6 +306,7 @@ private:
String hostName, hostPath;
int hostPort;
if (! decomposeURL (address, hostName, hostPath, hostPort))
return 0;
@@ -312,7 +314,8 @@ private:
int proxyPort = 0;
int port = 0;
const String proxyURL (getenv ("http_proxy"));
auto proxyURL = String::fromUTF8 (getenv ("http_proxy"));
if (proxyURL.startsWithIgnoreCase ("http://"))
{
if (! decomposeURL (proxyURL, proxyName, proxyPath, proxyPort))
@@ -335,6 +338,7 @@ private:
hints.ai_flags = AI_NUMERICSERV;
struct addrinfo* result = nullptr;
if (getaddrinfo (serverName.toUTF8(), String (port).toUTF8(), &hints, &result) != 0 || result == 0)
return 0;
@@ -379,17 +383,17 @@ private:
}
}
String responseHeader (readResponse (timeOutTime));
auto responseHeader = readResponse (timeOutTime);
position = 0;
if (responseHeader.isNotEmpty())
{
headerLines = StringArray::fromLines (responseHeader);
const int status = responseHeader.fromFirstOccurrenceOf (" ", false, false)
.substring (0, 3).getIntValue();
auto status = responseHeader.fromFirstOccurrenceOf (" ", false, false)
.substring (0, 3).getIntValue();
String location (findHeaderItem (headerLines, "Location:"));
auto location = findHeaderItem (headerLines, "Location:");
if (++levelsOfRedirection <= numRedirects
&& status >= 300 && status < 400
@@ -410,7 +414,7 @@ private:
return createConnection (listener, numRedirects);
}
String contentLengthString (findHeaderItem (headerLines, "Content-Length:"));
auto contentLengthString = findHeaderItem (headerLines, "Content-Length:");
if (contentLengthString.isNotEmpty())
contentLength = contentLengthString.getLargeIntValue();
@@ -425,7 +429,7 @@ private:
}
//==============================================================================
String readResponse (const uint32 timeOutTime)
String readResponse (uint32 timeOutTime)
{
int numConsecutiveLFs = 0;
MemoryOutputStream buffer;
@@ -436,6 +440,7 @@ private:
&& ! (finished || isError()))
{
char c = 0;
if (read (&c, 1) != 1)
return {};
@@ -447,7 +452,7 @@ private:
numConsecutiveLFs = 0;
}
const String header (buffer.toString().trimEnd());
auto header = buffer.toString().trimEnd();
if (header.startsWithIgnoreCase ("HTTP/"))
return header;
@@ -471,11 +476,11 @@ private:
dest << ':' << port;
}
static MemoryBlock createRequestHeader (const String& hostName, const int hostPort,
const String& proxyName, const int proxyPort,
static MemoryBlock createRequestHeader (const String& hostName, int hostPort,
const String& proxyName, int proxyPort,
const String& hostPath, const String& originalURL,
const String& userHeaders, const MemoryBlock& postData,
const bool isPost, const String& httpRequestCmd)
bool isPost, const String& httpRequestCmd)
{
MemoryOutputStream header;
@@ -503,7 +508,7 @@ private:
return header.getMemoryBlock();
}
static bool sendHeader (int socketHandle, const MemoryBlock& requestHeader, const uint32 timeOutTime,
static bool sendHeader (int socketHandle, const MemoryBlock& requestHeader, uint32 timeOutTime,
WebInputStream& pimplOwner, WebInputStream::Listener* listener)
{
size_t totalHeaderSent = 0;
@@ -513,7 +518,7 @@ private:
if (Time::getMillisecondCounter() > timeOutTime)
return false;
const int numToSend = jmin (1024, (int) (requestHeader.getSize() - totalHeaderSent));
auto numToSend = jmin (1024, (int) (requestHeader.getSize() - totalHeaderSent));
if (send (socketHandle, static_cast<const char*> (requestHeader.getData()) + totalHeaderSent, (size_t) numToSend, 0) != numToSend)
return false;
@@ -532,8 +537,9 @@ private:
if (! url.startsWithIgnoreCase ("http://"))
return false;
const int nextSlash = url.indexOfChar (7, '/');
int nextColon = url.indexOfChar (7, ':');
auto nextSlash = url.indexOfChar (7, '/');
auto nextColon = url.indexOfChar (7, ':');
if (nextColon > nextSlash && nextSlash > 0)
nextColon = -1;


+ 17
- 17
modules/juce_core/native/juce_linux_SystemStats.cpp View File

@@ -104,11 +104,11 @@ int SystemStats::getPageSize()
//==============================================================================
String SystemStats::getLogonName()
{
if (const char* user = getenv ("USER"))
return CharPointer_UTF8 (user);
if (auto user = getenv ("USER"))
return String::fromUTF8 (user);
if (struct passwd* const pw = getpwuid (getuid()))
return CharPointer_UTF8 (pw->pw_name);
if (auto pw = getpwuid (getuid()))
return String::fromUTF8 (pw->pw_name);
return {};
}
@@ -120,7 +120,8 @@ String SystemStats::getFullUserName()
String SystemStats::getComputerName()
{
char name [256] = { 0 };
char name[256] = {};
if (gethostname (name, sizeof (name) - 1) == 0)
return name;
@@ -129,20 +130,21 @@ String SystemStats::getComputerName()
static String getLocaleValue (nl_item key)
{
const char* oldLocale = ::setlocale (LC_ALL, "");
String result (String::fromUTF8 (nl_langinfo (key)));
auto oldLocale = ::setlocale (LC_ALL, "");
auto result = String::fromUTF8 (nl_langinfo (key));
::setlocale (LC_ALL, oldLocale);
return result;
}
String SystemStats::getUserLanguage() { return getLocaleValue (_NL_IDENTIFICATION_LANGUAGE); }
String SystemStats::getUserRegion() { return getLocaleValue (_NL_IDENTIFICATION_TERRITORY); }
String SystemStats::getDisplayLanguage() { return getUserLanguage() + "-" + getUserRegion(); }
String SystemStats::getUserLanguage() { return getLocaleValue (_NL_IDENTIFICATION_LANGUAGE); }
String SystemStats::getUserRegion() { return getLocaleValue (_NL_IDENTIFICATION_TERRITORY); }
String SystemStats::getDisplayLanguage() { return getUserLanguage() + "-" + getUserRegion(); }
//==============================================================================
void CPUInformation::initialise() noexcept
{
auto flags = getCpuInfo ("flags");
hasMMX = flags.contains ("mmx");
hasSSE = flags.contains ("sse");
hasSSE2 = flags.contains ("sse2");
@@ -176,19 +178,18 @@ void CPUInformation::initialise() noexcept
//==============================================================================
uint32 juce_millisecondsSinceStartup() noexcept
{
return uint32 (Time::getHighResolutionTicks() / 1000);
return (uint32) (Time::getHighResolutionTicks() / 1000);
}
int64 Time::getHighResolutionTicks() noexcept
{
#if JUCE_BELA
#if JUCE_BELA
return rt_timer_read() / 1000;
#else
#else
timespec t;
clock_gettime (CLOCK_MONOTONIC, &t);
return (t.tv_sec * (int64) 1000000) + (t.tv_nsec / 1000);
#endif
#endif
}
int64 Time::getHighResolutionTicksPerSecond() noexcept
@@ -215,8 +216,7 @@ JUCE_API bool JUCE_CALLTYPE juce_isRunningUnderDebugger() noexcept
#if JUCE_BSD
return false;
#else
return readPosixConfigFileValue ("/proc/self/status", "TracerPid")
.getIntValue() > 0;
return readPosixConfigFileValue ("/proc/self/status", "TracerPid").getIntValue() > 0;
#endif
}


+ 5
- 5
modules/juce_core/native/juce_linux_Threads.cpp View File

@@ -31,9 +31,9 @@ namespace juce
//==============================================================================
JUCE_API void JUCE_CALLTYPE Process::setPriority (const ProcessPriority prior)
{
const int policy = (prior <= NormalPriority) ? SCHED_OTHER : SCHED_RR;
const int minp = sched_get_priority_min (policy);
const int maxp = sched_get_priority_max (policy);
auto policy = (prior <= NormalPriority) ? SCHED_OTHER : SCHED_RR;
auto minp = sched_get_priority_min (policy);
auto maxp = sched_get_priority_max (policy);
struct sched_param param;
@@ -51,8 +51,8 @@ JUCE_API void JUCE_CALLTYPE Process::setPriority (const ProcessPriority prior)
static bool swapUserAndEffectiveUser()
{
int result1 = setreuid (geteuid(), getuid());
int result2 = setregid (getegid(), getgid());
auto result1 = setreuid (geteuid(), getuid());
auto result2 = setregid (getegid(), getgid());
return result1 == 0 && result2 == 0;
}


Loading…
Cancel
Save