示例#1
0
int main(void)
{
	string s = String_from_int(524);
	puts(s);
	string str = String_format("Hello, World!\nHere is a string %s and an integer %d\n",
		"BOOM!", 2492);
	printf("%s", str);
	printf("%s", String_format("Hello, World!\n"));
	return 0;
}
示例#2
0
static bool setBranchInHEAD(const char *branchName)
{
	int fd, ch;
	bool returnValue = false;
	String s = String_Create();
	fd = open(SCM_HEAD_FILE, O_CREAT | O_TRUNC | O_WRONLY, SCM_HEAD_FILE_PERMISSION);
	if(-1 == fd)
	{
		LOG_ERROR("fatal: setBranch: open('%s') failed(%d)", SCM_HEAD_FILE, errno);
		goto EXIT;
	}
	/*Write the branch name to 'head' file*/
	String_format(s, "branch: %s", branchName);
	if(write(fd, s_getstr(s), String_strlen(s)) != String_strlen(s))
	{
		LOG_ERROR("write() failed with errno %d", errno);
		close(fd);
		goto EXIT;
	}
	/*write a new line character*/
	ch = '\n';
	ch = write(fd, &ch, 1);
	close(fd);
	returnValue = true;
EXIT:
	String_Delete(s);
	return returnValue;
}
示例#3
0
String Misc_expandMacros(String          string,
                         const char      *macroTemplate,
                         const TextMacro macros[],
                         uint            macroCount
                        )
{
  #define APPEND_CHAR(string,index,ch) \
    do \
    { \
      if ((index) < sizeof(string)-1) \
      { \
        (string)[index] = ch; \
        (index)++; \
      } \
    } \
    while (0)

  #define SKIP_SPACES(string,i) \
    do \
    { \
      while (   ((string)[i] != '\0') \
             && isspace((string)[i]) \
            ) \
      { \
        (i)++; \
      } \
    } \
    while (0)

  bool  macroFlag;
  ulong i;
  uint  j;
  char  name[128];
  char  format[128];

  assert(macroTemplate != NULL);
  assert((macroCount == 0) || (macros != NULL));

  String_clear(string);
  i = 0;
  do
  {
    // add prefix string
    macroFlag = FALSE;
    while ((macroTemplate[i] != '\0') && !macroFlag)
    {
      if (macroTemplate[i] == '%')
      {
        if ((macroTemplate[i+1] == '%'))
        {
          String_appendChar(string,'%');
          i+=2;
        }
        else
        {
          macroFlag = TRUE;
          i++;
        }
      }
      else
      {
        String_appendChar(string,macroTemplate[i]);
        i++;
      }
    }

    if (macroFlag)
    {
      // skip spaces
      SKIP_SPACES(macroTemplate,i);

      // get macro name
      j = 0;
      if (   (macroTemplate[i] != '\0')
          && isalpha(macroTemplate[i])
         )
      {
        APPEND_CHAR(name,j,'%');
        do
        {
          APPEND_CHAR(name,j,macroTemplate[i]);
          i++;
        }
        while (   (macroTemplate[i] != '\0')
               && isalnum(macroTemplate[i])
              );
      }
      name[j] = '\0';

      // get format data (if any)
      j = 0;
      if (macroTemplate[i] == ':')
      {
        // skip ':'
        i++;

        // skip spaces
        SKIP_SPACES(macroTemplate,i);

        // get format string
        APPEND_CHAR(format,j,'%');
        while (   (macroTemplate[i] != '\0')
               && (   isdigit(macroTemplate[i])
                   || (macroTemplate[i] == '-')
                   || (macroTemplate[i] == '.')
                  )
              )
        {
          APPEND_CHAR(format,j,macroTemplate[i]);
          i++;
        }
        while (   (macroTemplate[i] != '\0')
               && (strchr("l",macroTemplate[i]) != NULL)
              )
        {
          APPEND_CHAR(format,j,macroTemplate[i]);
          i++;
        }
        if (   (macroTemplate[i] != '\0')
            && (strchr("duxfsS",macroTemplate[i]) != NULL)
           )
        {
          APPEND_CHAR(format,j,macroTemplate[i]);
          i++;
        }
      }
      format[j] = '\0';

      // find macro
      if (strlen(name) > 0)
      {
        // find macro
        j = 0;
        while (   (j < macroCount)
               && (strcmp(name,macros[j].name) != 0)
              )
        {
          j++;
        }

        if (j < macroCount)
        {
          // get default format if no format given
          if (strlen(format) == 0)
          {
            switch (macros[j].type)
            {
              case TEXT_MACRO_TYPE_INTEGER:
                strcpy(format,"%d");
                break;
              case TEXT_MACRO_TYPE_INTEGER64:
                strcpy(format,"%lld");
                break;
              case TEXT_MACRO_TYPE_DOUBLE:
                strcpy(format,"%lf");
                break;
              case TEXT_MACRO_TYPE_CSTRING:
                strcpy(format,"%s");
                break;
              case TEXT_MACRO_TYPE_STRING:
                strcpy(format,"%S");
                break;
              #ifndef NDEBUG
                default:
                  HALT_INTERNAL_ERROR_UNHANDLED_SWITCH_CASE();
                  break; /* not reached */
              #endif /* NDEBUG */
            }
          }

          // expand macro
          switch (macros[j].type)
          {
            case TEXT_MACRO_TYPE_INTEGER:
              String_format(string,format,macros[j].value.i);
              break;
            case TEXT_MACRO_TYPE_INTEGER64:
              String_format(string,format,macros[j].value.l);
              break;
            case TEXT_MACRO_TYPE_DOUBLE:
              String_format(string,format,macros[j].value.d);
              break;
            case TEXT_MACRO_TYPE_CSTRING:
              String_format(string,format,macros[j].value.s);
              break;
            case TEXT_MACRO_TYPE_STRING:
              String_format(string,format,macros[j].value.string);
              break;
            #ifndef NDEBUG
              default:
                HALT_INTERNAL_ERROR_UNHANDLED_SWITCH_CASE();
                break; /* not reached */
            #endif /* NDEBUG */
          }
        }
        else
        {
          // keep unknown macro
          String_appendCString(string,name);
        }
      }
      else
      {
        // empty macro: add empty string
        String_format(string,format,"");
      }
    }
  }
  while (macroFlag);

  return string;

  #undef SKIP_SPACES
  #undef APPEND_CHAR
}
示例#4
0
bool Commit_WriteCommitFile(Commit c, ShaBuffer commitSha)
{
	uint32_t fd, temp, dummy;
	bool returnValue = false;
	String s = String_Create();

	c->rawtime = time(NULL);
	String_format(s, "%s:%s:%s:%u", c->tree, c->parent0, c->parent1, (uint32_t)c->rawtime);

	sha_buffer((const unsigned char *)s_getstr(s), String_strlen(s), commitSha);
	String_format(s,"%s/%s", SCM_COMMIT_FOLDER, commitSha);
	if(true == isItFile(s_getstr(s)))
	{
		LOG_ERROR("fatal Commit_WriteCommitFile() commit '%s' already exist", commitSha);
		abort();
	}
	fd = open(s_getstr(s), O_CREAT | O_TRUNC | O_WRONLY, SCM_OBJECT_FILE_PERMISSION);
	if(fd < 0)
	{
		LOG_ERROR("Commit_WriteCommitFile() open('%s') failed(%d)", s_getstr(s), errno);
		goto EXIT;
	}

	/*Write the object identification Code..*/
	temp = htonl(OBJECT_COMMIT_FILE);
	dummy = write(fd, &temp, INT_SIZE);

	/*time */
	temp = htonl(c->rawtime);
	dummy = write(fd, &temp, INT_SIZE);

	/*size of tree SHA*/
	temp = htonl(strlen((char*)c->tree)+1);
	dummy = write(fd, &temp, INT_SIZE);
	dummy = write(fd, c->tree, ntohl(temp));

	/*size of parent0*/
	temp = htonl(strlen((char*)c->parent0)+1);
	dummy = write(fd, &temp, INT_SIZE);
	dummy = write(fd, c->parent0, ntohl(temp));

	/*size of parent1*/
	temp = htonl(strlen((char*)c->parent1)+1);
	dummy = write(fd, &temp, INT_SIZE);
	dummy = write(fd, c->parent1, ntohl(temp));

	/*length of author*/
	temp = htonl(String_strlen(c->author)+1);
	dummy = write(fd, &temp, INT_SIZE);
	dummy = write(fd, s_getstr(c->author), ntohl(temp));

	/*length of message*/
	temp = htonl(String_strlen(c->message)+1);
	dummy = write(fd, &temp, INT_SIZE);
	dummy = write(fd, s_getstr(c->message), ntohl(temp));
	returnValue = true;
	close(fd);
EXIT:
	String_Delete(s);
	return returnValue;
}
示例#5
0
bool Commit_SetAuthor(Commit c, const char *authorName, const char *authorEmailId)
{
	String_format(c->author, "%s <%s>", authorName, authorEmailId);
	return true;
}
示例#6
0
bool Commit_ReadCommitFile(Commit c, const ShaBuffer commitSha)
{
	String s = String_Create();
	bool returnValue = false;
	uint32_t fd, len = strlen((char*)commitSha), temp, dummy;
	if(len != SHA_HASH_LENGTH)
	{
		LOG_ERROR("Commit_ReadCommitFile() shaLength %d != %d", len, SHA_HASH_LENGTH);
		goto EXIT;
	}

	String_format(s,"%s/%s", SCM_COMMIT_FOLDER, commitSha);
	if(false == isItFile(s_getstr(s)))
	{
		LOG_ERROR("fatal Commit_ReadCommitFile() commit '%s' doesn't exist", commitSha);
		goto EXIT;
	}
	fd = open(s_getstr(s), O_RDONLY);


	/*read the object identification Code and check it*/
	dummy = read(fd, &temp, INT_SIZE);
	temp = ntohl(temp);
	if(temp != OBJECT_COMMIT_FILE)
	{
		LOG_ERROR("Commit_ReadCommitFile: '%s' not a commit file", commitSha);
		close(fd);
		goto EXIT;
	}

	/*read the time*/
	dummy = read(fd, &temp, INT_SIZE);
	c->rawtime = ntohl(temp);

	/*read the tree Sha*/
	dummy = read(fd, &temp, INT_SIZE);
	temp = ntohl(temp);
	dummy = read(fd, c->tree, temp);

	/*read parent0*/
	dummy = read(fd, &temp, INT_SIZE);
	temp = ntohl(temp);
	dummy = read(fd, c->parent0, temp);

	/*read parent1*/
	dummy = read(fd, &temp, INT_SIZE);
	temp = ntohl(temp);
	dummy = read(fd, c->parent1, temp);

	/*read the author name and EmailId*/
	dummy = read(fd, &temp, INT_SIZE);
	temp = ntohl(temp);
	String_SetSize(c->author,temp+10);
	dummy = read(fd, (void*)s_getstr(c->author), temp);

	/*read the message*/
	dummy = read(fd, &temp, INT_SIZE);
	temp = ntohl(temp);
	String_SetSize(c->message,temp+10);
	dummy = read(fd, (void*)s_getstr(c->message), temp);

	returnValue = true;
	close(fd);
EXIT:
	String_Delete(s);
	return returnValue;
}
示例#7
0
bool Password_input(Password   *password,
                    const char *title,
                    uint       modes
                   )
{
  bool okFlag;

  assert(password != NULL);

  Password_clear(password);

  okFlag = FALSE;

  /* input via SSH_ASKPASS program */
  if (((modes & PASSWORD_INPUT_MODE_GUI) != 0) && !okFlag)
  {
    const char *sshAskPassword;
    String     command;
    FILE       *file;
    bool       eolFlag;
    int        ch;

    sshAskPassword = getenv("SSH_ASKPASS");
    if ((sshAskPassword != NULL) && (strcmp(sshAskPassword,"") != 0))
    {
      /* open pipe to external password program */
      command = String_newCString(sshAskPassword);
      if (title != NULL)
      {
        String_format(command," %\"s:",title);
      }
      file = popen(String_cString(command),"r");
      if (file == NULL)
      {
        String_delete(command);
        return FALSE;
      }
      String_delete(command);

      /* read password, discard last LF */
      printInfo(2,"Wait for password...");
      eolFlag = FALSE;
      do
      {
        ch = getc(file);
        if (ch != EOF)
        {
          switch ((char)ch)
          {
            case '\n':
            case '\r':
              eolFlag = TRUE;
              break;
            default:
              Password_appendChar(password,(char)ch);
              break;
          }
        }
        else
        {
          eolFlag = TRUE;
        }
      }
      while (!eolFlag);
      printInfo(2,"ok\n");

      /* close pipe */
      pclose(file);

      okFlag = TRUE;
    }
  }

  /* input via console */
  if (((modes & PASSWORD_INPUT_MODE_CONSOLE) != 0) && !okFlag)
  {
    int            n;
    struct termios oldTermioSettings;
    struct termios termioSettings;
    bool           eolFlag;
    char           ch;

    if (isatty(STDIN_FILENO) == 1)
    {
      /* read data from interactive input */
      if (title != NULL)
      {
        fprintf(stderr,"%s: ",title);fflush(stderr);
      }

      /* save current console settings */
      if (tcgetattr(STDIN_FILENO,&oldTermioSettings) != 0)
      {
        return FALSE;
      }

      /* disable echo */
      memcpy(&termioSettings,&oldTermioSettings,sizeof(struct termios));
      termioSettings.c_lflag &= ~ECHO;
      if (tcsetattr(STDIN_FILENO,TCSANOW,&termioSettings) != 0)
      {
        return FALSE;
      }

      /* input password */
      eolFlag = FALSE;
      do
      {
        if (read(STDIN_FILENO,&ch,1) == 1)
        {
          switch (ch)
          {
            case '\r':
              break;
            case '\n':
              eolFlag = TRUE;
              break;
            default:
              Password_appendChar(password,ch);
              break;
          }
        }
        else
        {
          eolFlag = TRUE;
        }
      }
      while (!eolFlag);

      /* restore console settings */
      tcsetattr(STDIN_FILENO,TCSANOW,&oldTermioSettings);

      if (title != NULL)
      {
        fprintf(stderr,"\n");
      }
    }
    else
    {
      /* read data from non-interactive input */
      eolFlag = FALSE;
      do
      {
        ioctl(STDIN_FILENO,FIONREAD,(char*)&n);
        if (n > 0)
        {
          if (read(STDIN_FILENO,&ch,1) == 1)
          {
            switch (ch)
            {
              case '\r':
                break;
              case '\n':
                eolFlag = TRUE;
                break;
              default:
                Password_appendChar(password,ch);
                break;
            }
          }
          else
          {
            eolFlag = TRUE;
          }
        }
      }
      while (!eolFlag && (n > 0));
    }

    okFlag = TRUE;
  }

  return okFlag;
}
示例#8
0
int cmd_init(int argc, char *argv[])
{
	int i;
	String s;
	char *folders[] = { SCM_FOLDER,
						SCM_BRANCH_FOLDER,
						SCM_OBJECTS_FOLDER,
						SCM_BLOB_FOLDER,
						SCM_COMMIT_FOLDER,
						SCM_TREE_FOLDER,
						SCM_TEMP_FOLDER};
	const int size = sizeof(folders)/sizeof(folders[0]);

	if(true == isItFolder(".scm"))
	{
		LOG_ERROR("fatal: delete the .scm folder then try again");
		return 1;
	}

	/*create all the folders*/
	for(i = 0; i < size; i++)
	{
		if(0 !=	mkdir(folders[i], SCM_FOLDER_PERMISSION))
		{
			LOG_ERROR("fatal: mkdir('%s') failed(%d)", folders[i], errno);
			return 1;
		}
	}

	s = String_Create();
	String_format(s, "%s/%s", SCM_BRANCH_FOLDER, SCM_DEFAULT_BRANCH);
	/*Create the branch folder*/
	if(0 != mkdir(s_getstr(s), SCM_FOLDER_PERMISSION))
	{
		LOG_ERROR("mkdir('%s') failed(%d)", s_getstr(s), errno);
		String_Delete(s);
		return 1;
	}

	String_format(s, "%s/%s/%s", SCM_BRANCH_FOLDER, SCM_DEFAULT_BRANCH, SCM_BRANCH_CACHE_FOLDER);
	/*Create the branch folder*/
	if(0 != mkdir(s_getstr(s), SCM_FOLDER_PERMISSION))
	{
		LOG_ERROR("mkdir('%s') failed(%d)", s_getstr(s), errno);
		String_Delete(s);
		return 1;
	}
	/*set the branch name in 'HEAD' file*/
	setBranchInHEAD(SCM_DEFAULT_BRANCH);

	String_format(s, "%s/%s/%s", SCM_BRANCH_FOLDER, SCM_DEFAULT_BRANCH, SCM_INDEX_FILENAME);
	/*Create a empty index file*/
	if(false == CreateFirstIndexFile(s_getstr(s)))
	{
		LOG_INFO("CreateEmptyIndexFile() failed()");
	}

	String_format(s, "%s/%s/%s", SCM_BRANCH_FOLDER, SCM_DEFAULT_BRANCH, SCM_COMMIT_FILENAME);
	createEmptyCommitFile(s_getstr(s));
	String_Delete(s);
	return 0;
}