AREXPORT void ArArgumentBuilder::removeArg(size_t which,
																					 bool isRebuildFullString)
{
  size_t i;
  char *temp;

	if (which < 0) {
		ArLog::log(ArLog::Terse, "ArArgumentBuilder::removeArg: cannot remove arg at negative index (%i)",
							 which);
		return;
	}
  if (which > myArgc - 1)
  {
    ArLog::log(ArLog::Terse, "ArArgumentBuilder::removeArg: %d is greater than the number of arguments which is %d", which, myArgc);
    return;
  }


  temp = myArgv[which];
  //delete[] myArgv[which]; 
  for (i = which; i < myArgc - 1; i++)
    myArgv[i] = myArgv[i+1];
  // delete the one off the end
  myArgc -= 1;
  // Just stuffing the deleted argument value at the end of the array (not sure why)
	myArgv[i] = temp;

	// Note: It seems that compressQuoted calls removeArg and depends on it not 
	// changing the full string.  Therefore, the parameter was added so that the
	// desired behavior could be specified by the caller.
  if (isRebuildFullString) {	
	  rebuildFullString();
	}

}
AREXPORT void ArArgumentBuilder::internalAddAsIs(const char *str, int position)
{ 
  size_t k = 0;

  bool addAtEnd;
  if (position < 0 || (size_t)position > myArgc)
    addAtEnd = true;
  else
    addAtEnd = false;


  if (addAtEnd)
  {
    myArgv[myArgc] = new char[strlen(str) + 1];
    strcpy(myArgv[myArgc], str);
    myArgv[myArgc][strlen(str)] = '\0';
    
    // add to our full string
    // if its not our first add a space (or whatever our space char is)
    if (!myFirstAdd && myExtraSpace == '\0')
      myFullString += " ";
    else if (!myFirstAdd)
      myFullString += myExtraSpace;
    
    myFullString += myArgv[myArgc];
    myFirstAdd = false;

    myArgc++;
    myOrigArgc = myArgc;
  }
  else
  {
    // first move things down
    for (k = myArgc + 1; k > (size_t)position; k--)
    {
      myArgv[k] = myArgv[k - 1];
    }
    myArgc++;
    myOrigArgc = myArgc;
    
    myArgv[position] = new char[strlen(str) + 1];
    strcpy(myArgv[position], str);
    myArgv[position][strlen(str)] = '\0';
    
    rebuildFullString();
    myFirstAdd = false;
  }


}
/**
   Internal function that adds a string starting at some given space
   
   @param str the string to add

   @param position the position to add the string at, a position less
   than 0 means to add at the end, if this number is greater than how
   many positions exist then it will also be added at the end
 **/
AREXPORT void ArArgumentBuilder::internalAdd(const char *str, int position)
{
  char buf[10000];
  int i;
  int j;
  size_t k;
  bool findingSpace = true;
  int startNonSpace;
  int len;
  bool addAtEnd;
  //size_t startingArgc = getArgc();

  if (position < 0 || (size_t)position > myArgc)
    addAtEnd = true;
  else
    addAtEnd = false;
  
  strncpy(buf, str, sizeof(buf));
  len = strlen(buf);

  // can do whatever you want with the buf now
  // first we advance to non-space
  for (i = 0; i < len; ++i)
  {
    if (!isspace(buf[i]) || (myExtraSpace != '\0' && buf[i] == myExtraSpace))
      break;
  }
  // see if we're done
  if (i == len)
  {
    if (!myIsQuiet) {
      ArLog::log(ArLog::Verbose, "All white space add for argument builder.");
    }
    return;
  }


  // walk through the line until we get to the end of the buffer...
  // we keep track of if we're looking for white space or non-white...
  // if we're looking for white space when we find it we have finished
  // one argument, so we toss that into argv, reset pointers and moveon
  for (startNonSpace = i; ; ++i)
  {
    // take out the slash of escaped spaces
    if (buf[i] == '\\' && i + 1 < len && buf[i + 1] == ' ')
    {
      for (j = i; j < len && j != '\0'; j++)
      {
	buf[j] = buf[j + 1];
      }
      --len;
    }
    // if we're not finding space and we see a non space (or the end),
    // set us into finding space mode and denote where it started
    else if (!findingSpace && 
	     !(i == len || isspace(buf[i]) || buf[i] == '\0' || 
	       (myExtraSpace != '\0' && buf[i] == myExtraSpace)))	     
    {
      startNonSpace = i;
      findingSpace = true;
    }
    // if we're finding space, and we see it (or the end), we're at
    // the end of one arg, so toss it into the list
    else if (findingSpace && 
	     (i == len || isspace(buf[i]) || buf[i] == '\0' || 
	      (myExtraSpace != '\0' && buf[i] == myExtraSpace)))
    {
      // see if we have room in our argvLen
      if (myArgc + 1 >= myArgvLen)
      {
	ArLog::log(ArLog::Terse, "ArArgumentBuilder::Add: could not add argument since argc (%u) has grown beyond the argv given in the constructor (%u)", myArgc, myArgvLen);
      }
      else
      {
	// if we're adding at the end just put it there, also put it
	// at the end if its too far out
	if (addAtEnd)
	{
	  myArgv[myArgc] = new char[i - startNonSpace + 1];
	  strncpy(myArgv[myArgc], &buf[startNonSpace], i - startNonSpace);
	  myArgv[myArgc][i - startNonSpace] = '\0';
	  // add to our full string
	  // if its not our first add a space (or whatever our space char is)
	  if (!myFirstAdd && myExtraSpace == '\0')
	    myFullString += " ";
	  else if (!myFirstAdd)
	    myFullString += myExtraSpace;
	  
	  myFullString += myArgv[myArgc];
	  myFirstAdd = false;
	  
	  myArgc++;
	  myOrigArgc = myArgc;
	}
        // otherwise stick it where we wanted it if we can or just 
	else
	{
	  // first move things down
	  for (k = myArgc + 1; k > (size_t)position; k--)
	  {
	    myArgv[k] = myArgv[k - 1];
	  }
	  myArgc++;
	  myOrigArgc = myArgc;

	  myArgv[position] = new char[i - startNonSpace + 1];
	  strncpy(myArgv[position], &buf[startNonSpace], i - startNonSpace);
	  myArgv[position][i - startNonSpace] = '\0';
	  position++;

		rebuildFullString();
	  myFirstAdd = false;
	}

      }
      findingSpace = false;
    }
    // if we're at the end or its a null, we're at the end of the buf
    if (i == len || buf[i] == '\0')
      break;
  }
}
/**
   Internal function that adds a string starting at some given space
   
   @param str the string to add

   @param position the position to add the string at, a position less
   than 0 means to add at the end, if this number is greater than how
   many positions exist then it will also be added at the end
 **/
AREXPORT void ArArgumentBuilder::internalAdd(const char *str, int position)
{
  char buf[10000];
  int i = 0;
  int j = 0;
  size_t k = 0;
  int len = 0;
  bool addAtEnd = true;
  //size_t startingArgc = getArgc();

  bool isArgInProgress = false;
  int curArgStartIndex = -1;


  if (position < 0 || (size_t)position > myArgc)
    addAtEnd = true;
  else
    addAtEnd = false;

  strncpy(buf, str, sizeof(buf));
  len = strlen(buf);

  // can do whatever you want with the buf now
  // first we advance to non-space
  for (i = 0; i < len; ++i)
  {
    if (!isSpace(buf[i])) {
       //(!myIgnoreNormalSpaces && !isspace(buf[i])) || 
       // (myExtraSpace != '\0' && buf[i] != myExtraSpace))
      break;
    } // end if non-space found
  } // end for each char in buffer

  // see if we're done
  if (i == len)
  {
    if (!myIsQuiet) {
      ArLog::log(ArLog::Verbose, "All white space add for argument builder.");
    }
    return;
  }

  int endArgFlags = ANY_SPACE;

  // walk through the line until we get to the end of the buffer...
  // we keep track of if we're looking for white space or non-white...
  // if we're looking for white space when we find it we have finished
  // one argument, so we toss that into argv, reset pointers and moveon
  for (curArgStartIndex = i; ; ++i)
  {

    // Remove the slash of escaped spaces.  This is primarily done to handle command 
    // line arguments (especially on Linux). If quotes are "pre-compressed", then
    // the backslash is preserved.  Consecutive backslashes are also preserved 
    // (i.e. "\\ " is not modified).
    if (!myIsPreCompressQuotes &&
        (buf[i] == '\\') && (i + 1 < len) && (buf[i + 1] == ' ') &&
        ((i == 0) || (buf[i - 1] != '\\')))
    {
      for (j = i; j < len && j != '\0'; j++)
      {
        buf[j] = buf[j + 1];
      }
      --len;
    } // end if escaped space
  
    // If we're not in the middle of an argument, then determine whether the 
    // current buffer position marks the start of one.
    else if ((!isArgInProgress) && 
             (i < len) &&
             (buf[i] != '\0') &&
             (isStartArg(buf, len, i, &endArgFlags))) 
    {
      curArgStartIndex = i;
      isArgInProgress = true;
    }
    // If we are in the middle of an argument, then determine whether the current
    // buffer position marks the end of it.  (Note that i may be incremented by
    // isEndArg when quotes are pre-compressed.)
    else if (isArgInProgress && 
             ((i == len) || 
              (buf[i] == '\0') ||
              (isEndArg(buf, len, i, endArgFlags)))) 

    {
      // see if we have room in our argvLen
      if (myArgc + 1 >= myArgvLen)
      {
        ArLog::log(ArLog::Terse, "ArArgumentBuilder::Add: could not add argument since argc (%u) has grown beyond the argv given in the constructor (%u)", myArgc, myArgvLen);
      }
      else // room in arg array
      {
        // if we're adding at the end just put it there, also put it
        // at the end if its too far out
        if (addAtEnd)
        {
          myArgv[myArgc] = new char[i - curArgStartIndex + 1];
          strncpy(myArgv[myArgc], &buf[curArgStartIndex], i - curArgStartIndex);
          myArgv[myArgc][i - curArgStartIndex] = '\0';
          // add to our full string
          // if its not our first add a space (or whatever our space char is)
          if (!myFirstAdd && myExtraSpace == '\0')
            myFullString += " ";
          else if (!myFirstAdd)
            myFullString += myExtraSpace;

          myFullString += myArgv[myArgc];
          myFirstAdd = false;

          myArgc++;
          myOrigArgc = myArgc;
        }
        // otherwise stick it where we wanted it if we can or just 
        else // insert arg at specified position
        {
          // first move things down
          for (k = myArgc + 1; k > (size_t)position; k--)
          {
            myArgv[k] = myArgv[k - 1];
          }
          myArgc++;
          myOrigArgc = myArgc;

          myArgv[position] = new char[i - curArgStartIndex + 1];
          strncpy(myArgv[position], &buf[curArgStartIndex], i - curArgStartIndex);
          myArgv[position][i - curArgStartIndex] = '\0';
          position++;

          rebuildFullString();
          myFirstAdd = false;

        } // end else insert arg at specified position

      } // end else room in arg array

      isArgInProgress = false;
      endArgFlags = ANY_SPACE;

    } // end else if found end of argument

    // if we're at the end or its a null, we're at the end of the buf
    if (i == len || buf[i] == '\0')
      break;

  } // end for each char in buffer (after non-whitespace found)

} // end method internalAdd