/* 
compilation:
cc -fPIC -DCOMPILE_DL=1 -I/usr/local/src/php-4.2.3/main/ -I/usr/local/src/php-4.2.3/Zend/ -I/usr/local/src/php-4.2.3/  -I/usr/local/src/php-4.2.3/TSRM  import_module.c -c -o import_module.o
gcc -shared -L/usr/local/lib -rdynamic -o import_module.so import_module.o 


usage:
- assume headers are in the first row, use them for field titles
dl("/path/to/module");
start_import("/path/to/file");
while($some_array = get_import_row())
{
// do your thing here
}
end_import();

*/
/* include standard header */
#include <php.h>

/* declaration of functions to be exported */
ZEND_FUNCTION(start_import);
ZEND_FUNCTION(get_import_row);
ZEND_FUNCTION(end_import);
//ZEND_MSHUTDOWN(end_import);

/* our vars */
FILE *fp;


/* compiled function list so Zend knows what's in this module */
zend_function_entry firstmod_functions[] =
{
    ZEND_FE(start_import, NULL)
    ZEND_FE(get_import_row, NULL)
    ZEND_FE(end_import, NULL)
    {NULL, NULL, NULL}
};

/* compiled module information */
zend_module_entry firstmod_module_entry =
{
  STANDARD_MODULE_HEADER,
  "First Module",
  firstmod_functions, /* function list */
  NULL, /* process startup */
  NULL, 
  NULL, 
  NULL, 
  NO_VERSION_YET,
  STANDARD_MODULE_PROPERTIES
};

/* implement standard "stub" routine to introduce ourselves to Zend */
#if COMPILE_DL
ZEND_GET_MODULE(firstmod)
#endif

     /* starts the file open */
ZEND_FUNCTION(start_import)
{
    zval **parameter;

    if((ZEND_NUM_ARGS() != 1) || (zend_get_parameters_ex(1, &parameter) != SUCCESS))
    {
        WRONG_PARAM_COUNT;
    }
    fp = fopen((**parameter).value.str.val, "rt");
    if (fp == NULL)
      {
	php_error(E_ERROR, "could not open file");      
	RETURN_LONG(0);
      }
    else
      {
	RETURN_LONG(1);
      }
}

/* gets a row */
#define BUF_SIZE 4096

int rcnt = 0;

ZEND_FUNCTION(get_import_row)
{
    zval **parameter;
    char buf[BUF_SIZE] = "";
    char one_char = '\0';
    char tmp_str[2] = " ";
    char int_buf[32];
    int quote_mode = 0;
    int exit_flag = 0;
    int field_pos = 0;
    int char_ctr = 0;    
    int item_set = 0;
    int get_headers = 0;
    zval **result  = NULL;

    if((ZEND_NUM_ARGS() == 0) )
    {
      get_headers = 1;
    }
    else if((ZEND_NUM_ARGS() == 1) && (zend_get_parameters_ex(1, &parameter) == SUCCESS))
      {
		
      }
    else if((ZEND_NUM_ARGS() > 1) )
    {
        WRONG_PARAM_COUNT;
    }

    rcnt++;

    if (feof(fp))
      {
	RETURN_LONG(0);
      }
    else
      {	
	array_init(return_value);
	while ((one_char = fgetc(fp)) && (!(exit_flag)) && (char_ctr < 10000))
	  { 
	    if (one_char == '"')
		quote_mode = !(quote_mode); // toggle
	    else if (one_char == '\\')
		php_error(E_WARNING, "backslash not handled yet");
	    else if ((one_char == '\n' || one_char == '\r') && (! (quote_mode)))
		exit_flag = 1; // return here
	    else if (one_char == ',' &&( !(quote_mode)))
	      {
		item_set = 1;
		if (get_headers)
		  add_index_string(return_value, field_pos++,buf, 1);    
		else
		  { 
		   	    sprintf(int_buf, "%i",zend_hash_num_elements(HASH_OF(*parameter)));
			    zend_hash_index_find(HASH_OF(*parameter), field_pos++, (void **) &result);
			    if (result != NULL)
			      add_assoc_string(return_value, (**result).value.str.val, buf, 1); 
			      //	    add_index_string(return_value, field_pos++,int_buf, 1);    
		 
		  }
		strcpy(buf,"");
	      }
	    else
	      {
		tmp_str[0] = one_char;
		strncat(buf, tmp_str, BUF_SIZE-1);
	      }
	    char_ctr++;
	  }
	if (!(item_set))
	  add_index_string(return_value,1,"empty", 6);    
      }
}

ZEND_FUNCTION(end_import)
{
  fclose(fp);
  RETURN_LONG(1);
}

