예제 #1
0
int sendDir(int fSock, const char* fullpath, int fileSize, int fileType)
{
  char strtmp[FILENAME], strformat[50], strdata[RECFRG], fileName[FILENAME];
  int tmp, headLen, packetNo;
  FILE *sf;

  if (getFileName(strtmp, fullpath, sizeof(strtmp)) < 0)
  {
    printf("\nFilename is too long.\n");
    return -1;
  }

  addColon(strtmp, sizeof(strtmp));
  if (utf8)
    u2g(strtmp, sizeof(strtmp),
        fileName, sizeof(fileName));
  else strncpy(fileName, strtmp, sizeof(fileName));

  headLen = (strlen(fileName)+1) + (HL_HEADERSIZE+1) +
    (HL_FILETYPE+1) + (HL_FILESIZE+1) + 2*(HL_1416+1);
  packetNo = (unsigned int)time(NULL); //简化了,属性值并不准确
  snprintf(strformat, sizeof(strformat), "%%0%dx:%%s:%%0%dx:%%0%dx:14=%%0%dx:16=%%0%dx:",
           HL_HEADERSIZE, HL_FILESIZE, HL_FILETYPE, HL_1416-3, HL_1416-3);

  tmp = snprintf(strdata, sizeof(strdata), strformat,
           headLen, fileName, fileSize, fileType, packetNo, packetNo);

  switch (fileType)
  {
  case 1:
    if ((sf = fopen(fullpath, "r")) == NULL)
    {
      printf("file open error.\n");
      return -1;
    }
    if (writen(fSock, strdata, tmp)<0)
      return -1;
    while ((tmp = fread(strdata, 1, RECFRG, sf))>0)
    {
      if (writen(fSock, strdata, tmp)<0)
        return -1;
    }
    fclose(sf);
    break;
  case 2:

    //break;
  case 3:
    if (writen(fSock, strdata, tmp)<0)
      return -1;
    break;
  default:
    break;
  }
  return 0;
}
char *buildPostFields(TaskListItem *item)
{

    int str_length = strlen("{") + 1;
    char *ret_value = malloc(str_length * sizeof (char));
    strcpy(ret_value, "{");
    int added = 0;
    if (item != NULL)
    {

        if (item->title != NULL)
        {
            str_length = addQuotes(ret_value);
            str_length += strlen(TITLE_STRING);
            ret_value = realloc(ret_value, str_length);
            strcat(ret_value, TITLE_STRING);
            addQuotes(ret_value);
            addColon(ret_value);
            str_length = addQuotes(ret_value);
            str_length += strlen(item->title);
            ret_value = realloc(ret_value, str_length);
            strcat(ret_value, item->title);
            str_length = addQuotes(ret_value);
            added++;
        }

        if (item->id != NULL)
        {
            if (added++ > 0)
                str_length = addComma(ret_value);
            str_length = addQuotes(ret_value);
            str_length += strlen(ID_STRING);
            ret_value = realloc(ret_value, str_length);
            strcat(ret_value, ID_STRING);
            str_length = addQuotes(ret_value);
            str_length = addColon(ret_value);
            str_length = addQuotes(ret_value);
            str_length += strlen(item->id);
            ret_value = realloc(ret_value, str_length);
            strcat(ret_value, item->id);
            str_length = addQuotes(ret_value);
        }

        if (item->updated != NULL)
        {
            if (added++ > 0)
                str_length = addComma(ret_value);
            str_length = addQuotes(ret_value);
            str_length += strlen(UPDATED_STRING);
            ret_value = realloc(ret_value, str_length);
            strcat(ret_value, UPDATED_STRING);
            str_length = addQuotes(ret_value);
            str_length = addColon(ret_value);
            str_length = addQuotes(ret_value);
            str_length += strlen(item->updated);
            ret_value = realloc(ret_value, str_length);
            strcat(ret_value, item->updated);
            str_length = addQuotes(ret_value);
        }

        if (item->selfLink != NULL)
        {
            if (added++ > 0)
                str_length = addComma(ret_value);
            str_length = addQuotes(ret_value);
            str_length += strlen(SELFLINK_STRING);
            ret_value = realloc(ret_value, str_length);
            strcat(ret_value, SELFLINK_STRING);
            str_length = addQuotes(ret_value);
            str_length = addColon(ret_value);
            str_length = addQuotes(ret_value);
            str_length += strlen(item->selfLink);
            ret_value = realloc(ret_value, str_length);
            strcat(ret_value, item->selfLink);
            str_length = addQuotes(ret_value);
        }

        if (item->etag != NULL)
        {
            if (added++ > 0)
                str_length = addComma(ret_value);
            str_length = addQuotes(ret_value);
            str_length += strlen(ETAG_STRING);
            ret_value = realloc(ret_value, str_length);
            strcat(ret_value, ETAG_STRING);
            str_length = addQuotes(ret_value);
            str_length = addColon(ret_value);
            str_length = addQuotes(ret_value);
            str_length += strlen(item->etag);
            ret_value = realloc(ret_value, str_length);
            strcat(ret_value, item->etag);
            str_length = addQuotes(ret_value);
        }

        if (item->kind != NULL)
        {
            if (added++ > 0)
                str_length = addComma(ret_value);
            str_length = addQuotes(ret_value);
            str_length += strlen(KIND_STRING);
            ret_value = realloc(ret_value, str_length);
            strcat(ret_value, KIND_STRING);
            str_length = addQuotes(ret_value);
            str_length = addColon(ret_value);
            str_length = addQuotes(ret_value);
            str_length += strlen(item->kind);
            ret_value = realloc(ret_value, str_length);
            strcat(ret_value, item->kind);
            str_length = addQuotes(ret_value);
        }
    }

    appendString(ret_value, "}");
    return ret_value;
}