Пример #1
0
/*!

\brief Trims ASCII white space from front and back of the String.
\details Defined in dmzTypesStringUtil.h.
This function remove white space from the front and back of the string passed in.
For example the string " this is a string " would be converted to "this is a string".
\param[in,out] value String to have white space trimmed.

*/
void
dmz::trim_ascii_white_space (String &value) {

   Boolean done (False);
   Int32 length (value.get_length ());
   Int32 count (length - 1);

   while (!done) {

      if (count < 0) { done = True; }
      else if (!is_ascii_white_space (value.get_char (count))) { done = True; }
      else { count--; }
   }

   if (count < (length - 1)) { value.set_length (count + 1); }

   done = False;
   length = value.get_length ();
   count = 0;

   while (!done) {

      if (count >= length) { done = True; }
      else if (!is_ascii_white_space (value.get_char (count))) { done = True; }
      else { count++; }
   }

   if (count) { value.shift (-count); }
}
Пример #2
0
	void replace (const String& search, const String& replace) {
		int index = find (search);
		while (index != -1) {
			string.replace (index, search.get_length(), replace.string);
			index += replace.get_length();
			index = find (search, index);
		}
	}
// Time Slice Interface
void
dmz::ForgePluginScreenCaptureMulti::update_time_slice (const Float64 TimeDelta) {

   if (_fileIndex > 0) {

      if (_fileIndex > 1) {

         _heading += TwoPi64 / Float64 (_maxFiles);
         _update_portal ();
      }

      String count (String::number (_fileIndex));
      if (count.get_length () < _maxLength) {

         count.shift (_maxLength - count.get_length (), '0');
      }

      String file (_fileRoot);
      file << count << ".png";
      _fileList.add (file);

      Data out = _convertString.to_data (file);
      _doCaptureMsg.send (&out);

      _fileIndex++;
      if (_fileIndex > _maxFiles) { _fileIndex = -1; }
   }
   else if (_fileIndex < 0) {

      const Int32 Size (_fileList.get_count ());

      Int32 count (0);
      StringContainerIterator it;
      String file;
      while (_fileList.get_next (it, file)) {

         if (is_valid_path (file)) { count++; }
         else { break; }
      }

      _update_portal ();

      if (Size == count) {

         Data out = _convertList.to_data (_fileList);
         _finishedCaptureMsg.send (&out);
         _fileIndex = 0;
      }
      else {

_log.error << "Not all screen capture images created. Created " << count << " of " << Size << endl;
      }
   }
}
Пример #4
0
bool wImport_Form::Modify_Line(const String &line, String &result)
{
	String modify_line;
	int quotation_num = 0;
	bool in_one_part = false;

	for(int i = 0; i < line.get_length(); i++)
	{
                if(line[i] == ',')   // if element equal to ','
                {
			if(! in_one_part)  // if this element is not a part
				modify_line.add(' ');  // convert ',' to space
			else
                                modify_line.add(line[i]);
                }

		while(i < line.get_length() && line[i] == '"')
		{
			quotation_num++;   // count the number of '"'
			i++;
		}

		if(quotation_num)
		{
			i--;  // go back to the nearest element which is '"'

			if(quotation_num % 2 == 1)
				in_one_part = ! in_one_part;

			for(int j = 0; j < (int) quotation_num / 2; j++)
				modify_line.add('"');

			quotation_num = 0;  // reset
		}

		if(! Filter(line[i]) && line[i] != ',' && line[i] != '"')
			modify_line.add(line[i]);
	}

	if(in_one_part)
	{
		cout << "{ ERROR! file : wimport_form.cpp / Modify_Line(const String &line, String &result)\n"
			<< "\" can't match\n"
			<< "}\n";
		return false;
	}

	result = modify_line;
	return true;
}
Пример #5
0
String wImport_Form::Delete_Remark(String line)
{
	for(int i = 0; i < line.get_length() - 1; i++)
		if(line[i] == '/' && line[i + 1] == '/')
			return line(0, i);
	return line;
}
Пример #6
0
/*!

\ingroup Runtime
\brief Remove last element from a scoped Config name.
\details If the String "dmz.type.value" was passed in as \a Name. The string "value"
would be returned in \a value and the string "dmz.type" would be returned in \a remainder.
\param[in] Name String containing scoped config context name.
\param[out] value String for storing the last element of the config context name.
\param[out] remainder String for storing the remaining part of the config context name.
\return Returns dmz::True if the \a Name was successfully separated.

*/
dmz::Boolean
dmz::pop_last_config_scope_element (
      const String Name,
      String &value,
      String &remainder) {

   Boolean result (False);

   const Int32 Length (Name.get_length ());
   Boolean done (False);
   Int32 place = Length - 1;

   while (!done) {

      if (place < 0) { done = True; }
      else if (Name.get_char (place) == LocalScopeChar) {

         done = True;
      }
      else { place--; }
   }

   if (place >= 1) { value = Name.get_sub (0, place - 1); }

   if (place >= 0) { remainder = Name.get_sub (place + 1); result = True; }

   return result;
}
Пример #7
0
YETI_Result HttpHeaders::parse(BufferedInputStream & stream)
{
    String header_name;
    String header_value;
    bool   header_pending = false;
    String line;

    while (YETI_SUCCEEDED(stream.read_line(line, YETI_HTTP_PROTOCOL_MAX_LINE_LENGTH))) {
        if (line.get_length() == 0) {
            // end of headers
            break;
        }
        if (header_pending && (line[0] == ' ' || line[0] == '\t')) {
            header_value.append(line.get_chars() + 1, line.get_length() - 1);
        } else {
            if (header_pending) {
                header_value.trim();
                add_header(header_name, header_value);
                header_pending = false;
                YETI_LOG_FINEST_2("header - %s: %s", header_name.get_chars(), header_value.get_chars());
            }

            int colon_index = line.find(':');
            if (colon_index < 1) {
                // invalid syntax, ignore
                continue;
            }
            header_name = line.left(colon_index);
            const char * value = line.get_chars() + colon_index + 1;
            while (*value == ' ' || *value == '\t') {
                value++;
            }
            header_value =  value;
            header_pending = true;
        }
    }

    if (header_pending) {
        header_value.trim();
        add_header(header_name, header_value);
        YETI_LOG_FINEST_2("header - %s: %s", header_name.get_chars(), header_value.get_chars());
    }

    return YETI_SUCCESS;
}
Пример #8
0
   void init () {

      // size will be different than length if a Header and/or Footer is defined.
      size_t size  = get_file_size (FileName);
      length = size;

      if (Header) { length += Header.get_length (); size = length; }
      if (Footer) { length += Footer.get_length (); }

      if (length > 0) {

         buffer = new char[length + 1];

         if (buffer) {

            buffer[length] = '\0';

            Int32 place (0);

            if (Header) {

               strncpy (buffer, Header.get_buffer (), Header.get_length ());
               place += Header.get_length ();
            }

            FILE *file = open_file (FileName, "rb");

            if (file) {

               while (place < size) {

                  place += read_file (file, length - place, &(buffer[place]));
               }

               close_file (file);
            }

            if (Footer) {

               strncpy (&(buffer[place]), Footer.get_buffer (), Footer.get_length ());
            }
         }
      }
   }
Пример #9
0
void
dmz::ArchivePluginAutoSave::update_plugin_state (
      const PluginStateEnum State,
      const UInt32 Level) {

   if (State == PluginStateStart) {

      if (_firstStart && _saveFile && is_valid_path (_saveFile) && _archiveMod) {

         _log.info << "Restoring from auto save archive: " << _saveFile << endl;

         Config global ("global");
         XMLParser parser;
         XMLInterpreterConfig interpreter (global);
         parser.set_interpreter (&interpreter);

         FILE *file = open_file (_saveFile, "rb");

         if (file) {

            Boolean error (False);
            String buffer;

            while (read_file (file, 1024, buffer) && !error) {

               const Int32 Length = buffer.get_length ();
               const char *cbuf = buffer.get_buffer ();

               if (!parser.parse_buffer (cbuf, Length, Length < 1024)) {

                  error = True;
                  _log.error << "Unable to restore from auto save archive: " << _saveFile
                     << " : " << parser.get_error ();
               }
            }

            close_file (file);

            Config data;

            if (!error && global.lookup_all_config_merged ("dmz", data)) {

               _archiveMod->process_archive (_archiveHandle, data);
            }
         }
      }

      _firstStart = False;
   }
   else if (State == PluginStateShutdown) {

      if (is_valid_path (_saveFile)) { remove_file (_saveFile); }
   }
}
Пример #10
0
   State (
         const char *Buffer,
         const size_t Length,
         const String Header,
         const String &Footer) :
         buffer (0),
         length (Length),
         count (1) {

      if (Header) { length += Header.get_length (); }
      if (Footer) { length += Footer.get_length (); }

      if (length > 0) {

         size_t place (0);

         buffer = new char[length + 1];

         if (buffer) {

            buffer[length] = '\0';

            if (Header) {

               strncpy (buffer, Header.get_buffer (), Header.get_length ());
               place += Header.get_length ();
            }

            if (Length) {

               strncpy (&(buffer[place]), Buffer, Length);
               place += Length;
            }

            if (Footer) {

               strncpy (&(buffer[place]), Footer.get_buffer (), Footer.get_length ());
            }
         }
      }
   }
Пример #11
0
YETI_Result HttpResponse::parse(BufferedInputStream & stream,
                                HttpResponse *& response)
{
    response = NULL;
    String line;
    YETI_CHECK_FINE(stream.read_line(line, YETI_HTTP_PROTOCOL_MAX_LINE_LENGTH));
    /*if (YETI_FAILED(res)) {
    if (res != YETI_ERROR_TIMEOUT && res != YETI_ERROR_EOS) YETI_CHECK_WARNING(res);
    return res;
    }*/

    YETI_LOG_FINE_1("http response: %s", line.get_chars());

    int first_space = line.find(' ');
    if (first_space < 1) return YETI_ERROR_HTTP_INVALID_RESPONSE_LINE;
    int second_space =  line.find(' ', first_space + 1);
    if (second_space < 0) {
        if (line.get_length() != 12) {
            return YETI_ERROR_HTTP_INVALID_RESPONSE_LINE;
        }
    } else if (second_space != 4) {
        return YETI_ERROR_HTTP_INVALID_RESPONSE_LINE;
    }

    String protocol = line.sub_string(0, first_space);
    String status_code = line.sub_string(first_space + 1, 3);
    String reason_phrase = line.sub_string(first_space + 1 + 3 + 1,
        line.get_length() - (first_space + 1 + 3 + 1));

    YETI_UInt32 status_code_int = 0;
    status_code.to_integer32(status_code_int);
    response = new HttpResponse(status_code_int, reason_phrase, protocol);

    YETI_Result result = response->parse_headers(stream);
    if (YETI_FAILED(result)) {
        delete response;
        response = NULL;
    }

    return result;
}
Пример #12
0
/*!

\brief Converts ASCII white space in string to \a Target 8 bit character.
\details Defined in dmzTypesStringUtil.h.

*/
void
dmz::convert_ascii_white_space (
      String &value,
      const char Target) {

   const Int32 Length (value.get_length ());

   for (Int32 ix = 0; ix < Length; ix++) {

      if (is_ascii_white_space (value.get_char (ix))) { value.set_char (ix, Target); }
   }
}
Пример #13
0
void HttpEnvProxySelector::_parse_proxy_env(const String & env, HttpProxyAddress & proxy)
{
    if (env.get_length() == 0) return;
    String proxy_spec;
    if (env.find("://") >= 0) {
        proxy_spec = env;
    } else {
        proxy_spec = "http://" + env;
    }
    Url url(proxy_spec);
    proxy.set_hostname(url.get_host());
    proxy.set_port(url.get_port());
}
Пример #14
0
	List<String> split (const String& delimiter) const {
		// TODO: handle empty delimiter
		List<String> result;
		int start = 0;
		int index = find (delimiter);
		while (index != -1) {
			if (index > start) result.append (substring(start, index));
			start = index + delimiter.get_length ();
			index = find (delimiter, start);
		}
		if (start < get_length()) result.append (substring(start));
		return result;
	}
Пример #15
0
/*!

\ingroup Foundation
\brief Converts an XML file to a config context tree.
\details Defined in dmzFoundationXMLUtil.h.
\param[in] FileName String containing name of XML file to parse.
\param[out] data Config object to store parsed XML data.
\param[in] log Pointer to Log for streaming log messages.
\return Returns dmz::True if the XML file was successfully parsed.
\sa dmz::Config \n dmz::ConfigContext

*/
dmz::Boolean
dmz::xml_to_config (const String &FileName, Config &data, Log *log) {

   if (!data) { Config tmp ("global"); data = tmp; }

   ParserXML parser;
   InterpreterXMLConfig interpreter (data);
   parser.set_interpreter (&interpreter);
   Boolean error (False);

   FILE *file = open_file (FileName, "rb");

   if (file) {

      String buffer;

      while (read_file (file, 1024, buffer) && !error) {

         const Int32 Length = buffer.get_length ();
         const char *cbuf = buffer.get_buffer ();

         if (!parser.parse_buffer (cbuf, Length, Length < 1024)) {

            error = True;

            if (log) {

               log->error << "In file: " << FileName << " : " << parser.get_error ()
                  << endl;
            }
         }
      }

      close_file (file);
   }
   else {

      error = True;

      if (log) {

         log->error << "Unable to open file: " << FileName << endl;
      }
   }

   return !error;
}
Пример #16
0
/*!

\ingroup Foundation
\brief Converts an XML String to a config context tree.
\details Defined in dmzFoundationXMLUtil.h.
\param[in] Value String containing the XML  to parse.
\param[out] data Config object to store parsed XML data.
\param[in] log Pointer to Log for streaming log messages.
\return Returns dmz::True if the XML was successfully parsed.
\sa dmz::Config \n dmz::ConfigContext

*/
dmz::Boolean
dmz::xml_string_to_config (const String &Value, Config &data, Log *log) {

   ParserXML parser;
   InterpreterXMLConfig interpreter (data);
   parser.set_interpreter (&interpreter);
   Boolean error (False);

   if (!parser.parse_buffer (Value.get_buffer (), Value.get_length (), True)) {

      error = True;

      if (log) {

         log->error << "Error parsing: " << endl << "\t"
            << Value << endl << "\t" << parser.get_error () << endl;
      }
   }

   return !error;
}
Пример #17
0
bool wImport_Form::String_Element(String &str, String &element, String &rest)
{
	element = EMPTY_STRING;
	rest = EMPTY_STRING;
    bool in_one_part = false;
	int i = 0;
	int begin, end, rest_begin;
	bool is_str = false, is_arr = false, is_map = false;

	// looking for begin
	for(i = 0; i < str.get_length(); i++)
    {
		if(str[i] != ' ')
		{
			if(str[i] == '"')
				is_str = true;
			if(str[i] == '{')
				is_arr = true;
			if(str[i] == '[')
				is_map = true;
			begin = i;
			break;
		}
    }
    
	if(i == str.get_length())
		return true;

	// looking for end
	for(i = begin; i < str.get_length(); i++)
	{
		if(! is_str && ! is_arr && ! is_map && str[i] == ' ')
		{
			end = i;
			break;
		}
		if(str[i] == '}')
			is_arr = false;
		if(str[i] == '"')
			is_str = false;
		if(str[i] == ']')
			is_map = false;
	}

	if(i == str.get_length())
	{
		if(! is_arr && ! is_str && ! is_map)
		{
			end = str.get_length();
			element = str(begin, end - begin);
			return true;
		} else
		{
			cout << "{ ERROR! file : wimport_form.cpp / String_Element(String &str, String &element, String &rest)\n"
				<< "fail to generate regular string element\n"
				<< "}\n";
			return false;
		}
	}

	element = str(begin, end - begin);

	// looking for rest_begin
	for(i = end; i < str.get_length(); i++)
    {
		if(str[i] != ' ')
		{
			rest_begin = i;
			break;
		}
    }

	if(i == str.get_length())
		return true;

	rest = str(rest_begin, str.get_length() - rest_begin);
    
	return true;
}
Пример #18
0
YETI_Result HttpRequest::parse(BufferedInputStream & stream,
                               const SocketAddress * endpoint,
                               HttpRequest *& request)
{
    // default return value
    request = NULL;
skip_first_empty_line:
    // read the request line
    String line;
    YETI_CHECK_FINER(stream.read_line(line, YETI_HTTP_PROTOCOL_MAX_LINE_LENGTH));
    YETI_LOG_FINEST_1("http request: %s", line.get_chars());
    // when using keep-alive connections, clients such as XBox 360
    // incorrectly send a few empty lines as body for GET requests
    // so we try to skip them until we find something to parse
    if (line.get_length() == 0) goto skip_first_empty_line;
    if (line.get_length() == 0) goto skip_first_empty_line;

    // check the request line
    int first_space = line.find(' ');
    if (first_space < 0) {
        YETI_LOG_FINE_1("http request: %s", line.get_chars());
        return YETI_ERROR_HTTP_INVALID_REQUEST_LINE;
    }
    int second_space = line.find(' ', first_space + 1);
    if (second_space < 0) {
        YETI_LOG_FINE_1("http request: %s", line.get_chars());
        return YETI_ERROR_HTTP_INVALID_REQUEST_LINE;
    }

    // parse the request line
    String method = line.sub_string(0, first_space);
    String uri = line.sub_string(first_space + 1, second_space - first_space - 1);
    String protocol = line.sub_string(second_space + 1);

    // create a request
    bool proxy_style_request = false;
    if (uri.starts_with("http://", true)) {
        // proxy-style request with absolute URI
        request = new HttpRequest(uri, method, protocol);
        proxy_style_request = true;
    } else {
        // normal absolute path request
        request = new HttpRequest("http:", method, protocol);
    }

    // parse headers
    YETI_Result result = request->parse_headers(stream);
    if (YETI_FAILED(result)) {
        delete request;
        request = NULL;
        return result;
    }

    // update the URL
    if (!proxy_style_request) {
        request->m_url_.set_scheme("http");
        request->m_url_.parse_pathplus(uri);
        request->m_url_.set_port(YETI_HTTP_DEFAULT_PORT);

        // check for a Host: header
        HttpHeader * host_header = request->get_headers().get_header(YETI_HTTP_HEADER_HOST);
        if (host_header) {
            request->m_url_.set_host(host_header->get_value());
            // host sometimes doesn't contain port
            if (endpoint) {
                request->m_url_.set_port(endpoint->get_port());
            }
        } else {
            // use the endpoint as the host
            if (endpoint) {
                request->m_url_.set_host(endpoint->to_string());
            } else {
                // use defaults
                request->m_url_.set_host("localhost");
            }
        }
    }

    return YETI_SUCCESS;
}
Пример #19
0
YETI_Result File::save(const char * path, String & data)
{
    DataBuffer buffer(data.get_chars(), data.get_length());
    return File::save(path, buffer);
}
Пример #20
0
bool wImport_Form::To_Mix(String &element, Mix &result)
{
	int length = element.get_length();

	// if element is string
	if(element[0] == '"' && element[length - 1] == '"')
	{
		result = element(1, length - 2);
		return true;            // get string
	}

	// if element is number
	Array<int> left, right;
	bool pass_radix_point = false;
	bool is_number = true;
	bool positive_number = true;

	if(element[0] == '-')
	{
		positive_number = false;
		element = element(1, length - 1);
		length = length - 1;
	}

	for(int i = 0; i < length; i++)
	{
    if(((element[i] < '0' || element[i] > '9') && element[i] != '.') || (element[i] == '.' && pass_radix_point))
		{
			is_number = false;  // element isn't a number
			break;
		}

		if(element[i] == '.')
		{
			pass_radix_point = true;
			continue;
		}

		if(! pass_radix_point)
			left.add(element[i] - '0');
		else
			right.add(element[i] - '0');
	}

	if(is_number && !pass_radix_point)
	{
		int integer_num = 0;
		for(int i = 0; i < left.get_length(); i++)
			integer_num = integer_num * 10 + left[i];

		if(positive_number)
			result = integer_num;
		else
			result = -integer_num;
		return true;                             // get int
	} else
	if(is_number && pass_radix_point)
	{
		double double_num = 0;
		for(int i = 0; i < left.get_length(); i++)
			double_num = double_num * 10 + left[i];

		for(int i = 0; i < right.get_length(); i++)
			double_num = double_num + right[i] * pow(0.1, i + 1);

		if(positive_number)
			result = double_num;
		else
			result = -double_num;
		return true;                                // get double
	}

	// if element is array
	Array<Mix> arr;
	String line, pre_element, rest;
	line = element;
	Mix m;

	if(line[0] == '{' && line[length - 1] == '}')
	{
		line = line(1, line.get_length() - 2);
		do{
			if(! Array_Element(line, pre_element, rest))
			{
				cout << "{ ERROR! file : wimport_form.cpp/To_Mix(String &element, Mix &result)\n"
					<< "error function : Array_Element\n"
					<< "parameter1 : \n"
					<< line
					<< "\nparameter2 : \n"
					<< pre_element
					<< "\nparameter3 : \n"
					<< rest
					<< "\n}\n";
				return false;
			}
			if(pre_element == EMPTY_STRING && rest != EMPTY_STRING)
			{
				cout << "{ ERROR! file : wimport_form.cpp/To_Mix(String &element, Mix &result)\n"
					<< "generate array : pre_element == EMPTY_STRING && rest != EMPTY_STRING \n"
					<< "}\n";
				return false;
			}

			if(pre_element != EMPTY_STRING)
			{
				if(! To_Mix(pre_element, m))
				{
					cout << "{ ERROR! file : wimport_form.cpp/To_Mix(String &element, Mix &result)\n"
						<< "error function : To_Mix\n"
						<< "parameter1 : \n"
						<< pre_element
						<< "\nparameter2 : \n"
						<< m
						<< "\n}\n";
					return false;
				}
				arr.add(m);

				line = rest;
			}
		}while(rest != EMPTY_STRING);

		result = arr;
		return true;
	}

	// if element is mapping
	Mapping<Mix, Mix> mapp;
	bool is_end_mapping = true;
		//  All have declare before
		//	String line, pre_element, rest;
		//	line = element;
	Mix key, value;

	if(line[0] == '[' && line[length - 1] == ']')
	{
		line = line(1, line.get_length() - 2);

		do{
			if(! Mapping_Element(line, pre_element, rest, is_end_mapping))
			{
				cout << "{ ERROR! file : wimport_form.cpp / To_Mix(String &element, Mix &result)\n"
					<< " error function : Mapping_Element\n"
					<< "parameter1 : \n"
					<< line
					<< "\nparameter2 : \n"
					<< pre_element
					<< "\nparameter3 : \n"
					<< rest
					<< "\nparameter4 : \n"
					<< is_end_mapping
					<< "\n}\n";
				return false;
			}

			if(pre_element == EMPTY_STRING && rest != EMPTY_STRING)	
			{
				;
				cout << "{ ERROR! file : wimport_form.cpp/To_Mix(String &element, Mix &result)\n"
					<< "generate mapping : pre_element == EMPTY_STRING && rest != EMPTY_STRING \n"
					<< "}\n";
				return false;
			}

			if(pre_element != EMPTY_STRING)
			{
				if(! To_Mix(pre_element, m))
				{
					;
					cout << "{ ERROR! file : wimport_form.cpp/To_Mix(String &element, Mix &result)\n"
						<< "error function : To_Mix\n"
						<< "parameter1 : \n"
						<< pre_element
						<< "\nparameter2 : \n"
						<< m
						<< "\n}\n";
					return false;
				}
				
				if(is_end_mapping)
					value = m;
				else
					key = m;

				if(is_end_mapping)
					mapp[key] = value;

				line = rest;
			}

			if(rest == EMPTY_STRING && is_end_mapping == false)
			{
				;
				cout << "{ ERROR! file : wimport_form.cpp/To_Mix(String &element, Mix &result)\n"
					<< "only key found\n"
					<< "}\n";
				return false;
			}

		} while(rest != EMPTY_STRING);

		result = mapp;
		return true;
	}

	cout << "{ ERROR! file : wimport_form.cpp/To_Mix(String &element, Mix &result)\n"
		<< "error string form, can't be to Mix\n"
		<< "}\n";
	return false;
}
Пример #21
0
bool wImport_Form::Mapping_Element(String &line, String &element, String &rest, bool &is_end_mapping)
{
	element = EMPTY_STRING;
	rest = EMPTY_STRING;
	bool is_in_array = false, is_in_mapping = false;
	int begin = -1, end = -1, rest_begin = -1;
	int i = 0;
	char end_char;

	// looking for begin
	for(i = 0; i < line.get_length(); i++)
	{
		if(line[i] == ' ')
			continue;
		else
		{
			if(line[i] == '{')
				is_in_array = true;
			if(line[i] == '[')
				is_in_mapping = true;
			begin = i;
			break;
		}
	}

	// line only contain space
	if(i == line.get_length())
	{
		is_end_mapping = ! is_end_mapping;
		return true;
	}

	// looking for end
	for(i = begin;  i < line.get_length(); i++)
	{
		if(is_in_array)
		{
			if(line[i] != '}')
				continue;
			else
			{
				i++;
				end = i;
				is_in_array = false;
				break;
			}
		} else
		if(is_in_mapping)
		{
			if(line[i] != ']')
				continue;
			else
			{
				i++;
				end = i;
				is_in_mapping = false;
				break;
			}
		} else
		{
			if(is_end_mapping)
				end_char = ':';
			else
				end_char = ',';
			if(line[i] != ' ' && line[i] != end_char)
			{
				end = -1;
				continue;
			}
			if(line[i] == ' ' && end == -1)
				end = i;
			if(line[i] == end_char)
			{
				if(end != -1)
					break;
				else
				{
					end = i;
					break;
				}
			}
		}
	}

	if(is_in_array && i == line.get_length())
	{
		cout << "{ ERROR! file : wimport_form.cpp/Array_Element(String &line, String &element, String &rest)\n"
			<< "{} can't match\n"
			<< "}\n";
		return false;
	}

	if(is_in_mapping && i == line.get_length())
	{
		cout << "{ ERROR! file : wimport_form.cpp/Array_Element(String &line, String &element, String &rest)\n"
			<< "[] can't match\n"
			<< "}\n";
		return false;
	}

	if(end == -1 && i == line.get_length())
		end = line.get_length();

	element = line(begin, end - begin);
	if(i == line.get_length())
	{
		is_end_mapping = ! is_end_mapping;
		return true;
	}

	// looking for rest_begin
	for(i = end + 1; i < line.get_length(); i++)
	{
		if(is_end_mapping)
			end_char = ':';
		else
			end_char = ',';

		if(line[i] != ' ' && line[i] != end_char)
		{
			rest_begin = i;
			rest = line(i, line.get_length() - i);
			is_end_mapping = ! is_end_mapping;
			return true;
		}
	}

	// otherwise false
	cout << "{ ERROR! file : wimport_form.cpp/Array_Element(String &line, String &element, String &rest)\n"
		<< "wrong mapping form\n"
		<< "}\n";
	return false;
}
Пример #22
0
int
main (int argc, char *argv[]) {

   Test test ("dmzTypesStringTest", argc, argv);

   String str1;

   test.validate (
      "Default string constructor creates empty string.",
      !str1.get_buffer () && !str1.get_length () && !str1.get_size ());

   String str2 ("Foo");
   const Int32 Str2Len (str2.get_length ());
   test.validate (
      "String (char *) constructor creates string of correct length and size.",
      str2.get_buffer () && (Str2Len == 3) && (str2.get_size () > Str2Len));

   String str3 ("aaa");
   String str4 ("bbb");
   str3 << str4;
   const Int32 Str3Len (str3.get_length ());
   test.validate (
      "String to String stream operator.",
      !strcmp ("aaabbb", str3.get_buffer ()) &&
         (Str3Len == 6) &&
         (Str3Len < str3.get_size ()));

   const char *CharData = "Test String Value";
   const int CharDataLength = strlen (CharData);
   String inputValue (CharData);
   String copyCtrResult (inputValue);
   test.validate (
      "String copy constructor contains valid string.",
      (copyCtrResult.get_length () == CharDataLength) &&
      (!strcmp (copyCtrResult.get_buffer (), CharData)));

   String str5 ("..a...b....c.....d...");
   str5.replace_sub ("..", ".");
   test.validate (
      "String replace_sub sequential pattern.",
      !strcmp (str5.get_buffer (), ".a.b.c.d.") &&
         (str5.get_length () == 9) &&
         (str5.get_length () < str5.get_size ()));

   String str6 ("123456789");
   str6.flush ();
   test.validate (
      "String flush function.",
      !strcmp ("" , str6.get_buffer ()) &&
         (str6.get_length () == 0) &&
         (str6.get_size () >= 10));

   String str7 ("123456789");
   str7.empty ();
   test.validate (
      "String empty function.",
      (str7.get_buffer () == 0) &&
         (str7.get_length () == 0) &&
         (str7.get_size () == 0));

   String str8;
   str8.repeat ("123", 3);
   test.validate (
      "String repeat function.",
      !strcmp ("123123123", str8.get_buffer ()) &&
         (str8.get_length () == 9) &&
         (str8.get_size () >= 10));

   String str9 ("123456789aaa");
   str9.shift (-9);
   test.validate (
      "String shift left.",
      !strcmp ("aaa", str9.get_buffer ()) &&
      (str9.get_length () == 3) &&
      (str9.get_size () > 3));

   String str10 ("123456789");
   str10.shift (-9);
   test.validate (
      "String shift left to flush.",
      !strcmp ("", str10.get_buffer ()) &&
      (str10.get_length () == 0) &&
      (str10.get_size () >= 1));

   String str11 ("aaa");
   str11.shift (6, 'b');
   test.validate (
      "String shift right.",
      !strcmp ("bbbbbbaaa", str11.get_buffer ()) &&
      (str11.get_length () == 9) &&
      (str11.get_size () > 9));

   String str12 ("123456789");
   str12.flush ();
   test.validate (
      "Flush - String empty but not null.",
      !str12 && !str12.is_null ());

   String str13 ("123456789");
   str13.empty ();
   test.validate (
      "Empty - String empty and null.",
      !str13 && str13.is_null ());

   String str14;
   str14 += 'A';
   str14 += 'B';
   test.validate ("Append char 'A' and 'B'", str14 == "AB");

   String strAppend1 ("12345");
   String strAppend2 ("67890");
   String strResultAppend1 = strAppend1 + strAppend2;
   String strResultAppend2 ("");
   strResultAppend2 = strResultAppend2.append (strAppend1);
   strResultAppend2 = strResultAppend2.append (strAppend2);

   test.validate (
      "Appending strings with plus operator.",
      !strcmp ("1234567890", strResultAppend1.get_buffer ()));

   test.validate (
      "Appending strings with append function.",
      !strcmp ("1234567890", strResultAppend2.get_buffer ()));

   String strRepeat ("repeat!");
   String strRepeatResult ("");
   strRepeatResult = strRepeatResult.repeat (strRepeat, 4);

   test.validate (
      "Repeating a string",
      !strcmp ("repeat!repeat!repeat!repeat!", strRepeatResult.get_buffer ()));

   String strBoolean1 ("this is a test string!");
   String strBoolean2 ("this is a test string!");
   String strBoolean3 ("this is 4 test string!");

   test.validate (
      "String equality operator",
      strBoolean1 == strBoolean2);

   test.validate (
      "String inequality operator",
      strBoolean1 != strBoolean3);

   String strToUpperInitial ("this is a test string with symbols$%^!&*(");
   String strToLowerInitial ("THIS IS A TEST STRING WITH SYMBOLS$%^!&*(");

   String strToUpperFuncResult = strToUpperInitial.get_upper ();
   String strToLowerFuncResult = strToUpperInitial.get_lower ();

   strToUpperInitial.to_upper ();
   strToLowerInitial.to_lower ();

   test.validate (
      "To upper case inplace",
      !strcmp ("THIS IS A TEST STRING WITH SYMBOLS$%^!&*(",
               strToUpperInitial.get_buffer ()));
   test.validate (
      "To lower case inplace",
      !strcmp ("this is a test string with symbols$%^!&*(",
               strToLowerInitial.get_buffer ()));

   test.validate (
      "To upper case conversion function",
      !strcmp ("THIS IS A TEST STRING WITH SYMBOLS$%^!&*(",
               strToUpperFuncResult.get_buffer ()));
   test.validate (
      "To lower case conversion function",
      !strcmp ("this is a test string with symbols$%^!&*(",
               strToLowerFuncResult.get_buffer ()));

   String strSetGetChar ("abcdefghijk");
   const char grabbedChar1 = strSetGetChar.get_char (3);
   const char grabbedChar2 = strSetGetChar.get_char (5);

   test.validate (
      "Get a character - 1",
      (grabbedChar1 == 'd'));

   test.validate (
      "Get a character - 2",
      (grabbedChar2 == 'f'));

   test.validate (
      "Set a character - 1",
      strSetGetChar.set_char (3, 'x') &&
      !strcmp ("abcxefghijk", strSetGetChar.get_buffer ()));

   test.validate (
      "Set a character - 2",
      strSetGetChar.set_char (5, 'y') &&
      !strcmp ("abcxeyghijk", strSetGetChar.get_buffer ()));

   String strGetSub ("a1b2c3d4e5f6g7");
   String strSubRecieved1 = strGetSub.get_sub (2);
   String strSubRecieved2 = strGetSub.get_sub (2, 3);
   String strSubRecieved3 = strGetSub.get_sub (2, 2);
   String strSubRecieved4 = strGetSub.get_sub (12, 13);
   String strSubRecieved5 = strGetSub.get_sub (14);
   String strSubRecieved6 = strGetSub.get_sub (-1);
   String strSubRecieved7 = strGetSub.get_sub (-5);
   String strSubRecieved8 = strGetSub.get_sub (-15);


   test.validate (
      "Get substring - single index",
      strSubRecieved1 == String ("b2c3d4e5f6g7"));

   test.validate (
      "Get substring - index, index + 1",
      strSubRecieved2 == String ("b2"));

   test.validate (
      "Get substring - index, index",
      strSubRecieved3 == String ("b"));

   test.validate (
      "Get substring - lastIndex - 1, lastIndex",
      strSubRecieved4 == String ("g7"));

   test.validate (
      "Get substring - out of bounds after last index",
      strSubRecieved5.is_null ());

   test.validate (
      "Get substring - negative index, last character",
      strSubRecieved6 ==  String ("7"));

   test.validate (
      "Get substring - negative index, last 5 characters",
      strSubRecieved7 ==  String ("5f6g7"));

   test.validate (
      "Get substring - negative index with magnitude too large",
      strSubRecieved8.is_null ());



   Int32 findIndex;
   String strFindSubTarget ("item");
   String strFindSub1 ("01234567item0123item012345item01");
   String strFindSub2 ("01234item");
   Boolean findSubResult1 = strFindSub1.find_sub (strFindSubTarget, findIndex);

   test.validate (
      "Find substring - initial occurance",
      findSubResult1 &&
      (findIndex == 8));

   Boolean findSubResult2 = strFindSub1.find_sub (
      strFindSubTarget,
      findIndex,
      findIndex + strFindSubTarget.get_length());

   test.validate (
      "Find substring - occurance 2",
      findSubResult2 &&
      (findIndex == 16));

   Boolean findSubResult3 = strFindSub1.find_sub (
      strFindSubTarget,
      findIndex,
      findIndex + strFindSubTarget.get_length());

   test.validate (
      "Find substring - initial occurance",
      findSubResult3 &&
      (findIndex == 26));

   Boolean findSubResult4 = strFindSub1.find_sub (
      strFindSubTarget,
      findIndex,
      findIndex + strFindSubTarget.get_length());

   test.validate (
      "Find substring - no more occurances",
      !findSubResult4);

   Int32 findIndex2;
   Boolean findSubResult5 = strFindSub2.find_sub (strFindSubTarget, findIndex2);
   test.validate (
      "Find substring - initial occurance (test 2)",
      findSubResult5 &&
      (findIndex2 == 5));

   Boolean findSubResult6 = strFindSub2.find_sub (
      strFindSubTarget,
      findIndex2,
      findIndex2 + strFindSubTarget.get_length());

   test.validate (
      "Find substring - no more occurances (test 2)",
      !findSubResult6);

   Int32 index (-1);
   String strFindSub3 ("String to compare.");
   String strFindSub4 (strFindSub3);

   test.validate (
      "find_sub returns True when sub string is the same",
      strFindSub3.find_sub (strFindSub4, index));

   return test.result ();
}
Пример #23
0
/*!

\brief Splits a path into the directory, file name, and file extension.
\ingroup System
\details Defined in dmzSystemFile.h.
The \a FullPath is formatted before processing. The file extension is returned with
the leading period. If either a path or extension is not found in the \a FullPath,
the corresponding return value is an empty string.
\param[in] FullPath String containing full path to be split up.
\param[out] path String containing the directory component of the \a FullPath.
\param[out] file String containing the file component of the \a FullPath.
\param[out] ext String containing the extension of the \a FullPath.

*/
void
dmz::split_path_file_ext (
    const String &FullPath,
    String &path,
    String &file,
    String &ext) {

    path = format_path (FullPath);
    file.flush ();
    ext.flush ();

    const Int32 Length = path.get_length ();

    Int32 slashFound = -1;
    Int32 index = Length - 1;

    Boolean done (False);

    while (!done) {

        if (path.get_char (index) == '/') {
            slashFound = index;
            done = True;
        }
        else {
            index--;
            if (index < 0) {
                done = True;
            }
        }
    }

    if (slashFound >= 0) {

        file = path.get_sub (slashFound + 1);
        path = path.get_sub (0, slashFound);
    }
    else {

        file = path;
        path.flush ();
    }

    if (file) {

        const Int32 FileLength = file.get_length ();
        Int32 dotFound = -1;

        index = FileLength - 1;
        done = False;

        while (!done) {

            if (file.get_char (index) == '.') {
                dotFound = index;
                done = True;
            }
            else {
                index--;
                if (index < 0) {
                    done = True;
                }
            }
        }

        if (dotFound > 0) {

            ext = file.get_sub (dotFound);
            file = file.get_sub (0, dotFound - 1);
        }
    }
}
Пример #24
0
YETI_Result HttpEntity::set_input_stream(const String & string)
{
    MemoryStream * memory_stream = new MemoryStream((const void *)string.get_chars(), string.get_length());
    InputStreamReference body(memory_stream);
    return set_input_stream(body, true);
}