Пример #1
0
static void ProcessIniLine( unsigned char *MyLine )
{
	char *p;
	char *key, *value;
	
	if ( MyLine == NULL || strlen((char *)MyLine) < 1 )
		return;
	
	/* trim off comment char */
	p = strchr( (char *) MyLine, '#' );
	if ( p != NULL )
		*p = 0;
	
	p = TrimWhitespace( (char *) MyLine );
	if ( strlen( (char *) p ) < 1 ) /* empty line */
		return;
	
	/* break into key and value */
	key = p;
	p = strchr( (char *) p, '=' );
	if ( p == NULL )  /* no equal implies not key and value */
		return;
	
	*p = 0;
	value = p + 1;
	key = TrimWhitespace( (char *) key );
	value = TrimWhitespace( (char *) value );

	DL_KeyValue_Add( &head, (char *) key, (char *) value, FALSE );
}
Пример #2
0
    void CommandLine::ParseFromString(const std::wstring& command_line)
    {
        TrimWhitespace(command_line, TRIM_ALL, &command_line_string_);

        if(command_line_string_.empty())
        {
            return;
        }

        int num_args = 0;
        wchar_t** args = NULL;

        args = CommandLineToArgvW(command_line_string_.c_str(), &num_args);

        // 去掉第一个参数两端空白并填充到_program
        TrimWhitespace(args[0], TRIM_ALL, &program_);

        bool parse_switches = true;
        for(int i=1; i<num_args; ++i)
        {
            std::wstring arg;
            TrimWhitespace(args[i], TRIM_ALL, &arg);

            if(!parse_switches)
            {
                args_.push_back(arg);
                continue;
            }

            if(arg == kSwitchTerminator)
            {
                parse_switches = false;
                continue;
            }

            std::string switch_string;
            std::wstring switch_value;
            if(IsSwitch(arg, &switch_string, &switch_value))
            {
                switches_[switch_string] = switch_value;
            }
            else
            {
                args_.push_back(arg);
            }
        }

        if(args)
        {
            LocalFree(args);
        }
    }
Пример #3
0
// Get only the tags from the PSF
TagList GetTagsFromPSF(PseudoReadFile &file, uint8_t versionByte)
{
	// Check to make sure the file is valid
	CheckForValidPSF(file, versionByte);

	TagList tags;

	// Get the starting offset of the tags
	char TagHeader[] = "[TAG]";
	auto TagHeaderVector = std::vector<uint8_t>(TagHeader, TagHeader + 5);
	int32_t TagOffset = file.GetNextOffset(0, TagHeaderVector);

	// Only continue on if we have tags
	if (TagOffset != -1)
	{
		file.pos = TagOffset + 5;
		std::string name, value;
		bool onName = true;
		size_t lengthOfTags = file.data->size() - file.pos;
		for (size_t x = 0; x < lengthOfTags; ++x)
		{
			char curr = file.ReadLE<uint8_t>();
			if (curr == 0x0A)
			{
				if (!name.empty() && !value.empty())
				{
					name = TrimWhitespace(name);
					value = TrimWhitespace(value);
					if (tags.Exists(name))
						tags[name] += "\n" + value;
					else
						tags[name] = value;
				}
				name = value = "";
				onName = true;
				continue;
			}
			if (curr == '=')
			{
				onName = false;
				continue;
			}
			if (onName)
				name += curr;
			else
				value += curr;
		}
	}

	return tags;
}
Пример #4
0
static int AddSearchReplacePair( char *Search, char *Replace )
{
	if ( Search == NULL || Replace == NULL )
		return -999;

	Search = TrimWhitespace(Search);
	Replace = TrimWhitespace(Replace);
	DL_TrimQuotesInPlace( Search );
	DL_TrimQuotesInPlace( Replace );
	
	printf( "Adding Search and Replace Pair: Search is \"%s\"\n", Search );

	return  XTL_SubVars_Add( Search, Replace, TRUE );
}
Пример #5
0
 static void SplitStringT(const STR& str,
     const typename STR::value_type s,
     bool trim_whitespace,
     std::vector<STR>* r)
 {
     size_t last = 0;
     size_t i;
     size_t c = str.size();
     for(i=0; i<=c; ++i)
     {
         if(i==c || str[i]==s)
         {
             size_t len = i - last;
             STR tmp = str.substr(last, len);
             if(trim_whitespace)
             {
                 STR t_tmp;
                 TrimWhitespace(tmp, TRIM_ALL, &t_tmp);
                 r->push_back(t_tmp);
             }
             else
             {
                 r->push_back(tmp);
             }
             last = i + 1;
         }
     }
 }
Пример #6
0
static BOOL DebugInterpreter(THREADID tid, CONTEXT *ctxt, const string &cmd, string *result, VOID *)
{
    TINFO_MAP::iterator it = ThreadInfos.find(tid);
    if (it == ThreadInfos.end())
        return FALSE;
    TINFO *tinfo = it->second;

    std::string line = TrimWhitespace(cmd);
    *result = "";

    if (line == "help")
    {
        result->append("mappings             -- Mappings.\n");
        return TRUE;
    }
    else if(line == "mappings")
    {
      tinfo->_os.str("");
      tinfo->_os << "{"; //open JSON
      for( IMG img= APP_ImgHead(); IMG_Valid(img); img = IMG_Next(img) )
      {
        const string& name = LEVEL_PINCLIENT::IMG_Name(img);
        tinfo->_os <<"\""<< name << "\":{"; //open img
        ADDRINT address = LEVEL_PINCLIENT::IMG_LowAddress(img);
        tinfo->_os << "\"start\":" << address << ",";
        address = LEVEL_PINCLIENT::IMG_HighAddress(img);
        tinfo->_os << "\"end\":" << address << ",";
        tinfo->_os << "\"sections\":" << "{"; //open sections
        for( SEC sec = IMG_SecHead(img); SEC_Valid(sec); sec = SEC_Next(sec))
        {
          const string& name = LEVEL_PINCLIENT::SEC_Name(sec);
          if(name != "")
          {
            tinfo->_os << "\"" << name <<"\":{"; //open section
            ADDRINT address = LEVEL_PINCLIENT::SEC_Address(sec);
            tinfo->_os << "\"start\":" << address << ",";
            USIZE size = LEVEL_PINCLIENT::SEC_Size(sec);
            if(SEC_Valid(SEC_Next(sec)))
            {
              tinfo->_os << "\"size\":" << size << "},"; //close section
            }else
            {
              tinfo->_os << "\"size\":" << size << "}}"; //close section and sections
            }
          }
        }
        if(IMG_Valid(IMG_Next(img)))
        {
          tinfo->_os << "},"; //close img
        }else
        {
          tinfo->_os << "}}"; //close img and json
        }
      }
      *result = tinfo->_os.str();
      return TRUE;
    }

    return FALSE;   /* Unknown command */
}
Пример #7
0
std::unique_ptr<Item> ParseItem(const std::string& itemText)
{
  // Item is split as key = value.
  auto equalPos = itemText.find('=');

  auto key = TrimWhitespace(itemText.substr(0, equalPos));
  if (key.empty())
    return nullptr; // All items must have a key.

  std::unique_ptr<Item> item(new Item);
  item->key = std::move(key);

  std::string value = TrimWhitespace(itemText.substr(equalPos + 1));
  if (value.size() >= 2 && value.front() == '{' && value.back() == '}')
    // This is a complex value that needs to be further passed.
    item->items = Parse(std::istringstream(value.substr(1, value.size() - 2)));
  else
    item->value = value;

  return item;
}
Пример #8
0
 static void SplitStringUsingSubstrT(const STR& str,
     const STR& s, std::vector<STR>* r)
 {
     typename STR::size_type begin_index = 0;
     while(true)
     {
         const typename STR::size_type end_index = str.find(s, begin_index);
         if(end_index == STR::npos)
         {
             const STR term = str.substr(begin_index);
             STR tmp;
             TrimWhitespace(term, TRIM_ALL, &tmp);
             r->push_back(tmp);
             return;
         }
         const STR term = str.substr(begin_index, end_index-begin_index);
         STR tmp;
         TrimWhitespace(term, TRIM_ALL, &tmp);
         r->push_back(tmp);
         begin_index = end_index + s.size();
     }
 }
Пример #9
0
void CommandLine::ParseFromString(const std::wstring& command_line)
{
    std::wstring command_line_string;
    TrimWhitespace(command_line, TRIM_ALL, &command_line_string);
    if(command_line_string.empty())
    {
        return;
    }

    int num_args = 0;
    wchar_t** args = NULL;
    args = ::CommandLineToArgvW(command_line_string.c_str(), &num_args);

    PLOG_IF(FATAL, !args) << "CommandLineToArgvW failed on command line: " <<
        command_line;
    InitFromArgv(num_args, args);
    LocalFree(args);
}
Пример #10
0
/*=========================================================================*/
SLPDDatabaseEntry* SLPDRegFileReadEntry(FILE* fd, SLPDDatabaseEntry** entry)
/* A really big and nasty function that reads an entry SLPDDatabase entry  */
/* from a file. Don't look at this too hard or you'll be sick              */
/*                                                                         */
/* fd       (IN) file to read from                                         */
/*                                                                         */
/* entry    (OUT) Address of a pointer that will be set to the location of */
/*                a dynamically allocated SLPDDatabase entry.  The entry   */
/*                must be freed                                            */
/*                                                                         */
/* Returns  *entry or null on error.                                       */
/*=========================================================================*/
{
	char*   slider1;
	char*   slider2;
	char    line[4096];

	/* give the out param a value */
	*entry = 0;

	/*----------------------------------------------------------*/
	/* read the next non-white non-comment line from the stream */
	/*----------------------------------------------------------*/
	do
	{
		slider1 = RegFileReadLine(fd,line,4096);
		if(slider1 == 0)
		{
			/* read through the whole file and found no entries */
			return 0;
		}
	}while(*slider1 == 0x0d ||  *slider1 == 0x0a);

	/*---------------------------*/
	/* Allocate a database entry */
	/*---------------------------*/
	*entry = SLPDDatabaseEntryAlloc();
	if(entry == 0)
	{
		SLPFatal("Out of memory!\n");
		return 0;
	}

	/* entries read from the .reg file are always local */
	(*entry)->islocal = 1;

	/*---------------------*/
	/* Parse the url-props */
	/*---------------------*/
	slider2 = strchr(slider1,',');
	if(slider2)
	{
		/* srvurl */
		*slider2 = 0; /* squash comma to null terminate srvurl */
		(*entry)->url = strdup(TrimWhitespace(slider1));
		if((*entry)->url == 0)
		{
			SLPLog("Out of memory reading srvurl from regfile line ->%s",line);
			goto SLPD_ERROR;
		}
		(*entry)->urllen = strlen((*entry)->url);

		/* derive srvtype from srvurl */
		(*entry)->srvtype = strstr(slider1,"://");
		if((*entry)->srvtype == 0)
		{
			SLPLog("Looks like a bad url on regfile line ->%s",line);
			goto SLPD_ERROR;   
		}
		*(*entry)->srvtype = 0;
		(*entry)->srvtype=strdup(TrimWhitespace(slider1));
		(*entry)->srvtypelen = strlen((*entry)->srvtype);
		slider1 = slider2 + 1;

		/*lang*/
		slider2 = strchr(slider1,',');
		if(slider2)
		{
			*slider2 = 0; /* squash comma to null terminate lang */
			(*entry)->langtag = strdup(TrimWhitespace(slider1)); 
			if((*entry)->langtag == 0)
			{
				SLPLog("Out of memory reading langtag from regfile line ->%s",line);
				goto SLPD_ERROR;
			}(*entry)->langtaglen = strlen((*entry)->langtag);     
			slider1 = slider2 + 1;                                  
		}
		else
		{
			SLPLog("Expected language tag near regfile line ->%s\n",line);
			goto SLPD_ERROR;
		}

		/* ltime */
		slider2 = strchr(slider1,',');
		if(slider2)
		{
			*slider2 = 0; /* squash comma to null terminate ltime */
			(*entry)->lifetime = atoi(slider1);
			slider1 = slider2 + 1;
		}
		else
		{
			(*entry)->lifetime = atoi(slider1);
			slider1 = slider2;
		}
		if((*entry)->lifetime < 1 || (*entry)->lifetime > 0xffff)
		{
			SLPLog("Invalid lifetime near regfile line ->%s\n",line);
			goto SLPD_ERROR;
		}

		/* get the srvtype if one was not derived by the srvurl*/
		if((*entry)->srvtype == 0)
		{
			(*entry)->srvtype = strdup(TrimWhitespace(slider1));
			if((*entry)->srvtype == 0)
			{
				SLPLog("Out of memory reading srvtype from regfile line ->%s",line);
				goto SLPD_ERROR;
			}
			(*entry)->srvtypelen = strlen((*entry)->srvtype);
			if((*entry)->srvtypelen == 0)
			{
				SLPLog("Expected to derive service-type near regfile line -> %s\n",line);
				goto SLPD_ERROR;
			}
		}
	}
	else
	{
		SLPLog("Expected to find srv-url near regfile line -> %s\n",line);
		goto SLPD_ERROR;
	}

	/*-------------------------------------------------*/
	/* Read all the attributes including the scopelist */
	/*-------------------------------------------------*/
	*line=0;
	while(1)
	{
		if(RegFileReadLine(fd,line,4096) == 0)
		{
			break;
		}
		if(*line == 0x0d || *line == 0x0a)
		{
			break;
		}

		/* Check to see if it is the scopes line */
		/* FIXME We can collapse the scope stuff into the value getting and 
		 * just make it a special case (do strcmp on the tag as opposed to the 
		 * line) of attribute getting. 
		 */
		if(strncasecmp(line,"scopes",6) == 0)
		{
			/* found scopes line */
			slider1 = line;
			slider2 = strchr(slider1,'=');
			if(slider2)
			{
				slider2++;
				if(*slider2)
				{
					/* just in case some idiot puts multiple scopes lines */
					if((*entry)->scopelist)
					{
						SLPLog("scopes already defined previous to regfile line ->%s",line);
						goto SLPD_ERROR;
					}

					(*entry)->scopelist=strdup(TrimWhitespace(slider2));
					if((*entry)->scopelist == 0)
					{
						SLPLog("Out of memory adding scopes from regfile line ->%s",line);
						goto SLPD_ERROR;
					}
					(*entry)->scopelistlen = strlen((*entry)->scopelist);
				}
			}
		}
		else
		{
#ifdef USE_PREDICATES
			char *tag; /* Will point to the start of the tag. */
			char *val; /* Will point to the start of the value. */
			char *end;
			char *tag_end;

			tag = line;
			/*** Elide ws. ***/
			while(isspace(*tag))
			{
				tag++;
			}
			tag_end = tag;

			/*** Find tag end. ***/
			while(*tag_end && (!isspace(*tag_end)) && (*tag_end != '='))
			{
				tag_end++;
			}
			while(*tag_end && *tag_end != '=')
			{
				tag_end++;
			}
			*tag_end = 0;

			/*** Find value start. ***/
			val = tag_end + 1;
			/*** Elide ws. ***/
			while(isspace(*val))
			{
				val++;
			}

			/*** Elide trailing ws. ***/
			end = val;

			/** Find tag end. **/
			while(*end != 0)
			{
				end++;
			}

			/*** Back up over trailing whitespace. ***/
			end--;
			while(isspace(*end))
			{
				*end = 0; /* Overwrite ws. */
				end--;
			}           

			SLPAttrSet_guess((*entry)->attr, tag, val, SLP_ADD);

#else


			/* line contains an attribute (slow but it works)*/
			/* TODO Fix this so we do not have to realloc memory each time! */
			TrimWhitespace(line); 
			(*entry)->attrlistlen += strlen(line) + 2;

			if((*entry)->attrlist == 0)
			{
				(*entry)->attrlist = malloc((*entry)->attrlistlen + 1);
				*(*entry)->attrlist = 0;
			}
			else
			{
				(*entry)->attrlist = realloc((*entry)->attrlist,
											 (*entry)->attrlistlen + 2);
				strcat((*entry)->attrlist,",");
			}

			if((*entry)->attrlist == 0)
			{
				SLPLog("Out of memory adding DEFAULT scope\n");
				goto SLPD_ERROR;
			}

			strcat((*entry)->attrlist,"(");
			strcat((*entry)->attrlist,line);
			strcat((*entry)->attrlist,")");

#endif
		}
	}

	/* Set the scope set in properties if not is set */
	if((*entry)->scopelist == 0)
	{
		(*entry)->scopelist=strdup(G_SlpdProperty.useScopes);
		if((*entry)->scopelist == 0)
		{
			SLPLog("Out of memory adding DEFAULT scope\n");
			goto SLPD_ERROR;
		}
		(*entry)->scopelistlen = G_SlpdProperty.useScopesLen;
	}

	return *entry;

	SLPD_ERROR:
	if(*entry)
	{
		SLPDDatabaseEntryFree(*entry);
		*entry = 0;
	}

	return 0;
}
Пример #11
0
//-------------------------------------------------------------------------
// NavigationSyncer constructor using nav file and lev1 file to set up
//-------------------------------------------------------------------------
NavigationSyncer::NavigationSyncer(std::string navfilename, std::string lev1filename)
{
   //Set to NULL here anyway just to be safe - they should all be none-null by the end of this function
   nscans=0;
   time=NULL;
   navfile=NULL;
   hdrsync=0;
   framerate=0;
   NOSYNCINHDR=-999;
   lev1firstscanmaxexpectedsize=30; //30 seconds 

   if(navfilename.compare("NULL")!=0)
   {
      //Create a new Specim nav file object
      navfile=new SpecimFileChooser(navfilename);
      //Read in the nav file to get the time syncs
      navfile->Reader();
   }

   //Need to read in the level1 file hdr info to get nscans
   BinFile bilin(lev1filename);
   //Get the number of scan lines
   nscans=StringToUINT(bilin.FromHeader("lines"));
   DEBUGPRINT("Number of scans: "<<nscans)
   //Get the NavSync timing from the hdr
   try
   {
      hdrsync=StringToUINT(TrimWhitespace(bilin.FromHeader("NavSync Timing",1,"true")))/1000.0; 
   }
   catch(std::string e)
   {
      if(e.compare(0,bilin.MissingHeaderItemError().length(),bilin.MissingHeaderItemError())==0)
      {
         //Set to a value that means "no value in header"
         hdrsync=NOSYNCINHDR;
      }
      else
      {
         throw e;         
      }
   }

   DEBUGPRINT("Sync from header:"<<hdrsync)
   //Get the acquisition date of the data
   acquisitiondate=bilin.FromHeader("acquisition date");
   //Remove the start of the date string 
   acquisitiondate=TrimWhitespace(acquisitiondate.substr(acquisitiondate.find_first_of(':')+1));
   //Get the date format string for leap seconds
   dateformat=bilin.FromHeader("acquisition date");
   size_t startofdateformat=dateformat.find_first_of("DATE(")+5;
   size_t lengthofdateformat=dateformat.find_first_of("):")-startofdateformat;
   dateformat=dateformat.substr(startofdateformat,lengthofdateformat);

   DEBUGPRINT("Date: "<<acquisitiondate)
   //Get the start and stop times from the header to use in case no sync messages 
   //found in the specim nav file
   gpsstarttime=bilin.FromHeader("GPS Start Time");
   gpsstarttime=RemoveAllBut(gpsstarttime,"1234567890.:");
   gpsstarttime=TrimWhitespace(ReplaceAllWith(&gpsstarttime,':',' '));
   gpsstoptime=bilin.FromHeader("GPS Stop Time");
   gpsstoptime=RemoveAllBut(gpsstoptime,"1234567890.:");
   gpsstoptime=TrimWhitespace(ReplaceAllWith(&gpsstoptime,':',' '));
   
   //Get the frame rate from the hdr
   framerate=StringToDouble(bilin.FromHeader("fps"));
   DEBUGPRINT("Frame rate from header:"<<framerate)
   if((framerate <= 0)||(framerate>100000))
   {
      throw "Frame rate (fps) in hdr file seems erroneous - will only process for frame rates >0 and <100000.";
   }

   //Get the Processed_crop_start from the header file - this tells us if 
   //nav for the full line or crop of the line is required
   std::string cropstart=bilin.FromHeader("y start");
   if(cropstart.compare("")==0)
   {
      Logger::Warning("No y start found in level 1 header, if data was cropped in previous stages navigation may be wrongly synced.");
      //Set the time offset to 0
      croptimeoffset=0;
   }
   else
   {
      //Convert to a double
      croptimeoffset=StringToDouble(cropstart);
      //Get the number of dropped scans that occurred in the crop prior to y start (if y start = 0 so will this)
      std::string prevdropscans=bilin.FromHeader("dropped scans before y start");
      if(prevdropscans.compare("")==0)
      {
         Logger::Warning("No 'dropped scans before y start' found in level 1 header, if y start is non-zero navigation may be wrongly synced.");
         //Set the time offset to 0
         prevdropscans="0";
      }
      double previousdroppedscans=StringToDouble(prevdropscans);
      //Convert the sum of the frames (cropstart and prevdropscans) to a time offset to add onto start time
      croptimeoffset=(croptimeoffset + previousdroppedscans)/framerate;
      Logger::Log("Using cropped level-1 data - will add a time offset relating to number of lines cropped (y start + dropped scans values in hdr): "+ToString(croptimeoffset));
   }

   //Close the level 1 file
   bilin.Close();

   //Create the scan time array
   time=new double[nscans];

   //Get the number of leap seconds for the data
   LeapSecond leap;
   leapseconds=leap.GetLeapSeconds(acquisitiondate,dateformat);
   DEBUGPRINT("Using leap seconds of:"<<leapseconds);
}
Пример #12
0
char_t Tokenizer::GetDelimiter(TokenRange range) const {
  // Symbols are sorted by their precedence, in decreasing order. While the most
  // common delimiters are underscore, space and dot, we give comma the priority
  // to handle the case where words are separated by ", ". Besides, we'll be
  // trimming whitespace later on.
  static const string_t kDelimiterTable = L",_ .-+;&|~";

  // Trim whitespace so that it doesn't interfere with our frequency analysis.
  // This proves useful for handling some edge cases, and it doesn't seem to
  // have any side effects.
  if (!TrimWhitespace(filename_, range))
    return L' ';

  static std::map<char_t, size_t> frequency;

  if (frequency.empty()) {
    // Initialize frequency map
    for (const auto& character : kDelimiterTable) {
      frequency.insert(std::make_pair(character, 0));
    }
  } else {
    // Reset frequency map
    for (auto& pair : frequency) {
      pair.second = 0;
    }
  }

  // Count all possible delimiters
  for (size_t i = range.offset; i < range.offset + range.size; i++) {
    const char_t character = filename_.at(i);
    if (IsAlphanumericChar(character))
      continue;
    if (frequency.find(character) == frequency.end())
      continue;
    frequency.at(character) += 1;
  }

  char_t delimiter = L'\0';

  for (const auto& pair : frequency) {
    if (pair.second == 0)
      continue;

    // Initialize delimiter at first iteration
    if (delimiter == L'\0') {
      delimiter = pair.first;
      continue;
    }

    int character_distance =
        static_cast<int>(kDelimiterTable.find(pair.first)) -
        static_cast<int>(kDelimiterTable.find(delimiter));
    // If the distance is negative, then the new delimiter has higher priority
    if (character_distance < 0) {
      delimiter = pair.first;
      continue;
    }

    // Even if the new delimiter has lower priority, it may be much more common
    float frequency_ratio = static_cast<float>(pair.second) /
                            static_cast<float>(frequency[delimiter]);
    // The constant value was chosen by trial and error. There should be room
    // for improvement.
    if (frequency_ratio / abs(character_distance) > 0.8f)
      delimiter = pair.first;
  }

  return delimiter;
}
Пример #13
0
    // Helper method for converting from MS CF_HTML to text/html.
    void ClipboardUtil::CFHtmlToHtml(const std::string& cf_html,
        std::string* html,
        std::string* base_url)
    {
        // Obtain base_url if present.
        if(base_url)
        {
            static std::string src_url_str("SourceURL:");
            size_t line_start = cf_html.find(src_url_str);
            if(line_start != std::string::npos)
            {
                size_t src_end = cf_html.find("\n", line_start);
                size_t src_start = line_start + src_url_str.length();
                if(src_end!=std::string::npos && src_start!=std::string::npos)
                {
                    *base_url = cf_html.substr(src_start, src_end-src_start);
                    TrimWhitespace(*base_url, TRIM_ALL, base_url);
                }
            }
        }

        // Find the markup between "<!--StartFragment-->" and "<!--EndFragment-->".
        // If the comments cannot be found, like copying from OpenOffice Writer,
        // we simply fall back to using StartFragment/EndFragment bytecount values
        // to get the markup.
        if(html)
        {
            size_t fragment_start = std::string::npos;
            size_t fragment_end = std::string::npos;

            std::string cf_html_lower = StringToLowerASCII(cf_html);
            size_t markup_start = cf_html_lower.find("<html", 0);
            size_t tag_start = cf_html.find("<!--StartFragment", markup_start);
            if(tag_start == std::string::npos)
            {
                static std::string start_fragment_str("StartFragment:");
                size_t start_fragment_start = cf_html.find(start_fragment_str);
                if(start_fragment_start != std::string::npos)
                {
                    fragment_start = static_cast<size_t>(atoi(cf_html.c_str() +
                        start_fragment_start + start_fragment_str.length()));
                }

                static std::string end_fragment_str("EndFragment:");
                size_t end_fragment_start = cf_html.find(end_fragment_str);
                if(end_fragment_start != std::string::npos)
                {
                    fragment_end = static_cast<size_t>(atoi(cf_html.c_str() +
                        end_fragment_start + end_fragment_str.length()));
                }
            }
            else
            {
                fragment_start = cf_html.find('>', tag_start) + 1;
                size_t tag_end = cf_html.rfind("<!--EndFragment", std::string::npos);
                fragment_end = cf_html.rfind('<', tag_end);
            }
            if(fragment_start!=std::string::npos && fragment_end!=std::string::npos)
            {
                *html = cf_html.substr(fragment_start, fragment_end-fragment_start);
                TrimWhitespace(*html, TRIM_ALL, html);
            }
        }
    }
Пример #14
0
int Parse(FILE *asmFile, Instruction **instructions, SymbolTable **symbolTables)
{
  char buf[128]; // buffer for each line
  char *temp = NULL; // temp for the each instruction
  char *symbol = NULL; // symbol address for C command
  int count=0; // count of instructions
  int i; // array index
  Instruction *currentInstruction = NULL;

  while(!feof(asmFile)) { // hasMoreCommands
    if(fgets(buf,128,asmFile)!=NULL) { // advance
      // remove the comments and white space on each line
      temp = TrimWhitespace(RemoveComments(buf));
      // if *temp is 0, it must be a space line 
      if(*temp != 0) {
        if(*temp == '(') { // Label
          symbol = temp + 1;
		  symbol[strcspn(symbol,")")] = '\0';
          AddEntry(symbol, count, symbolTables); // Add Label to Symbol Table
          continue;
        }

        currentInstruction = *instructions+count;
		memset(currentInstruction, 0, sizeof(Instruction));
        if(*temp == '@') { // A Command or L COMMAND
          strcpy(currentInstruction->symbol, temp+1);
          if(IsDecimalOnly(currentInstruction->symbol)) {
            currentInstruction->commandType = A_COMMAND;
          } else {
            currentInstruction->commandType = L_COMMAND;
          }
        } else { // C Command dest=comp;jump
          currentInstruction->commandType = C_COMMAND;

          symbol = strchr(temp, '='); // check if '=' is existed
          if(symbol != 0) { // if '=' is existed, copy L value to dest, change temp to be the remainder 
            strncpy(currentInstruction->dest, temp, strcspn(temp,"="));  
            temp = symbol+1;
          } else { // else, dest is empty
            strcpy(currentInstruction->dest, "");
		  }

          symbol = strchr(temp, ';'); // check if ';' is existed
          if(symbol != 0) { // if ';' is existed, copy L value to comp, R value to jump
            strncpy(currentInstruction->comp, temp, strcspn(temp,";"));
            strcpy(currentInstruction->jump, symbol+1);
          } else { // since ';' is not existed, there is no jump op, and the remainder is comp op only
            strcpy(currentInstruction->comp, temp);
			strcpy(currentInstruction->jump, "");
		  }
        }
        count++;
      }
    }
  }

  // Add variables to symbol table
  for( i = 0; i < count; i++ ){
    currentInstruction = *instructions + i;
    if( currentInstruction->commandType == L_COMMAND ){
      AddEntry(currentInstruction->symbol, VARIABLE, symbolTables);
    }
  }

  return count;
}
Пример #15
0
/*
 * This call-back implements the extended debugger commands.
 *
 *  tid[in]         Pin thread ID for debugger's "focus" thread.
 *  ctxt[in,out]    Register state for the debugger's "focus" thread.
 *  cmd[in]         Text of the extended command.
 *  result[out]     Text that the debugger prints when the command finishes.
 *
 * Returns: TRUE if we recognize this extended command.
 */
static BOOL DebugInterpreter(THREADID tid, CONTEXT *ctxt, const string &cmd, string *result, VOID *)
{
    TINFO_MAP::iterator it = ThreadInfos.find(tid);
    if (it == ThreadInfos.end())
        return FALSE;
    TINFO *tinfo = it->second;

    std::string line = TrimWhitespace(cmd);
    *result = "";

    if (line == "help")
    {
        result->append("stacktrace on        -- Enable tracing of stack usage.\n");
        result->append("stacktrace off       -- Disable tracing of stack usage.\n");
        result->append("stats                -- Show stack usage for current thread.\n");
        result->append("stackbreak newmax    -- Break when any thread stack reaches new maximum usage.\n");
        result->append("stackbreak <number>  -- Break when any thread stack usage exceeds <number> bytes.\n");
        result->append("stackbreak off       -- Disable stack breakpoints.\n");
        return TRUE;
    }
    else if (line == "stats")
    {
        ADDRINT sp = PIN_GetContextReg(ctxt, REG_STACK_PTR);
        tinfo->_os.str("");
        if (sp <= tinfo->_stackBase)
            tinfo->_os << "Current stack usage: " << std::dec << (tinfo->_stackBase - sp) << " bytes.\n";
        else
            tinfo->_os << "Current stack usage: -" << std::dec << (sp - tinfo->_stackBase) << " bytes.\n";
        tinfo->_os << "Maximum stack usage: " << tinfo->_max << " bytes.\n";
        *result = tinfo->_os.str();
        return TRUE;
    }
    else if (line == "stacktrace on")
    {
        if (!EnableInstrumentation)
        {
            PIN_RemoveInstrumentation();
            EnableInstrumentation = true;
            *result = "Stack tracing enabled.\n";
        }
        return TRUE;
    }
    else if (line == "stacktrace off")
    {
        if (EnableInstrumentation)
        {
            PIN_RemoveInstrumentation();
            EnableInstrumentation = false;
            *result = "Stack tracing disabled.\n";
        }
        return TRUE;
    }
    else if (line == "stackbreak newmax")
    {
        if (!EnableInstrumentation)
        {
            PIN_RemoveInstrumentation();
            EnableInstrumentation = true;
        }
        BreakOnNewMax = true;
        BreakOnSize = 0;
        *result = "Will break when thread reaches new stack usage max.\n";
        return TRUE;
    }
    else if (line == "stackbreak off")
    {
        BreakOnNewMax = false;
        BreakOnSize = 0;
        return TRUE;
    }
    else if (line.find("stackbreak ") == 0)
    {
        std::istringstream is(&line.c_str()[sizeof("stackbreak ")-1]);
        size_t size;
        is >> size;
        if (!is)
        {
            *result = "Please specify a numeric size (in bytes)\n";
            return TRUE;
        }
        if (!EnableInstrumentation)
        {
            PIN_RemoveInstrumentation();
            EnableInstrumentation = true;
        }
        BreakOnNewMax = false;
        BreakOnSize = size;
        tinfo->_os.str("");
        tinfo->_os << "Will break when thread uses more than " << size << " bytes of stack.\n";
        *result = tinfo->_os.str();
        return TRUE;
    }
Пример #16
0
void CommandLine::SetProgram(const FilePath& program)
{
    TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]);
}
Пример #17
0
/*=========================================================================*/
SLPDDatabaseEntry* SLPDRegFileReadEntry(FILE* fd, SLPDDatabaseEntry** entry)
/* A really big and nasty function that reads an entry SLPDDatabase entry  */
/* from a file. Don't look at this too hard or you'll be sick              */
/*                                                                         */
/* fd       (IN) file to read from                                         */
/*                                                                         */
/* entry    (OUT) Address of a pointer that will be set to the location of */
/*                a dynamically allocated SLPDDatabase entry.  The entry   */
/*                must be freed                                            */
/*                                                                         */
/* Returns  *entry or null on error.                                       */
/*=========================================================================*/
{
    char*   slider1;
    char*   slider2;
    char    line[4096];

    /* give the out param a value */
    *entry = 0;

    /*----------------------------------------------------------*/
    /* read the next non-white non-comment line from the stream */
    /*----------------------------------------------------------*/
    do
    {
        slider1 = RegFileReadLine(fd,line,4096);
        if(slider1 == 0)
        {
            /* read through the whole file and found no entries */
            return 0;
        }
    }while(*slider1 == 0x0d ||  *slider1 == 0x0a);

    /*---------------------------*/
    /* Allocate a database entry */
    /*---------------------------*/
    *entry =  (SLPDDatabaseEntry*)malloc(sizeof(SLPDDatabaseEntry));
    if(entry == 0)
    {
        SLPFatal("Out of memory!\n");
        return 0;
    }
    memset(*entry,0,sizeof(SLPDDatabaseEntry));

    /*---------------------*/
    /* Parse the url-props */
    /*---------------------*/
    slider2 = strchr(slider1,',');
    if(slider2) 
    {
        /* srvurl */
        *slider2 = 0; /* squash comma to null terminate srvurl */
        (*entry)->url = strdup(TrimWhitespace(slider1));
        if((*entry)->url == 0)
        {
            SLPLog("Out of memory reading srvurl from regfile line ->%s",line);
            goto SLPDREGFILEREADENTRY_ERROR1;
        }
        (*entry)->urllen = strlen((*entry)->url);
        
        /* derive srvtype from srvurl if srvurl is "service:" scheme URL */
        if(strncasecmp(slider1,"service:",8)==0) 
        {
            (*entry)->srvtype = strstr(slider1,"://");
            if((*entry)->srvtype == 0)
            {
                SLPLog("Looks like a bad url on regfile line ->%s",line);
                goto SLPDREGFILEREADENTRY_ERROR1;   
            }
            *(*entry)->srvtype = 0;
            (*entry)->srvtype=strdup(TrimWhitespace(slider1));
            (*entry)->srvtypelen = strlen((*entry)->srvtype);
        }
        slider1 = slider2 + 1;

        /*lang*/
        slider2 = strchr(slider1,',');
        if(slider2)
        {
            *slider2 = 0; /* squash comma to null terminate lang */
            (*entry)->langtag = strdup(TrimWhitespace(slider1)); 
            if((*entry)->langtag == 0)
            {
                SLPLog("Out of memory reading langtag from regfile line ->%s",line);
                goto SLPDREGFILEREADENTRY_ERROR1;
            }            (*entry)->langtaglen = strlen((*entry)->langtag);     
            slider1 = slider2 + 1;                                  
        }
        else
        {
            SLPLog("Expected language tag near regfile line ->%s\n",line);
            goto SLPDREGFILEREADENTRY_ERROR1;
        }
             
        /* ltime */
        slider2 = strchr(slider1,',');
        if(slider2)                      
        {
            *slider2 = 0; /* squash comma to null terminate ltime */
            (*entry)->lifetime = atoi(slider1);
            slider1 = slider2 + 1;
        }                                  
        else
        {
            (*entry)->lifetime = atoi(slider1);
            slider1 = slider2;
        }
        if((*entry)->lifetime < 1 || (*entry)->lifetime > 0xffff)
        {
            SLPLog("Invalid lifetime near regfile line ->%s\n",line);
            goto SLPDREGFILEREADENTRY_ERROR1;
        }
        
        /* get the srvtype if one was not derived by the srvurl*/
        if((*entry)->srvtype == 0)
        {
            (*entry)->srvtype = strdup(TrimWhitespace(slider1));
            if((*entry)->srvtype == 0)
            {
                SLPLog("Out of memory reading srvtype from regfile line ->%s",line);
                goto SLPDREGFILEREADENTRY_ERROR1;
            }
            (*entry)->srvtypelen = strlen((*entry)->srvtype);
            if((*entry)->srvtypelen == 0)
            {
                SLPLog("Expected to derive service-type near regfile line -> %s\n",line);
                goto SLPDREGFILEREADENTRY_ERROR1;
            }
        }   

    }
    else
    {
        SLPLog("Expected to find srv-url near regfile line -> %s\n",line);
        goto SLPDREGFILEREADENTRY_ERROR1;
    }
    
    /*-------------------------------------------------*/
    /* Read all the attributes including the scopelist */
    /*-------------------------------------------------*/
    *line=0;
    while(1)
    {
        if(RegFileReadLine(fd,line,4096) == 0)
        {
            break;
        }         
        if(*line == 0x0d || *line == 0x0a)
        {
            break;
        }

        /* Check to see if it is the scopes line */
        if(strncasecmp(line,"scopes",6) == 0)
        {
            /* found scopes line */
            slider1 = line;
            slider2 = strchr(slider1,'=');
            if(slider2)
            {
                slider2++;
                if(*slider2)
                {
                    /* just in case some idiot puts multiple scopes lines */
                    if((*entry)->scopelist)
                    {
                        SLPLog("scopes already defined previous to regfile line ->%s",line);
                        goto SLPDREGFILEREADENTRY_ERROR1;
                    }

                    (*entry)->scopelist=strdup(TrimWhitespace(slider2));
                    if((*entry)->scopelist == 0)
                    {
                        SLPLog("Out of memory adding scopes from regfile line ->%s",line);
                        goto SLPDREGFILEREADENTRY_ERROR1;
                    }
                    (*entry)->scopelistlen = strlen((*entry)->scopelist);
                }
            }
        }
        else
        {
            /* line contains an attribute (slow but it works)*/
            /* TODO Fix this so we do not have to realloc memory each time! */
            TrimWhitespace(line); 
            (*entry)->attrlistlen += strlen(line) + 2;
            
            if((*entry)->attrlist == 0)
            {
                (*entry)->attrlist = malloc((*entry)->attrlistlen + 1);
                *(*entry)->attrlist = 0;
            }
            else
            {
                (*entry)->attrlist = realloc((*entry)->attrlist,
                                             (*entry)->attrlistlen + 1);
            }
            
            if((*entry)->attrlist == 0)
            {
                SLPLog("Out of memory adding DEFAULT scope\n");
                goto SLPDREGFILEREADENTRY_ERROR1;
            }

            strcat((*entry)->attrlist,"(");
            strcat((*entry)->attrlist,line);
            strcat((*entry)->attrlist,")");
        }
    }

    /* Set the scope to default if not is set */
    if((*entry)->scopelist == 0)
    {
        (*entry)->scopelist=strdup("DEFAULT");
        if((*entry)->scopelist == 0)
        {
            SLPLog("Out of memory adding DEFAULT scope\n");
            goto SLPDREGFILEREADENTRY_ERROR1;
        }
        (*entry)->scopelistlen = 7;
    }

    return *entry;

    SLPDREGFILEREADENTRY_ERROR1:
    if(*entry)
    {
        if((*entry)->srvtype) free((*entry)->srvtype);
        if((*entry)->url) free((*entry)->url);
        if((*entry)->langtag) free((*entry)->langtag);
        if((*entry)->scopelist) free((*entry)->scopelist);
        if((*entry)->attrlist) free((*entry)->attrlist);
        free(*entry);
        *entry = 0;
    }

    return 0;
}
Пример #18
0
/** Read service registrations from a text file.
 *
 * A really big and nasty function that reads service registrations from
 * from a file. Don't look at this too hard or you'll be sick. This is by
 * far the most horrible code in OpenSLP. Please volunteer to rewrite it!
 *
 * "THANK GOODNESS this function is only called at startup" -- Matt
 *
 * @param[in] fd - The file to read from.
 * @param[out] msg - A message describing the SrvReg in buf.
 * @param[out] buf - The buffer used to hold @p message data.
 *
 * @return Zero on success. A value greater than zero on error. A value
 *    less than zero on EOF.
 *
 * @note Eventually the caller needs to call SLPBufferFree and
 *    SLPMessageFree to free memory.
 */
int SLPDRegFileReadSrvReg(FILE * fd, SLPMessage ** msg, SLPBuffer * buf)
{
   char * slider1;
   char * slider2;
   char line[4096];

   struct sockaddr_storage peer;
   int result = 0;
   size_t bufsize = 0;
   size_t langtaglen = 0;
   char * langtag = 0;
   size_t scopelistlen = 0;
   char * scopelist = 0;
   size_t urllen = 0;
   char * url = 0;
   int lifetime = 0;
   size_t srvtypelen = 0;
   char * srvtype = 0;
   size_t attrlistlen = 0;
   char * attrlist = 0;
   SLPBuffer tmp;

#ifdef ENABLE_SLPv2_SECURITY
   unsigned char * urlauth = 0;
   int urlauthlen = 0;
   unsigned char * attrauth = 0;
   int attrauthlen = 0;
#endif

   /* give the out params an initial NULL value */
   *buf = 0;
   *msg = 0;

   /* read the next non-white non-comment line from the stream */
   do
   {
      slider1 = RegFileReadLine(fd, line, 4096);
      if (slider1 == 0)
         return -1;

   } while (*slider1 == 0x0d ||  *slider1 == 0x0a);

   /* Parse the url-props */
   slider2 = strchr(slider1, ',');
   if (slider2)
   {
      /* srvurl */
      *slider2 = 0; /* squash comma to null terminate srvurl */
      url = xstrdup(TrimWhitespace(slider1));
      if (url == 0)
      {
         result = SLP_ERROR_INTERNAL_ERROR;
         goto CLEANUP;
      }
      urllen = strlen(url);

      /* derive srvtype from srvurl */
      srvtype = strstr(slider1, "://");
      if (srvtype == 0)
      {
         result = SLP_ERROR_INVALID_REGISTRATION;
         goto CLEANUP;
      }
      *srvtype = 0;
      srvtype=xstrdup(TrimWhitespace(slider1));
      if (srvtype == 0)
      {
         result = SLP_ERROR_INTERNAL_ERROR;
         goto CLEANUP;
      }
      srvtypelen = strlen(srvtype);
      slider1 = slider2 + 1;

      /*lang*/
      slider2 = strchr(slider1, ',');
      if (slider2)
      {
         *slider2 = 0; /* squash comma to null terminate lang */
         langtag = xstrdup(TrimWhitespace(slider1));
         if (langtag == 0)
         {
            result = SLP_ERROR_INVALID_REGISTRATION;
            goto CLEANUP;
         }
         langtaglen = strlen(langtag);
         slider1 = slider2 + 1;
      }
      else
      {
         result = SLP_ERROR_INVALID_REGISTRATION;
         goto CLEANUP;
      }

      /* ltime */
      slider2 = strchr(slider1,',');
      if (slider2)
      {
         *slider2 = 0; /* squash comma to null terminate ltime */
         lifetime = atoi(slider1);
         slider1 = slider2 + 1;
      }
      else
      {
         lifetime = atoi(slider1);
         slider1 = slider2;
      }
      if (lifetime < 1 || lifetime > SLP_LIFETIME_MAXIMUM)
      {
         result = SLP_ERROR_INVALID_REGISTRATION;
         goto CLEANUP;
      }

      /* get the srvtype if one was not derived by the srvurl */
      if (srvtype == 0)
      {
         srvtype = xstrdup(TrimWhitespace(slider1));
         if (srvtype == 0)
         {
            result = SLP_ERROR_INTERNAL_ERROR;
            goto CLEANUP;
         }
         srvtypelen = strlen(srvtype);
         if (srvtypelen == 0)
         {
            result = SLP_ERROR_INVALID_REGISTRATION;
            goto CLEANUP;
         }
      }
   }
   else
   {
      result = SLP_ERROR_INVALID_REGISTRATION;
      goto CLEANUP;
   }

   /* read all the attributes including the scopelist */
   *line=0;
   while (1)
   {
      slider1 = RegFileReadLine(fd,line,4096);
      if (slider1 == 0)
      {
         result = -1;
         break;
      }
      if (*slider1 == 0x0d || *slider1 == 0x0a)
         break;

      /* Check to see if it is the scopes line */
      /* FIXME We can collapse the scope stuff into the value getting and
         just make it a special case (do strcmp on the tag as opposed to the
         line) of attribute getting. */
      if (strncasecmp(slider1,"scopes", 6) == 0)
      {
         /* found scopes line */
         slider2 = strchr(slider1,'=');
         if (slider2)
         {
            slider2++;
            if (*slider2)
            {
               /* just in case some idiot puts multiple scopes lines */
               if (scopelist)
               {
                  result = SLP_ERROR_SCOPE_NOT_SUPPORTED;
                  goto CLEANUP;
               }

               /* make sure there are no spaces in the scope list
      NOTE: There's nothing in the spec that indicates that
      scopes can't contain spaces. Commenting out for now. --jmc
               if (strchr(slider2, ' '))
               {
                  result = SLP_ERROR_SCOPE_NOT_SUPPORTED;
                  goto CLEANUP;
               } */

               scopelist = xstrdup(TrimWhitespace(slider2));
               if (scopelist == 0)
               {
                  result = SLP_ERROR_INTERNAL_ERROR;
                  goto CLEANUP;
               }
               scopelistlen = strlen(scopelist);
            }
         }
      }
      else
      {
         /* line contains an attribute (slow but it works)*/
         /* TODO Fix this so we do not have to realloc memory each time! */
         TrimWhitespace(slider1);

         if (attrlist == 0)
         {
            attrlistlen += strlen(slider1) + 2;
            attrlist = xmalloc(attrlistlen + 1);
            if (attrlist == 0)
            {
               result = SLP_ERROR_INTERNAL_ERROR;
               goto CLEANUP;
            }
            *attrlist = 0;
         }
         else
         {
            char * tmp_attrlist;
            attrlistlen += strlen(slider1) + 3;
            if ((tmp_attrlist = xrealloc(attrlist, attrlistlen + 1)) == 0)
            {
               xfree(attrlist);
               result = SLP_ERROR_INTERNAL_ERROR;
               goto CLEANUP;
            }
            attrlist = tmp_attrlist;
            strcat(attrlist, ",");
         }

         if (attrlist == 0)
         {
            result = SLP_ERROR_INTERNAL_ERROR;
            goto CLEANUP;
         }

         /* we need special case for keywords (why do we need these)
            they seem like a waste of code.  Why not just use booleans */
         if (strchr(slider1, '='))
         {
            /* normal attribute (with '=') */
            strcat(attrlist, "(");
            strcat(attrlist, slider1);
            strcat(attrlist, ")");
         }
         else
         {
            /* keyword (no '=') */
            attrlistlen -= 2; /* subtract 2 bytes for no '(' or ')' */
            strcat(attrlist, slider1);
         }
      }
   }

   /* Set the scope set in properties if not is set */
   if (scopelist == 0)
   {
      scopelist = xstrdup(G_SlpdProperty.useScopes);
      if (scopelist == 0)
      {
         result = SLP_ERROR_INTERNAL_ERROR;
         goto CLEANUP;
      }
      scopelistlen = G_SlpdProperty.useScopesLen;
   }

#ifdef ENABLE_SLPv2_SECURITY
   /* generate authentication blocks */
   if (G_SlpdProperty.securityEnabled)
   {
      SLPAuthSignUrl(G_SlpdSpiHandle, 0, 0, urllen, url,
            &urlauthlen, &urlauth);
      SLPAuthSignString(G_SlpdSpiHandle, 0, 0, attrlistlen, attrlist,
            &attrauthlen, &attrauth);
   }
#endif

   /* allocate buffer for the SrvReg Message */
   bufsize = 14 + langtaglen;    /* 14 bytes for header    */
   bufsize += urllen + 6;        /*  1 byte for reserved   */
                                 /*  2 bytes for lifetime  */
                                 /*  2 bytes for urllen    */
                                 /*  1 byte for authcount  */
   bufsize += srvtypelen + 2;    /*  2 bytes for len field */
   bufsize += scopelistlen + 2;  /*  2 bytes for len field */
   bufsize += attrlistlen + 2;   /*  2 bytes for len field */
   bufsize += 1;                 /*  1 byte for authcount  */

#ifdef ENABLE_SLPv2_SECURITY
   bufsize += urlauthlen;
   bufsize += attrauthlen;
#endif

   tmp = *buf = SLPBufferAlloc(bufsize);
   if (tmp == 0)
   {
      result = SLP_ERROR_INTERNAL_ERROR;
      goto CLEANUP;
   }

   /* now build the SrvReg Message */

   /* version */
   *tmp->curpos++ = 2;

   /* function id */
   *tmp->curpos++ = SLP_FUNCT_SRVREG;

   /* length */
   PutUINT24(&tmp->curpos, bufsize);

   /* flags */
   PutUINT16(&tmp->curpos, 0);

   /* ext offset */
   PutUINT24(&tmp->curpos, 0);

   /* xid */
   PutUINT16(&tmp->curpos, 0);

   /* lang tag len */
   PutUINT16(&tmp->curpos, langtaglen);

   /* lang tag */
   memcpy(tmp->curpos, langtag, langtaglen);
   tmp->curpos += langtaglen;

   /* url-entry reserved */
   *tmp->curpos++ = 0;

   /* url-entry lifetime */
   PutUINT16(&tmp->curpos, lifetime);

   /* url-entry urllen */
   PutUINT16(&tmp->curpos, urllen);

   /* url-entry url */
   memcpy(tmp->curpos, url, urllen);
   tmp->curpos += urllen;

   /* url-entry authblock */
#ifdef ENABLE_SLPv2_SECURITY
   if (urlauth)
   {
      /* authcount */
      *tmp->curpos++ = 1;

      /* authblock */
      memcpy(tmp->curpos, urlauth, urlauthlen);
      tmp->curpos += urlauthlen;
   }
   else
#endif
      *tmp->curpos++ = 0;

   /* service type */
   PutUINT16(&tmp->curpos, srvtypelen);
   memcpy(tmp->curpos, srvtype, srvtypelen);
   tmp->curpos += srvtypelen;

   /* scope list */
   PutUINT16(&tmp->curpos, scopelistlen);
   memcpy(tmp->curpos, scopelist, scopelistlen);
   tmp->curpos += scopelistlen;

   /* attr list */
   PutUINT16(&tmp->curpos, attrlistlen);
   memcpy(tmp->curpos, attrlist, attrlistlen);
   tmp->curpos += attrlistlen;

   /* attribute auth block */
#ifdef ENABLE_SLPv2_SECURITY
   if (attrauth)
   {
      /* authcount */
      *tmp->curpos++ = 1;

      /* authblock */
      memcpy(tmp->curpos, attrauth, attrauthlen);
      tmp->curpos += attrauthlen;
   }
   else
#endif
      *tmp->curpos++ = 0;

   /* okay, now comes the really stupid (and lazy part) */
   *msg = SLPMessageAlloc();
   if (*msg == 0)
   {
      SLPBufferFree(*buf);
      *buf = 0;
      result = SLP_ERROR_INTERNAL_ERROR;
      goto CLEANUP;
   }

   /* this should be ok even if we are not supporting IPv4,
    * since it's a static service
    */
   memset(&peer, 0, sizeof(struct sockaddr_in));
   peer.ss_family = AF_UNSPEC;
   ((struct sockaddr_in *)&peer)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
   result = SLPMessageParseBuffer(&peer, &peer, *buf, *msg);
   (*msg)->body.srvreg.source = SLP_REG_SOURCE_STATIC;

CLEANUP:

   /* check for errors and free memory */
   switch(result)
   {
      case SLP_ERROR_INTERNAL_ERROR:
         SLPDLog("\nERROR: Out of memory one reg file line:\n   %s\n", line);
         break;

      case SLP_ERROR_INVALID_REGISTRATION:
         SLPDLog("\nERROR: Invalid reg file format near:\n   %s\n", line);
         break;

      case SLP_ERROR_SCOPE_NOT_SUPPORTED:
         SLPDLog("\nERROR: Duplicate scopes or scope list with "
               "embedded spaces near:\n   %s\n", line);
         break;

      default:
         break;
   }

   xfree(langtag);
   xfree(scopelist);
   xfree(url);
   xfree(srvtype);
   xfree(attrlist);

#ifdef ENABLE_SLPv2_SECURITY
   xfree(urlauth);
   xfree(attrauth);
#endif

   return result;
}
Пример #19
0
int
LoadSAM(bool bPE2,			// false if loading PE1, true if loading PE2
		char *pszSAMFile)	// load alignments from this SAM file
{
etClassifyFileType FileType;
FILE *pSAMStream;
int NumParsedElLines;
int NumAcceptedEls;
char szLine[16000];				// buffer input lines
char *pTxt;
char szDescriptor[128];			// parsed out descriptor
int Flags;						// parsed out flags
char szChrom[128];				// parsed out chrom
int StartLoci;					// start loci
int NumUnmappedEls;
int ScaffoldID;

	// open SAM for reading
if(pszSAMFile == NULL || *pszSAMFile == '\0')
	return(eBSFerrParams);

gDiagnostics.DiagOut(eDLInfo,gszProcName,"Loading alignments for %s from: '%s'",bPE2 ? "PE2" : "PE1", pszSAMFile);

FileType = CUtility::ClassifyFileType(pszSAMFile);
if(FileType != eCFTSAM)
	{
	gDiagnostics.DiagOut(eDLFatal,gszProcName,"Unable to classify file as SAM formated: '%s'",pszSAMFile);
	return(eBSFerrOpnFile);
	}

if((pSAMStream = fopen(pszSAMFile,"r"))==NULL)
	{
	gDiagnostics.DiagOut(eDLFatal,gszProcName,"ParseSAMFileElements: Unable to fopen SAM format file %s error: %s",pszSAMFile,strerror(errno));
	return(eBSFerrOpnFile);
	}

NumParsedElLines = 0;
NumAcceptedEls = 0;
NumUnmappedEls = 0;
while(fgets(szLine,sizeof(szLine)-1,pSAMStream)!= NULL)
	{
	NumParsedElLines += 1;
	if(!(NumParsedElLines % 1000000) || NumParsedElLines == 1)
		gDiagnostics.DiagOut(eDLInfo,gszProcName,"Parsed %d SAM lines",NumParsedElLines);

	szLine[sizeof(szLine)-1] = '\0';
	pTxt = TrimWhitespace(szLine);
	if(*pTxt=='\0' || *pTxt=='@')	// simply slough lines which were just whitespace or start with '@'
		continue;
	
	// expecting to parse as "%s\t%d\t%s\t%d\t%d\t%s\t%s\t%d\t%d\t", szDescriptor, Flags, m_szSAMTargChromName, StartLoci+1,MAPQ,szCigar,pszRNext,PNext,TLen);
	// interest is in the descriptor,chromname,flags
	sscanf(szLine,"%s\t%d\t%s\t%d\t",szDescriptor, &Flags, szChrom, &StartLoci);
		// check if element has been mapped, if not then slough ...
	if(Flags & 0x04)	// will be set if unmapped
		{
		NumUnmappedEls += 1;
	    continue;
		}

	if((ScaffoldID = AddScaffold(bPE2,szDescriptor,szChrom,Flags & 0x10 ? '+' : '-')) < 1)
		return(ScaffoldID);
	NumAcceptedEls += 1;
	}

gDiagnostics.DiagOut(eDLInfo,gszProcName,"Loading alignments (%d) for %s from: '%s' completed",NumAcceptedEls,bPE2 ? "PE2" : "PE1", pszSAMFile);
return(NumAcceptedEls);
}
Пример #20
0
	void BaseRunTimeConfig::initializeFromBuffer( const string& cfgStr )
	{ TRACER_OP_START("BaseRunTimeConfig::initializeFromBuffer"); TRACER(cfgStr, READ, HEAP, "Configuration string");
        if (&cfgStr != &this->cfgStr)
            this->cfgStr = cfgStr;

        m_warnings.str("");
        RunTimeVariableMap newVars;
        int lineNum = 0;

        try
        {
            getVariables();

            istringstream cfgStream(cfgStr);

            string line;
		    while (getline(cfgStream, line))
            {
                ++lineNum;
                bal::trim(line); // trim whitespace

                // skip blank or comment lines
                if (line.empty() || line.find_first_of("#[") == 0)
                    continue;

                // otherwise, the line must be in the form "Key=Value" and Key must be in the variables map
                if (!bal::contains(line, "="))
                {
                    m_warnings << "Line " << lineNum << ": line does not define a parameter in the \"Parameter = Value\" format.\n";
                    continue;
                }

                size_t predIdx = line.find_first_of('=') + 1;
				TRACER_OP_START("get value for key"); TRACER_BI;
                string key = line.substr(0, predIdx-1); TRACER(key, WRITE, STACK, "Parameter name");
                bal::trim(key);

                if (m_variables.count(key) == 0)
                {
                    m_warnings << "Line " << lineNum << ": \"" << key << "\" is not a supported parameter.\n";
                    continue;
                }

                RunTimeVariableMap::iterator itr = newVars.find(key);
                if (itr != newVars.end())
                {
                    m_warnings << "Line " << lineNum << ": \"" << key << "\" has already been defined.\n";
                    continue;
                }
				
                size_t valBegin = line.find_first_not_of("\t ", predIdx);
                size_t valEnd = valBegin;
                bool inQuote = false;
                for (valEnd = valBegin; valEnd < line.size(); ++valEnd)
                {
                    if (line[valEnd] == '"' && line[valEnd-1] != '\\')
                        inQuote = !inQuote;
                    else if ((line[valEnd] == '#') && !inQuote)
                        break; // stop at unquoted comment token
                }
                
                if (valEnd == valBegin || valBegin == string::npos)
                {
                    m_warnings << "Line " << lineNum << ": no value set for \"" << key << "\"; did you mean to use an empty string (\"\")?\n";
                    continue;
                }

                string& value = newVars[key];
                value = TrimWhitespace(line.substr(valBegin, valEnd-valBegin));
                if (value.empty())
                {
                    m_warnings << "Line " << lineNum << ": no value set for \"" << key << "\"; did you mean to use an empty string (\"\")?\n";
                    continue;
                }

                value = UnquoteString(value);
                bal::replace_all(value, "\\\"", "\"");
                bal::replace_all(value, "true", "1");
                bal::replace_all(value, "false", "0"); TRACER(value, WRITE, HEAP, std::string("Value for ")+std::string(key)); TRACER_BO; TRACER_OP_END("get value for key");
            }
        }
        catch (exception& e)
        {
            m_warnings << "Line " << lineNum << ": " << e.what() << "\n";
        }

        // apply the new variable values
        setVariables(newVars); TRACER_OP_END("BaseRunTimeConfig::initializeFromBuffer");
	}
Пример #21
0
//-------------------------------------------------------------------------
// Function to read in the information from the header file and store in map
// Keys are now converted to lowercase , Values are not converted
//-------------------------------------------------------------------------
int BinaryReader::ReadHeader()
{
   //DEBUG statement
   DEBUGPRINT("Reading BIL Header file...");
   //Open the header file
   //It is expected to be the same as filename(-'raw' + 'hdr')
   //std::string hdrfile=this->filename;
   //hdrfile.replace(this->filename.length() - 3, 3, "hdr");
   std::string hdrfile;
   //Now try and open the file
   std::ifstream hdrin;
   int failcount=0,maxtypes=2;

   //Loop through the types of header file name to try and open until one works
   for(short int type=0;type<maxtypes;type++)
   {
      this->hdrfilename=GetHeaderFileName(type);
      hdrin.open(this->hdrfilename.c_str());
      if(!hdrin.is_open())
      {
         failcount++;
         //An error has occured
         //brinfo<<"An error has occurred opening the hdr file: "<<this->hdrfilename<<std::endl;
         continue;
         //return -1;
      }
      else
         break;
   }

   if(failcount==maxtypes)
   {
      //An error has occured
      brinfo<<"Unable to open a hdr file, does one exist?"<<std::endl;
      return -1;
   }
   else
   {
      //the hdr file has been opened successfully
      //Now search for the number of lines/samples/bands first
      std::string strbuffer;//buffer to hold lines from hdr file
      std::string strkey; //string for storing map key temporarally
      std::string strval; //string for storing map value temporarly
      bool multilineval=false; // bool to test if values are over multiple file lines
      while(!hdrin.eof())
      {
         //get a line from the hdr file
         std::getline(hdrin,strbuffer);
         //std::cout<<strbuffer<<std::endl;
         //search for an equals
         if(strbuffer.find('=') != std::string::npos)
         {
            //An = has been found in this string
            if(strbuffer.find('{') != std::string::npos)
            {
               //A { has been found. This could mean that the value is on more than one line
               strkey=strbuffer.substr(0,strbuffer.find('=')); //can set key
               strval.assign(strbuffer.substr(strbuffer.find('=')+1)); //this is part of value
               //trim whitespace
               strkey=TrimWhitespace(strkey);
               strval=TrimWhitespace(strval);

               if(strbuffer.find('}')!=std::string::npos) // } also found on same line
               {
                  //The value is only on one line
                  this->Header[ToLowerCase(strkey)]=strval;
                  strkey.clear();
                  strval.clear();
                  continue;

               }
               else
                  multilineval=true;// value is spread over multiple lines of header file
            }
            else
            {
               //The value is only on this line
               //Split up string at equals
               strkey=strbuffer.substr(0,strbuffer.find('='));
               strval=strbuffer.substr(strbuffer.find('=')+1);
               //trim whitespace
               strkey=TrimWhitespace(strkey);
               strval=TrimWhitespace(strval);
               this->Header[ToLowerCase(strkey)]=strval;
               strkey.clear();
               strval.clear();//empty the string
               continue;
            }
         }
         else if(strbuffer.find('}') != std::string::npos)
         {
            //there is a }. This could mean that the end of a value has been reached
            //Check if found at the end of the line (whitespace removed)
            strbuffer=TrimWhitespace(strbuffer);
            if(strbuffer.find('}')==strbuffer.length()-1)
            {
               //At the end - get everything upto there as values - comma separates values
               strbuffer=ReplaceAllWith(&strbuffer,',',';');
               strval=strval+";"+strbuffer; // add the string to the value string using ; as delimiter
               this->Header[ToLowerCase(strkey)]=strval; //add to the header map
               multilineval=false; //reset the bool
               strval.clear();
            }
            else
            {
               //Not at the end - this is odd
               throw "Error. Problem with hdr file - contains data after '}' value: "+strbuffer;
            }

         }
         if((strbuffer.find('=') == std::string::npos) && (strbuffer.find('}') == std::string::npos)) // no = and no }
         {
            strbuffer=TrimWhitespace(strbuffer);
            //No = in this string
            if(multilineval==false)//For the moment will just pass this as the key AND value into the map
            {
               this->Header[strbuffer]=strbuffer;
            }
            else if(!strbuffer.empty())
            {
               // 14/02/2011 TrimPunctuation and then remove commas and replace with ';' - this could be dangerous
               strbuffer=TrimPunctuation(strbuffer);
               strbuffer=ReplaceAllWith(&strbuffer,',',';');
               strval=strval+";"+strbuffer; // add the string to the value string using ; as delimiter
            }
         }
      }

      //close the file
      hdrin.close();
      hdrin.clear();
   }
   return 1;
}