Modules-old/cutil/lua_cutil.cpp
/* Copies string data to the torch tensor */
Copies string data to the torch tensor
Copies string data to the torch tensor
static int lua_string2storage(lua_State *L) {
  size_t n_str;
  char * src = (char*)lua_tolstring(L, 1, &n_str);
  if( src==NULL )
    return luaL_error(L, "Bad source string.");
  const char* s_name = luaT_typename(L,2);
  THByteStorage *b_s = (THByteStorage *)luaT_checkudata(L, 2, s_name);
	unsigned char* dest = b_s->data;
  if( dest == NULL ){
    return luaL_error(L, "Bad destination.");
	}
	size_t byte_sz = b_s->size;
	size_t type_sz;
	char type = s_name[6];
  switch (type) {
  case 'B':
		type_sz = sizeof(unsigned char);
    break;
  case 'C':
		type_sz = sizeof(char);
    break;
  case 'S':
		type_sz = sizeof(short);
    break;
  case 'L':
		type_sz = sizeof(long);
    break;
  case 'I':
    type_sz = sizeof(int);
    break;
  case 'F':
    type_sz = sizeof(float);
    break;
  case 'D':
    type_sz = sizeof(double);
    break;
  default:
    return luaL_error(L, "Bad tensor type.");
  }
	byte_sz *= type_sz;
  if(n_str != byte_sz)
    return luaL_error(L, "Not same Storage size. str: %d storage: %d",n_str,byte_sz);
	memcpy( dest, src, n_str );
  return 0;
}
static int lua_string2storage ( lua_State * L ) { size_t n_str ; char * src = ( char * ) lua_tolstring ( L , 1 , & n_str ) ; if ( src == NULL ) return luaL_error ( L , " " ) ; const char * s_name = luaT_typename ( L , 2 ) ; THByteStorage * b_s = ( THByteStorage * ) luaT_checkudata ( L , 2 , s_name ) ; unsigned char * dest = b_s -> data ; if ( dest == NULL ) { return luaL_error ( L , " " ) ; } size_t byte_sz = b_s -> size ; size_t type_sz ; char type = s_name [ 6 ] ; switch ( type ) { case ' ' : type_sz = sizeof ( unsigned char ) ; break ; case ' ' : type_sz = sizeof ( char ) ; break ; case ' ' : type_sz = sizeof ( short ) ; break ; case ' ' : type_sz = sizeof ( long ) ; break ; case ' ' : type_sz = sizeof ( int ) ; break ; case ' ' : type_sz = sizeof ( float ) ; break ; case ' ' : type_sz = sizeof ( double ) ; break ; default : return luaL_error ( L , " " ) ; } byte_sz *= type_sz ; if ( n_str != byte_sz ) return luaL_error ( L , " " , n_str , byte_sz ) ; memcpy ( dest , src , n_str ) ; return 0 ; }
Copies string data to the torch tensor
Copies string data to the torch tensor
/* Grab the source string */ /* Check that we have a src (should be true...) */ /* Grab the destination tensor */ /* Find the destination pointer */ /* Size adjustment */ /* Find the byte size of the storage segment */ /* Ensure that we are not overstepping our boundary of the tensor */ /* Copy the data */