Beispiel #1
0
/*----------------------------------------------------------------------
|   NPT_UrlQuery::Parse
+---------------------------------------------------------------------*/
NPT_Result 
NPT_UrlQuery::Parse(const char* query)
{
    const char* cursor = query;
    NPT_String  name;
    NPT_String  value;
    bool        in_name = true;
    do {
        if (*cursor == '\0' || *cursor == '&') {
            if (!name.IsEmpty() && !value.IsEmpty()) {
                AddField(name, value, true);   
            }
            name.SetLength(0);
            value.SetLength(0);
            in_name = true;
        } else if (*cursor == '=' && in_name) {
            in_name = false;
        } else {
            if (in_name) {
                name += *cursor;
            } else {
                value += *cursor;
            }
        }
    } while (*cursor++);
    
    return NPT_SUCCESS;
}
/*----------------------------------------------------------------------
|   NPT_String::Format
+---------------------------------------------------------------------*/
NPT_String
NPT_String::Format(const char* format, ...)
{
    NPT_String result;
    NPT_Size   buffer_size = NPT_STRING_FORMAT_BUFFER_DEFAULT_SIZE; // default value
    
    va_list  args;

    for(;;) {
        /* try to format (it might not fit) */
        result.Reserve(buffer_size);
        char* buffer = result.UseChars();
        va_start(args, format);
        int f_result = NPT_FormatStringVN(buffer, buffer_size, format, args);
        va_end(args);
        if (f_result >= (int)(buffer_size)) f_result = -1;
        if (f_result >= 0) {
            result.SetLength(f_result);
            break;
        }
        
        /* the buffer was too small, try something bigger         */
        /* (we don't trust the return value of NPT_FormatStringVN */
        /* for the actual size needed)                            */
        buffer_size *= 2;
        if (buffer_size > NPT_STRING_FORMAT_BUFFER_MAX_SIZE) break;
    }
    
    return result;
}
Beispiel #3
0
/*----------------------------------------------------------------------
|   NPT_File::Load
+---------------------------------------------------------------------*/
NPT_Result
NPT_File::Load(const char* path, NPT_String& data, NPT_FileInterface::OpenMode mode)
{
    NPT_DataBuffer buffer;

    // reset ouput params
    data = "";

    // create and open the file
    NPT_File file(path);
    NPT_Result result = file.Open(mode);
    if (NPT_FAILED(result)) return result;
    
    // load the file
    result = file.Load(buffer);

    if (NPT_SUCCEEDED(result) && buffer.GetDataSize() > 0) {
        data.Assign((const char*)buffer.GetData(), buffer.GetDataSize());
        data.SetLength(buffer.GetDataSize());
    }

    // close the file
    file.Close();

    return result;
}
Beispiel #4
0
/*----------------------------------------------------------------------
|   NPT_DirectorySplitFilePath
+---------------------------------------------------------------------*/
NPT_Result 
NPT_DirectorySplitFilePath(const char* filepath, 
                           NPT_String& path, 
                           NPT_String& filename)
{
    if (!filepath || filepath[0] == '\0') 
        return NPT_ERROR_INVALID_PARAMETERS;

    path = filepath;

    char       last_char;
    NPT_Int32  i = path.GetLength();
    do {
        last_char = path[i-1];
        if (last_char == '\\' || last_char == '/') 
            break;
    } while (--i);

    // we need at least one delimiter and it cannot be last
    if (i == 0 || i == (NPT_Int32)path.GetLength())  {
        return NPT_ERROR_INVALID_PARAMETERS;
    }

    // assign filename
    filename = filepath+i;

    // truncate path & remove trailing slashes
    NPT_CHECK_FATAL(path.SetLength(i-1));

    // remove excessive delimiters
    path.TrimRight("/");
    path.TrimRight("\\");
    return NPT_SUCCESS;
}
/*----------------------------------------------------------------------
|   NPT_GetEnvironment
+---------------------------------------------------------------------*/
NPT_Result 
NPT_GetEnvironment(const char* name, NPT_String& value)
{
    char* env;

    /* default value */
    value.SetLength(0);

#if defined(NPT_CONFIG_HAVE_GETENV)
    env = getenv(name);
    if (env) {
        value = env;
        return NPT_SUCCESS;
    } else {
        return NPT_ERROR_NO_SUCH_ITEM;
    }
#elif defined(NPT_CONFIG_HAVE_DUPENV_S)
    if (dupenv_s(&env, NULL, name) != 0) {
        return NPT_FAILURE;
    } else if (env != NULL) {
        value = env;
        free(env);
        return NPT_SUCCESS;
    } else {
        return NPT_ERROR_NO_SUCH_ITEM;
    }
#else
#error "no getenv or getenv_s available on this platform"
#endif
}
/*----------------------------------------------------------------------
|   PLT_DeviceData::SetURLBase
+---------------------------------------------------------------------*/
NPT_Result
PLT_DeviceData::SetURLBase(NPT_HttpUrl& url) 
{
    // only http scheme supported
    m_URLBase.SetScheme(url.GetScheme());

    // update port if any
    if (url.GetPort() != NPT_URL_INVALID_PORT) m_URLBase.SetPort(url.GetPort());

    // update host if any
    if (!url.GetHost().IsEmpty()) m_URLBase.SetHost(url.GetHost());

    // update path
    NPT_String path = url.GetPath();

    // remove trailing file according to RFC 2396
    if (!path.EndsWith("/")) {
        int index = path.ReverseFind('/');
        if (index < 0) return NPT_FAILURE;
        path.SetLength(index+1);
    }
    m_URLBase.SetPath(path, true);

    return NPT_SUCCESS;
}    
Beispiel #7
0
/*----------------------------------------------------------------------
|   CMediaCrawler::SplitObjectId
+---------------------------------------------------------------------*/
NPT_Result
CMediaCrawler::SplitObjectId(const NPT_String& object_id, NPT_String& server_uuid, NPT_String& server_object_id)
{
    // reset output params
    server_uuid = "";
    server_object_id = "";

    if (object_id.GetLength() == 0 || object_id[0] != '0')
        return NPT_ERROR_INVALID_FORMAT;

    if (object_id.GetLength() > 1) {
        if (object_id[1] != '/') return NPT_ERROR_INVALID_FORMAT;
    
        server_uuid = object_id.SubString(2);

        // look for next delimiter
        int index = server_uuid.Find('/');
        if (index >= 0) {
            server_object_id = server_uuid.SubString(index+1);
            server_uuid.SetLength(index);
        }
    }

    return NPT_SUCCESS;
}
Beispiel #8
0
/*----------------------------------------------------------------------
|   NPT_FilePath::DirectoryName
+---------------------------------------------------------------------*/
NPT_String 
NPT_FilePath::DirectoryName(const char* path)
{
    NPT_String result = path;
    int separator = result.ReverseFind(Separator);
    if (separator >= 0) {
        if (separator == 0) {
            result.SetLength(NPT_StringLength(Separator));
        } else {
            result.SetLength(separator);
        }
    } else {
        result.SetLength(0);
    } 

    return result;
}
Beispiel #9
0
/*----------------------------------------------------------------------
|   AppendNumber
+---------------------------------------------------------------------*/
static void
AppendNumber(NPT_String& output, NPT_UInt32 number, unsigned int digit_count)
{
    NPT_Size new_length = output.GetLength()+digit_count;
    output.SetLength(new_length);
    char* dest = output.UseChars()+new_length;
    while (digit_count--) {
        *--dest = '0'+(number%10);
        number /= 10;
    }
}
/*----------------------------------------------------------------------
|   NPT_BufferedInputStream::ReadLine
+---------------------------------------------------------------------*/
NPT_Result
NPT_BufferedInputStream::ReadLine(NPT_String& line,
                                  NPT_Size    max_chars,
                                  bool        break_on_cr)
{
    // clear the line
    line.SetLength(0);

    // reserve space for the chars
    line.Reserve(max_chars);

    // read the line
    NPT_Size chars_read = 0;
    NPT_CHECK_NOLOGTIMEOUT(ReadLine(line.UseChars(), max_chars, &chars_read, break_on_cr));

    // adjust the length of the string object
    line.SetLength(chars_read);

    return NPT_SUCCESS;
}
Beispiel #11
0
/*----------------------------------------------------------------------
|   NPT_FilePath::FileExtension
+---------------------------------------------------------------------*/
NPT_String 
NPT_FilePath::FileExtension(const char* path)
{
    NPT_String result = path;
    int separator = result.ReverseFind('.');
    if (separator >= 0) {
        result = path+separator;
    } else {
        result.SetLength(0);
    }

    return result;
}
/*----------------------------------------------------------------------
|   NPT_FilePath::BaseName
+---------------------------------------------------------------------*/
NPT_String 
NPT_FilePath::BaseName(const char* path, bool with_extension /* = true */)
{
    NPT_String result = path;
    int separator = result.ReverseFind(Separator);
    if (separator >= 0) {
        result = path+separator+NPT_StringLength(Separator);
    } 

    if (!with_extension) {
        int dot = result.ReverseFind('.');
        if (dot >= 0) {
            result.SetLength(dot);
        }
    }

    return result;
}
/*----------------------------------------------------------------------
|   NPT_File::GetWorkingDir
+---------------------------------------------------------------------*/
NPT_Result
NPT_File::GetWorkingDir(NPT_String& path)
{
    path.SetLength(0);
    return NPT_ERROR_NOT_IMPLEMENTED;
}
Beispiel #14
0
/*----------------------------------------------------------------------
|   NPT_DateTime::ToString
+---------------------------------------------------------------------*/
NPT_String
NPT_DateTime::ToString(Format format, NPT_Flags flags) const
{
    NPT_String result;
    
    if (NPT_FAILED(CheckDate(*this))) return result;
    
    switch (format) {
        case FORMAT_W3C:
            AppendNumber(result, m_Year, 4);
            result += '-';
            AppendNumber(result, m_Month, 2);
            result += '-';
            AppendNumber(result, m_Day, 2);
            result += 'T';
            AppendNumber(result, m_Hours, 2);
            result += ':';
            AppendNumber(result, m_Minutes, 2);
            result += ':';
            AppendNumber(result, m_Seconds, 2);
            if (flags & FLAG_EMIT_FRACTION) {
                result += '.';
                if (flags & FLAG_EXTENDED_PRECISION) {
                    // nanoseconds precision
                    AppendNumber(result, m_NanoSeconds, 9);
                } else {
                    // only miliseconds precision
                    AppendNumber(result, m_NanoSeconds/1000000, 3);
                }
            }
            if (m_TimeZone) {
                NPT_UInt32 tz;
                if (m_TimeZone > 0) {
                    result += '+';
                    tz = m_TimeZone;
                } else {
                    result += '-';
                    tz = -m_TimeZone;
                }
                AppendNumber(result, tz/60, 2);
                result += ':';
                AppendNumber(result, tz%60, 2);
            } else {
                result += 'Z';
            }
            break;
            
        case FORMAT_ANSI: {
            // compute the number of days elapsed since 1900
            NPT_UInt32 days = ElapsedDaysSince1900(*this);
            
            // format the result
            result.SetLength(24);
            NPT_FormatString(result.UseChars(), result.GetLength()+1, 
                             "%.3s %.3s%3d %.2d:%.2d:%.2d %d",
                             NPT_TIME_DAYS_SHORT[(days+1)%7],
                             NPT_TIME_MONTHS[m_Month-1],
                             m_Day,
                             m_Hours,
                             m_Minutes,
                             m_Seconds,
                             m_Year);
            break;
        }
            
        case FORMAT_RFC_1036:
        case FORMAT_RFC_1123: {
            // compute the number of days elapsed since 1900
            NPT_UInt32 days = ElapsedDaysSince1900(*this);

            if (format == FORMAT_RFC_1036) {
                result += NPT_TIME_DAYS_LONG[(days+1)%7];
                result += ", ";
                AppendNumber(result, m_Day, 2);
                result += '-';
                result += NPT_TIME_MONTHS[m_Month-1];
                result += '-';
                AppendNumber(result, m_Year%100, 2);
            } else {
                result += NPT_TIME_DAYS_SHORT[(days+1)%7];
                result += ", ";
                AppendNumber(result, m_Day, 2);
                result += ' ';
                result += NPT_TIME_MONTHS[m_Month-1];
                result += ' ';
                AppendNumber(result, m_Year, 4);
            }
            result += ' ';
            AppendNumber(result, m_Hours, 2);
            result += ':';
            AppendNumber(result, m_Minutes, 2);
            result += ':';
            AppendNumber(result, m_Seconds, 2);
            if (m_TimeZone) {
                if (m_TimeZone > 0) {
                    result += " +";
                    AppendNumber(result, m_TimeZone/60, 2);
                    AppendNumber(result, m_TimeZone%60, 2);
                } else {
                    result += " -";
                    AppendNumber(result, -m_TimeZone/60, 2);
                    AppendNumber(result, -m_TimeZone%60, 2);
                }
            } else {
                result += " GMT";
            }
            break;
        }
    }

    return result;
}