Exemple #1
0
/* OPEN
 * ------------------------------------------------------------------------- */
struct gstor_open_ret
gstor_open(const char * name) {
   struct gstor_open_ret ret;

   string_t petri_filename = string_concat(name,".petri",0);
   if (INVALID_STR(petri_filename)) goto error;
   string_t dfile_filename = string_concat(name,".dfile",0);
   if (INVALID_STR(dfile_filename)) goto error;

   ret.gstor_h = malloc(sizeof(gstor));
   ret.gstor_h->index = petri_open(string_tochar(petri_filename));
   if (ret.gstor_h->index==0) goto error;
   ret.gstor_h->data  = dfile_open(string_tochar(dfile_filename));
   if (ret.gstor_h->data==0) goto close_and_error;

   string_destroy(petri_filename);
   string_destroy(dfile_filename);

   return ret.result = GSTOR_OKAY, ret;

close_and_error:
   petri_close(ret.gstor_h->index);
error:
   return ret.result = GSTOR_ERROR, ret;
}
Exemple #2
0
int main(int argc, char** argv)
{
    struct string args;
    int i;
    int j;
    int k;

    string_init(&args, 1, 1);
    string_concatb(&args, "foo", 3);
    string_concatb(&args, "b", 1);
    printf("%d\n", args._u._s.length);
    for (i = 0; i < argc; i++) string_concat(&args, *argv++);
    puts(string_get(&args));
    string_free(&args);

    for (j = 1; j <= 3; j++) {
        for (k = 1; k <= 200; k+=50) {
            printf("Testing with isize=%d growby=%d\n", j, k);
            string_init(&args, j, k);
            for (i = 0; i < 100; i++) {
                string_concat(&args, "test");
            }
            printf("100x test == %d len, %d size, %d grow\n", args._u._s.length,
                   args._u._s.size, args._u._s.growby);
            string_free(&args);
        }
    }

    return 0;
}
char *get_path(char **env , char *cmd)
{
  char **path;
  char *dir_path;
  int i;
  char **unique_path;
  char *concat_path;
  char *cmd_path;
  for (i = 0; env[i]!= '\0'; i++) /* Loops through each string in env ie each environment variable */
  {
    if(str_ncomp(env[i], "PATH=", str_len("PATH=")) == 0) /* Looks for a string containing "PATH=" */
      {
        path = string_split(env[i],'='); /* Separates the string found into 2 strings; "PATH" and what comes after */
        dir_path = path[1]; /* The second string ie the whole path is stored */
        unique_path = string_split(dir_path, ':'); /* The whole path contained in env var PATH is divided into all possible paths to search for input command */
        break; /* Gets the unique_paths */
      }
  }
  for(i = 0; unique_path[i] != '\0'; i++)
   {
     concat_path = string_concat(unique_path[i],"/"); /* "/" is appended to each unique path */
     cmd_path = string_concat(concat_path, cmd); /* Input command is appended to each unique path */
     if(find_ex(cmd_path)) /* If path to program to be executed is found */
        return cmd_path;
        cmd_path = '\0';
   }
   return NULL; /* If no path found */
}
Exemple #4
0
void cmd_insert (char *cp)

{
    Line *line;
    String *string;

    if (range_single (cp, &cp, &cur_position) < 0) return;		/* see where to insert */
    if (*cp == ';')  							/* if ;, insert the remaining string before current line */
    {
        cur_position.offset = 0;
        string = string_create (strlen (cp + 1), cp + 1);
        string_concat (string, 1, "\n");
        buffer_dirty (cur_position.buffer, 1);
        line_insert (cur_position.buffer, cur_position.line, string);
        return;
    }
    if (!eoltest (cp)) return;						/* otherwise, that's all there should be */

    /* Read tty input until eof into the current buffer just before the current line */

    cur_position.offset = 0;
    while ((string = jnl_readprompt ("\r\n               >")) != NULL)
    {
        string_concat (string, 1, "\n");					/* put line terminator on string */
        buffer_dirty (cur_position.buffer, 1);
        line_insert (cur_position.buffer, cur_position.line, string);	/* insert line just before current line */
    }
}
Exemple #5
0
void StringData::append(const char *s, int len) {
  if (len == 0) return;

  if (len < 0 || (len & IsMask)) {
    throw InvalidArgumentException("len: %d", len);
  }

  ASSERT(!isStatic()); // never mess around with static strings!

  if (!isMalloced()) {
    int newlen;
    m_data = string_concat(data(), size(), s, len, newlen);
    if (isShared()) {
      m_shared->decRef();
    }
    m_len = newlen;
    m_hash = 0;
  } else if (m_data == s) {
    int newlen;
    char *newdata = string_concat(data(), size(), s, len, newlen);
    releaseData();
    m_data = newdata;
    m_len = newlen;
  } else {
    int dataLen = size();
    ASSERT((m_data > s && m_data - s > len) ||
           (m_data < s && s - m_data > dataLen)); // no overlapping
    m_len = len + dataLen;
    m_data = (const char*)realloc((void*)m_data, m_len + 1);
    memcpy((void*)(m_data + dataLen), s, len);
    ((char*)m_data)[m_len] = '\0';
    m_hash = 0;
  }
}
Exemple #6
0
static void debugserver_format_command(const char* prefix, const char* command, const char* arguments, int calculate_checksum, char** buffer, uint32_t* size)
{
	char checksum_hash[DEBUGSERVER_CHECKSUM_HASH_LENGTH + 1] = {'#', '0', '0', '\0'};
	char* encoded = NULL;
	uint32_t encoded_length = 0;

	if (arguments) {
		/* arguments must be hex encoded */
		debugserver_encode_string(arguments, &encoded, &encoded_length);
	} else {
		encoded = NULL;
	}

	char* encoded_command = string_concat(command, encoded, NULL);
	encoded_length = strlen(encoded_command);

	if (calculate_checksum) {
		uint32_t checksum = debugserver_get_checksum_for_buffer(encoded_command, encoded_length);
		checksum_hash[1] = DEBUGSERVER_HEX_ENCODE_FIRST_BYTE(checksum);
		checksum_hash[2] = DEBUGSERVER_HEX_ENCODE_SECOND_BYTE(checksum);
	}

	*buffer = string_concat(prefix, encoded_command, checksum_hash, NULL);
	*size = strlen(prefix) + strlen(encoded_command) + DEBUGSERVER_CHECKSUM_HASH_LENGTH;

	debug_info("formatted command: %s size: %d checksum: 0x%s", *buffer, *size, checksum_hash);

	if (encoded_command)
		free(encoded_command);

	if (encoded)
		free(encoded);
}
static void rtmsg_dump(const uint8_t *buf, size_t len)
{
  char str[80];
  size_t i, off = 0;
  int k = 0;

  for(i=0; i<len; i++)
    {
      if(k == 20)
	{
	  printerror(0, NULL, __func__, "%s", str);
	  k = 0;
	  off = 0;
	}

      if(k != 0 && (k % 4) == 0)
	string_concat(str, sizeof(str), &off, " ");
      string_concat(str, sizeof(str), &off, "%02x", buf[i]);
      k++;
    }

  if(k != 0)
    printerror(0, NULL, __func__, "%s", str);
  return;
}
Exemple #8
0
static char *tcp_pos(char *buf, size_t len, scamper_probe_t *probe)
{
  size_t off = 0;
  string_concat(buf, len, &off, "%u", probe->pr_tcp_seq);
  if(probe->pr_tcp_flags & TH_ACK)
    string_concat(buf, len, &off, ":%u", probe->pr_tcp_ack);
  return buf;
}
Exemple #9
0
char *charRecognition_getText(struct charRecognition *charReg,
			      SDL_Surface *surface, char *dic)
{
	char *recognized = "";

	ImageBlockArray imageBlock = charDetection_blocks(surface);
	for (unsigned h = 0; h < imageBlock.size; h++) {
		ImageLineArray imageLine = imageBlock.elements[h].lines;
		for (unsigned i = 0; i < imageLine.size; i++) {
			char *curWord = "";

			for (unsigned j = 0;
			     j < imageLine.elements[i].chars.size; j++) {
				struct ImageChar imageChar =
				    imageLine.elements[i].chars.elements[j];

				if (imageChar.space) {
					if (strcmp(curWord, "") > 0)
						recognized = string_concat(
						    recognized,
						    wordCorrector_correct(
							dic, curWord));

					curWord = "";

					recognized =
					    string_concat(recognized, " ");
					continue;
				}

				SDL_Surface *s = image_scale(
				    image_extractChar(surface, &imageChar), 16,
				    16);

				imageChar.content =
				    charRecognition_getChar(charReg, s);

				curWord = string_concatChar(
				    curWord, tolower(charRecognition_getChar(
						 charReg, s)));
			}

			if (strcmp(curWord, "") > 0)
				recognized = string_concat(
				    recognized,
				    wordCorrector_correct(dic, curWord));

			recognized = string_concat(recognized, "\n");
		}
		recognized = string_concat(recognized, "\n");
	}

	return recognized;
}
Exemple #10
0
static char *tcp_flags(char *buf, size_t len, scamper_probe_t *probe)
{
  uint8_t flags = probe->pr_tcp_flags;
  uint8_t flag;
  size_t off;
  int i;

  buf[0] = '\0';
  if(probe->pr_len != 0)
    flags &= ~(TH_ACK);

  off = 0;
  for(i=0; i<8 && flags != 0; i++)
    {
      flag = 1 << i;

      if((flags & flag) == 0)
	continue;
      flags &= ~flag;

      switch(flag)
	{
	case TH_SYN:
	  string_concat(buf, len, &off, " syn");
	  break;

	case TH_RST:
	  string_concat(buf, len, &off, " rst");
	  break;

	case TH_FIN:
	  string_concat(buf, len, &off, " fin");
	  break;

	case TH_ACK:
	  string_concat(buf, len, &off, " ack");
	  break;

	case TH_PUSH:
	  string_concat(buf, len, &off, " psh");
	  break;

	case TH_URG:
	  string_concat(buf, len, &off, " urg");
	  break;

	case TH_ECE:
	  string_concat(buf, len, &off, " ece");
	  break;

	case TH_CWR:
	  string_concat(buf, len, &off, " cwr");
	  break;
	}
    }

  return buf;
}
Exemple #11
0
void string_concat_string(string* s, char* chars) {
 while(*chars) {
  string_concat(s, *chars);

  chars++;
 }
}
Exemple #12
0
void string_split(string* s, array* a, char c) {
 string *current;
 int i;

 current = (string*)malloc(sizeof(string));

 string_init(current);

 for(i = 0; i < s->used; i++) {
  if(s->data[i] != c)
   string_concat(current, s->data[i]);
  else if(current->used) {
   array_push(a, (int)current);

   current = (string*)malloc(sizeof(string));

   string_init(current);
  }
 }

 if(current->used)
  array_push(a, (int)current);
 else
  free(current);
}
Exemple #13
0
static struct uri *
proxy_uri(struct uri *uri, unsigned char *proxy,
          struct connection_state *error_state)
{
    struct string string;

    if (init_string(&string)
            && string_concat(&string, "proxy://", proxy, "/",
                             (unsigned char *) NULL)
            && add_uri_to_string(&string, uri, URI_BASE)) {
        /* There is no need to use URI_BASE when calling get_uri()
         * because URI_BASE should not add any fragments in the first
         * place. */
        uri = get_uri(string.source, 0);
        /* XXX: Assume the problem is due to @proxy having bad format.
         * This is a lot faster easier than checking the format. */
        if (!uri)
            *error_state = connection_state(S_PROXY_ERROR);
    } else {
        uri = NULL;
        *error_state = connection_state(S_OUT_OF_MEM);
    }

    done_string(&string);
    return uri;
}
int main(void) {

	char* a = "hola ";
	char* b = "mundo";

	printf("%s\n", string_concat(a, b));
	getchar();

	char* concat = (char*) malloc(mallocSize(sizeof(char), strlen(a), strlen(b), -1));
	string_concat_dinamyc(a, b, &concat);

	printf("%s\n", concat);
	getchar();

	char* user = (char*) malloc(mallocSize(sizeof(char), 8, -1));
	char* dominio = (char*) malloc(mallocSize(sizeof(char), 40, -1));
	char* mail = "*****@*****.**";

	mail_split(mail, &user, &dominio);

	printf("el user es %s y el dominio es %s", user, dominio);
	getchar();

	return EXIT_SUCCESS;
}
Exemple #15
0
static int modfile_file( INSTANCE * my, int * params )
{
    char buffer[1025] ;
    int str = string_new( "" ) ;
    file * f ;
    int l;

    f = file_open( string_get( params[0] ), "rb" ) ;
    string_discard( params[0] ) ;

    if ( f )
    {
        while ( !file_eof( f ) )
        {
            l = file_read( f, buffer, sizeof( buffer ) - 1 ) ;
            buffer[l] = '\0' ;
            if ( l )
            {
                string_concat( str, buffer ) ;
                buffer[0] = '\0' ;
            }
            else
                break;
        }
        file_close( f ) ;
    }

    string_use( str ) ;

    return str ;
}
Exemple #16
0
static int out_scan_fbox_dir(s_outbound_callback_data *callback,
		const char *path, s_faddr addr, int flavor)
{
	DIR *dir;
	struct dirent *dirent;
	
	if( (dir = opendir(path)) == NULL )
	{
		logerr("can't open filebox directory \"%s\"", path);
		return -1;
	}
	
	while( (dirent = readdir(dir)) )
	{
		callback->path = string_concat(path, dirent->d_name, NULL);
		if( is_regfile(callback->path) )
		{
			callback->addr = addr;
			callback->type = OUTB_TYPE_FBOX;
			callback->flavor = flavor;
			callback->callback(callback);
		}
		free(callback->path);
		callback->path = NULL;
	}
	
	closedir(dir);
	
	return 0;
}
Exemple #17
0
struct charRecognition *charRecognition_learn(char *rootPath,
						  char chars[], size_t size, size_t variants)
{
	struct charRecognition *charReg =
		malloc(sizeof(struct charRecognition));

	struct NeuralNetwork *myNeuralNetwork =
		neuralNetwork_main(256, HIDDEN_LAYER_COUNT, size);

	unsignedArray2D input = new_unsignedArray2D(size * variants, 256);
	unsignedArray2D output = new_unsignedArray2D(size * variants, size);

	unsigned count = 0;
	for(unsigned i = 0; i < size; i++) {
		for(unsigned j = 0; j < variants; j++) {
			char toAscii[15];
			sprintf(toAscii, "%d", (int)chars[i]);
			char *path = string_concat(rootPath, toAscii);
			path = string_concat(path, "/");
			char filename[5];
			sprintf(filename, "%d", j);
			path = string_concat(path, filename);
			path = string_concat(path, ".bmp");
			SDL_Surface *s = image_scale(
				image_crop(image_load(path)),
				16, 16);

			for (unsigned k = 0; k < 16; k++)
				for (unsigned l = 0; l < 16; l++)
					input.elements[count].elements[k + l * 16] =
						image_getPixelBool(s, k, l);

			for(unsigned k = 0; k < size; k++)
				output.elements[count].elements[k] = ((count / variants) == k);

			count++;
		}
	}

	NeuralNetwork_train(myNeuralNetwork, input, output, 0.05, 0.1,
				0.9);
	charReg->letters = chars;
	charReg->size    = size;
	charReg->network = myNeuralNetwork;

	return charReg;
}
Exemple #18
0
static int out_scan_lbox(s_outbound_callback_data *callback,
		const s_falist *mailfor, const char *path)
{
	DIR *dir;
	struct dirent *dirent;
	const s_falist *alst;
	s_faddr addr;
	char *newpath;
	
	if( (dir = opendir(path)) == NULL )
	{
		logerr("can't open filebox directory \"%s\"", path);
		return -1;
	}
	
	while( (dirent = readdir(dir)) )
	{
		if( out_parse_name_lbox(&addr, dirent->d_name) == 0 )
		{
			if( mailfor )
			{
				/* Scan only this fileboxes */
				for( alst = mailfor;
					alst && ftn_addrcomp(addr, alst->addr);
					alst = alst->next );
				if( alst )
				{
					newpath = string_concat(path, dirent->d_name, "/", NULL);
					(void)out_scan_fbox_dir(callback, newpath, addr, FLAVOR_HOLD);
					free(newpath);					
				}
			}
			else
			{
				/* Scan all fileboxes */
				newpath = string_concat(path, dirent->d_name, "/", NULL);
				(void)out_scan_fbox_dir(callback, newpath, addr, FLAVOR_HOLD);
				free(newpath);
			}
		}
	}
	
	closedir(dir);
	
	return 0;
}
static int do_attach(void)
{
  char buf[256];
  size_t off = 0;

  string_concat(buf, sizeof(buf), &off, "attach");
  if((options & OPT_PRIORITY) != 0)
    string_concat(buf, sizeof(buf), &off, " priority %d", priority);
  string_concat(buf, sizeof(buf), &off, "\n");

  if(write_wrap(scamper_fd, buf, NULL, off) != 0)
    {
      fprintf(stderr, "could not attach to scamper process\n");
      return -1;
    }

  return 0;
}
int main()
{
  char *concat;

  concat = string_concat("Holberton ", "School");
  printf("%s\n", concat);
  free(concat);
  return (0);
}
int main()
{
  char *concat;

  concat = string_concat("my name is ", "steven garcia");
  printf("%s\n", concat);
  free(concat);
  return (0);
}
Exemple #22
0
String String::operator+(litstr str) const {
  if (empty()) return str;

  if (!str || !*str) return *this;

  int len;
  char *ret = string_concat(data(), size(), str, strlen(str), len);
  return NEW(StringData)(ret, len, AttachString);
}
Exemple #23
0
String String::operator+(CStrRef str) const {
  if (empty()) return str;

  if (str.empty()) return *this;

  int len;
  char *ret = string_concat(data(), size(), str.data(), str.size(), len);
  return NEW(StringData)(ret, len, AttachString);
}
Exemple #24
0
static int
epoch2http(struct string* s, time_t time)
{
    char buf[512];
    struct tm* tm;

    tm = gmtime(&time);
    strftime(buf, 512, "%a, %d %b %Y %H:%M:%S GMT", tm);
    return(!string_concat(s, buf));
}
static char *header_tostr() {

  char buf[512], tmp[128];
  size_t off = 0;

  string_concat(buf, sizeof(buf), &off, "%s\n",
      "version;userID;timestamp;src;dst;method;status;ttl;hopaddr;rtt");

  return strdup(buf);
}
Exemple #26
0
void respond_to_request( int sockfd, char *output, size_t output_size ){


    //printf( "Request #%d\n", ++x );

    /*
    char *local_var;
    testme( &local_var );
    printf( "LocalVar %s\n", local_var );


    printf( "Request2 #%d\n", ++x );

    */

    //output = "abcd";
    //output_size = 4;

    //convert the output size message body to a string
    char content_length[100];
    memset( (void*)content_length, 0, 100 );
    snprintf( content_length, 100, "%d", (int)output_size );
    //printf( "buffer_size: %s\n", content_length );

    //create the response header
    char *response_header = NULL;
    char *response = NULL;
    string_concat( &response_header, "HTTP/1.1 200 OK\r\nContent-Length: ", content_length );
    string_concat( &response_header, response_header, "\r\nContent-Type: text/html\r\n\r\n" );

    //write both the response header and the message body to the socket
    string_concat( &response, response_header, output );
    size_t response_length = strlen( response );


    //printf( "Content Length: %d, Response Length: %d\n", output_size, response_length );

    Writen( sockfd, response, response_length );

    free( response_header );
    free( response );

}
Exemple #27
0
/* ChapelIO.chpl:267 */
void _writeIt_18503(StringClass _this_18574, _string s, int32_t _ln, _string _fn) {
    _string T1;
    _string T2;
    _string T3;
    T1 = (_this_18574->s);
    T2 = string_concat(T1, s, _ln, _fn);
    T3 = string_copy(T2, _ln, _fn);
    _this_18574->s = T3;
    return;
}
Exemple #28
0
/* ChapelIO.chpl:383 */
void halt(_string _e0_args, int32_t _ln, _string _fn) {
    _tuple_1__string T1;
    _string T2;
    _string T3;
    T1.x1 = _e0_args;
    T2 = _tuple2string(&(T1), _ln, _fn);
    T3 = string_concat("halt reached - ", T2, _ln, _fn);
    chpl_error(T3, _ln, _fn);
    return;
}
Exemple #29
0
bool unlink_recursive(const char *path, bool include_self)
{
  if (!path) {
    return false;
  }
  
  DIR *dir;
  struct dirent *e;
  dir = opendir(path);
  if (!dir) {
    return false;
  }
  
  char *item = NULL;
  while ((e = readdir(dir)) != NULL) {
    if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) {
      continue;
    }
    
    item = string_concat(path, "/", e->d_name, NULL);
    if (!item) {
      goto fail;
    }
    
    if (e->d_type == DT_REG) {
      if (unlink(item) != 0) {
        goto fail;
      }
    
    } else if (e->d_type == DT_DIR) {
      if (unlink_recursive(item, true) != 0) {
        goto fail;
      }
    }
    
    free(item);
    item = NULL;
  }
  
  closedir(dir);
  
  if (include_self && rmdir(path) != 0) {
    return false;
  }
  
  return true;

fail:
  if (item) {
    free(item);
  }
  closedir(dir);
  return false;
}
static void halt2(c_string s, int64_t _ln, int32_t _fn) {
#line 688 "ChapelIO.chpl"
  c_string_copy call_tmp;
#line 694 "ChapelIO.chpl"
  call_tmp = string_concat("halt reached - ", s, _ln, _fn);
#line 694 "ChapelIO.chpl"
  chpl_error(call_tmp, _ln, _fn);
#line 693 "ChapelIO.chpl"
  return;
#line 693 "ChapelIO.chpl"
}