示例#1
0
int main(int argc, char **argv) {
	char Buf[256], *cps, *cph;

	Setup();
	ParseOptions(argc, argv);
	if (simple_to_from_hex)
		return simple_convert();

	// if no input redirection then give usage. I 'guess' we could allow
	// a user to type in hashes, but is that really likely?  It is almost
	// certain that if there is no input redirection, the user does not
	// know how to use the tool, so tell him how.
	if (isatty(fileno(stdin)))
		usage(argv[0]);

	FGETS(Buf, sizeof(Buf), stdin);
	while (!feof(stdin)) {
		strtok(Buf, "\r\n");
		if (!leading_salt) {
			cph = Buf;
			cps = &Buf[hash_len];
			if (salt_sep && *cps == salt_sep) ++cps;
		} else {
			cps = Buf;
			cph = &Buf[leading_salt];
			if (salt_sep && *cph == salt_sep) {*cph++ = 0;}
		}
		printf("$dynamic_%d$%*.*s$%s\n", dyna_num, hash_len,hash_len, cph, GetSalt(cps));
		FGETS(Buf, sizeof(Buf), stdin);
	}
	MEMDBG_PROGRAM_EXIT_CHECKS(stderr);
	return 0;
}
示例#2
0
STATICFNDEF char *read_table(char *b, int l, FILE *f)
{
	char	*t;
	int	fclose_res;

	ext_source_column = 0;
	FGETS(b, l, f, t);
	if (NULL == t)
	{
		if (0 != ferror(f))
		{
			FCLOSE(f, fclose_res);
			/* Expand error message */
			rts_error_csa(CSA_ARG(NULL) VARLSTCNT(1) errno);
		} else
			assert(feof(f) != 0);
	} else
	{
		/* Unfortunately, fgets does not strip the NL, we will do it for it */
		for (ext_source_line_len = 0; *t ; t++)
		{
			ext_source_line[ext_source_line_len++] = *t;
			if (CR == *t)
			{
				*t = 0;
				ext_source_line_num += 1;
				break;
			}
		}
	}
	return t;
}
示例#3
0
int main(int argc, char **argv) {
  ULONG lineCount = 10;

  try {
    argv++;
    if((*argv) && (sscanf(*argv, "%lu", &lineCount) == 1))  {
      argv++;
    }
    FILE *f = *argv ? FOPEN(*argv, _T("r")) : stdin;
    if(isatty(f)) {
      usage();
    }
    TCHAR line[4096];
    for(UINT i = 0; i < lineCount; i++) {
      if(!FGETS(line,ARRAYSIZE(line), f)) {
        break;
      }
      _tprintf(_T("%s\n"),line);
    }
    if(f != stdin) {
      fclose(f);
    }
  } catch(Exception e) {
    _ftprintf(stderr, _T("head:%s\n"), e.what());
    return -1;
  }
  return 0;
}
示例#4
0
int mu_rndwn_sem_all(void)
{
	int 			save_errno, exit_status = SS_NORMAL, semid;
	char			entry[MAX_ENTRY_LEN];
	FILE			*pf;
	char			fname[MAX_FN_LEN + 1], *fgets_res;
	boolean_t 		rem_sem;
	shm_parms		*parm_buff;

	if (NULL == (pf = POPEN(IPCS_SEM_CMD_STR ,"r")))
        {
		save_errno = errno;
		gtm_putmsg_csa(CSA_ARG(NULL) VARLSTCNT(8) ERR_SYSCALL, 5, RTS_ERROR_LITERAL("POPEN()"), CALLFROM, save_errno);
                return ERR_MUNOTALLSEC;
        }
	while (NULL != (FGETS(entry, SIZEOF(entry), pf, fgets_res)) && entry[0] != '\n')
	{
		if (-1 != (semid = parse_sem_id(entry)))
		{
			if (is_orphaned_gtm_semaphore(semid))
			{	/* semval == 0 and corresponding shared memory has been removed */
				if (-1 != semctl(semid, 0, IPC_RMID))
				{
					gtm_putmsg_csa(CSA_ARG(NULL) VARLSTCNT(3) ERR_SEMREMOVED, 1, semid);
					send_msg_csa(CSA_ARG(NULL) VARLSTCNT(3) ERR_SEMREMOVED, 1, semid);
				}
			}
		}
	}
	pclose(pf);
	return exit_status;
}
示例#5
0
/** Returns the next character from the input buffer and fills the buffer if it is empty (similar to
 * fgetc()).
 */
static
int my_getc(
   PPOS*                 ppos
   )
{
   assert(ppos      != NULL);
   assert(ppos->fp  != NULL);
   assert(ppos->pos <  LINE_BUF_SIZE);

   if (ppos->buf[ppos->pos] == '\0')
   {
#if 0
      if (NULL == FGETS(ppos->buf, sizeof(ppos->buf), ppos->fp))
         return EOF;
#else
      int len = FREAD(ppos->buf, sizeof(ppos->buf) - 1, ppos->fp);

      if (len <= 0)
         return EOF;

      ppos->buf[len] = '\0';
#endif
      ppos->pos = 0;
   }
   return (unsigned char)ppos->buf[ppos->pos++];
}
void InputThread::readTextFile(const String &fileName) {
  FILE *f = FOPEN(fileName, _T("r"));
  TCHAR line[1000];
  while(FGETS(line, ARRAYSIZE(line), f)) {
    m_inputQueue.put(line);
  }
  fclose(f);
}
示例#7
0
int mu_rndwn_all(void)
{
	int 			save_errno, fname_len, exit_status = SS_NORMAL, shmid, tmp_exit_status;
	char			entry[MAX_ENTRY_LEN];
	FILE			*pf;
	char			*fname, *fgets_res;
	shm_parms		*parm_buff;

	if (NULL == (pf = POPEN(IPCS_CMD_STR ,"r")))
        {
		save_errno = errno;
		gtm_putmsg_csa(CSA_ARG(NULL) VARLSTCNT(8) ERR_SYSCALL, 5, RTS_ERROR_LITERAL("POPEN()"), CALLFROM, save_errno);
                return ERR_MUNOTALLSEC;
        }
	fname = (char *)malloc(MAX_FN_LEN + 1);
	while (NULL != (FGETS(entry, SIZEOF(entry), pf, fgets_res)) && entry[0] != '\n')
	{
		tmp_exit_status = SS_NORMAL;
		parm_buff = get_shm_parm(entry);
		if (NULL == parm_buff)
		{
			exit_status = ERR_MUNOTALLSEC;
			continue;
		}
		mu_rndwn_all_helper(parm_buff, fname, &exit_status, &tmp_exit_status);
		if ((SS_NORMAL == exit_status) && (SS_NORMAL != tmp_exit_status))
			exit_status = tmp_exit_status;
		if (mu_rndwn_all_helper_error)
		{	/* Encountered a runtime error while processing this ipc. Make sure we return with
			 * MUNOTALLSEC and reset this static variable before starting processing on next ipc.
			 */
			mu_rndwn_all_helper_error = FALSE;
			if (SS_NORMAL == exit_status)
				exit_status = ERR_MUNOTALLSEC;
		}
		if (NULL != parm_buff)
			free(parm_buff);
	}
	pclose(pf);
	free(fname);
	return exit_status;
}
示例#8
0
void InitializeParameters::readTextFile(const String &fileName) {
  FILE *f = FOPEN(fileName, _T("r"));

  TCHAR line[100];
  CompactIntArray data;
  try {
    int lineCount = 0;
    while(FGETS(line, ARRAYSIZE(line), f)) {
      lineCount++;
      if(data.size() >= MAXDATACOUNT) {
        throwException(_T("Too many data. Max = %d"), MAXDATACOUNT);
      }
      double x;
      int i;
      if(_stscanf(line,_T("%le"),&x) == 1 && x >= 0) {
        if(x > INT_MAX) {
          i = INT_MAX;
        } else {
          i = (int)floor(x);
        }
        data.add(i);
      } else {
        throwException(_T("Illegal input in line %d:<%s>. Must be non-negative number."), lineCount, line);
      }
    }
    if(data.size() == 0) {
      throwException(_T("No data read"));
    }
    fclose(f);
    f = NULL;
    m_fileData     = data;
    m_elementCount = m_fileData.size();
    m_fileName     = fileName;
  } catch(...) {
    if(f) {
      fclose(f);
    }
    throw;
  }
}
示例#9
0
    void loadKoreanStrings()
    {
        Common::File fp;
        char fname[128];
        
        strcpy(fname, "sub/");
        strcat(fname, _gameID);
        strcat(fname, ".dat");
        
        if(!fp.open(fname)) {
            warning("WARNING: Cannot load Korean V2 subtitle!, %s\n", fname);
            return;
//            strcpy(fname, "");
//            strcat(fname, _gameID);
//            strcat(fname, ".dat");
//                
//            if(!fp.open(fname)) {
//                warning("WARNING: Cannot load Korean V2 subtitle!, %s\n", fname);
//                return;
//            }
        }
        
        int k;
        char *buf = new char[1024];
        int len = 0;
        
        for(k = 0; !fp.eos(); k++) {
            FGETS(buf, 1023, fp);
            //printf("%d: <%s>\n", k+1, buf);
        }
        // 파일의 라인 수를 계산
        _numKLines = k;
        
        // memory size = file size
        k = fp.size();
        fp.seek(0, SEEK_SET);
        
        _KBuffer = new char *[_numKLines];
        //printf("_KBuffer size = %d\n", (int)k);
        _KBuffer[0] = new char[k];
        //printf("_KBuffer[0] = 0x%x\n", (int)_KBuffer[0]);
        
        for(int i = 0; i < _numKLines; i++) {
            FGETS(buf, 1023, fp);
            buf[1023] = 0;
            len = strlen(buf);
            //_KBuffer[i] = new char[strlen(buf) + 1]; //\0
            if (i > 0)
                _KBuffer[i] = _KBuffer[i-1] + strlen(_KBuffer[i-1])+1;
            if (len > 1000)
                warning("_KBuffer[%d]:%lx, len=%d\n", i, (long)_KBuffer[i], len);
            if(strlen(buf)) {
                strcpy(_KBuffer[i], buf);
                char *b = _KBuffer[i];
                if(b[strlen(b) - 1] == '\n' || b[strlen(b) - 1] == 0x0a || b[strlen(b) - 1] == 0x0d)
                    b[strlen(b) - 1] = 0;
                if(b[strlen(b) - 1] == '\n' || b[strlen(b) - 1] == 0x0a || b[strlen(b) - 1] == 0x0d)
                    b[strlen(b) - 1] = 0;
            } else
                _KBuffer[i][0] = 0;
        }
        fp.close();
        
        warning("Korean subtitle file loaded -- total %d lines, %ld bytes\n", _numKLines, _KBuffer[_numKLines-1]-_KBuffer[0]+len);
        
        delete[] buf;
        
        return;
    }
示例#10
0
文件: wpng.c 项目: tcoupin/rok4
int main ( int argc, char **argv ) {
#ifndef DOS_OS2_W32
    FILE *keybd;
#endif
#ifdef sgi
    FILE *tmpfile;      /* or we could just use keybd, since no overlap */
    char tmpline[80];
#endif
    char *inname = NULL, outname[256];
    char *p, pnmchar, pnmline[256];
    char *bgstr, *textbuf = NULL;
    ulg rowbytes;
    int rc, len = 0;
    int error = 0;
    int text = FALSE;
    int maxval;
    double LUT_exponent;                /* just the lookup table */
    double CRT_exponent = 2.2;          /* just the monitor */
    double default_display_exponent;    /* whole display system */
    double default_gamma = 0.0;


    wpng_info.infile = NULL;
    wpng_info.outfile = NULL;
    wpng_info.image_data = NULL;
    wpng_info.row_pointers = NULL;
    wpng_info.filter = FALSE;
    wpng_info.interlaced = FALSE;
    wpng_info.have_bg = FALSE;
    wpng_info.have_time = FALSE;
    wpng_info.have_text = 0;
    wpng_info.gamma = 0.0;


    /* First get the default value for our display-system exponent, i.e.,
     * the product of the CRT exponent and the exponent corresponding to
     * the frame-buffer's lookup table (LUT), if any.  If the PNM image
     * looks correct on the user's display system, its file gamma is the
     * inverse of this value.  (Note that this is not an exhaustive list
     * of LUT values--e.g., OpenStep has a lot of weird ones--but it should
     * cover 99% of the current possibilities.  This section must ensure
     * that default_display_exponent is positive.) */

#if defined(NeXT)
    /* third-party utilities can modify the default LUT exponent */
    LUT_exponent = 1.0 / 2.2;
    /*
    if (some_next_function_that_returns_gamma(&next_gamma))
        LUT_exponent = 1.0 / next_gamma;
     */
#elif defined(sgi)
    LUT_exponent = 1.0 / 1.7;
    /* there doesn't seem to be any documented function to
     * get the "gamma" value, so we do it the hard way */
    tmpfile = fopen ( "/etc/config/system.glGammaVal", "r" );
    if ( tmpfile ) {
        double sgi_gamma;

        fgets ( tmpline, 80, tmpfile );
        fclose ( tmpfile );
        sgi_gamma = atof ( tmpline );
        if ( sgi_gamma > 0.0 )
            LUT_exponent = 1.0 / sgi_gamma;
    }
#elif defined(Macintosh)
    LUT_exponent = 1.8 / 2.61;
    /*
    if (some_mac_function_that_returns_gamma(&mac_gamma))
        LUT_exponent = mac_gamma / 2.61;
     */
#else
    LUT_exponent = 1.0;   /* assume no LUT:  most PCs */
#endif

    /* the defaults above give 1.0, 1.3, 1.5 and 2.2, respectively: */
    default_display_exponent = LUT_exponent * CRT_exponent;


    /* If the user has set the SCREEN_GAMMA environment variable as suggested
     * (somewhat imprecisely) in the libpng documentation, use that; otherwise
     * use the default value we just calculated.  Either way, the user may
     * override this via a command-line option. */

    if ( ( p = getenv ( "SCREEN_GAMMA" ) ) != NULL ) {
        double exponent = atof ( p );

        if ( exponent > 0.0 )
            default_gamma = 1.0 / exponent;
    }

    if ( default_gamma == 0.0 )
        default_gamma = 1.0 / default_display_exponent;


    /* Now parse the command line for options and the PNM filename. */

    while ( *++argv && !error ) {
        if ( !strncmp ( *argv, "-i", 2 ) ) {
            wpng_info.interlaced = TRUE;
        } else if ( !strncmp ( *argv, "-time", 3 ) ) {
            wpng_info.modtime = time ( NULL );
            wpng_info.have_time = TRUE;
        } else if ( !strncmp ( *argv, "-text", 3 ) ) {
            text = TRUE;
        } else if ( !strncmp ( *argv, "-gamma", 2 ) ) {
            if ( !*++argv )
                ++error;
            else {
                wpng_info.gamma = atof ( *argv );
                if ( wpng_info.gamma <= 0.0 )
                    ++error;
                else if ( wpng_info.gamma > 1.01 )
                    fprintf ( stderr, PROGNAME
                              " warning:  file gammas are usually less than 1.0\n" );
            }
        } else if ( !strncmp ( *argv, "-bgcolor", 4 ) ) {
            if ( !*++argv )
                ++error;
            else {
                bgstr = *argv;
                if ( strlen ( bgstr ) != 7 || bgstr[0] != '#' )
                    ++error;
                else {
                    unsigned r, g, b;  /* this way quiets compiler warnings */

                    sscanf ( bgstr+1, "%2x%2x%2x", &r, &g, &b );
                    wpng_info.bg_red   = ( uch ) r;
                    wpng_info.bg_green = ( uch ) g;
                    wpng_info.bg_blue  = ( uch ) b;
                    wpng_info.have_bg = TRUE;
                }
            }
        } else {
            if ( **argv != '-' ) {
                inname = *argv;
                if ( argv[1] ) /* shouldn't be any more args after filename */
                    ++error;
            } else
                ++error;   /* not expecting any other options */
        }
    }


    /* open the input and output files, or register an error and abort */

    if ( !inname ) {
        if ( isatty ( 0 ) ) {
            fprintf ( stderr, PROGNAME
                      ":  must give input filename or provide image data via stdin\n" );
            ++error;
        } else {
#ifdef DOS_OS2_W32
            /* some buggy C libraries require BOTH setmode() and fdopen(bin) */
            setmode ( fileno ( stdin ), O_BINARY );
            setmode ( fileno ( stdout ), O_BINARY );
#endif
            if ( ( wpng_info.infile = fdopen ( fileno ( stdin ), "rb" ) ) == NULL ) {
                fprintf ( stderr, PROGNAME
                          ":  unable to reopen stdin in binary mode\n" );
                ++error;
            } else if ( ( wpng_info.outfile = fdopen ( fileno ( stdout ), "wb" ) ) == NULL ) {
                fprintf ( stderr, PROGNAME
                          ":  unable to reopen stdout in binary mode\n" );
                fclose ( wpng_info.infile );
                ++error;
            } else
                wpng_info.filter = TRUE;
        }
    } else if ( ( len = strlen ( inname ) ) > 250 ) {
        fprintf ( stderr, PROGNAME ":  input filename is too long [%d chars]\n",
                  len );
        ++error;
    } else if ( ! ( wpng_info.infile = fopen ( inname, "rb" ) ) ) {
        fprintf ( stderr, PROGNAME ":  can't open input file [%s]\n", inname );
        ++error;
    }

    if ( !error ) {
        fgets ( pnmline, 256, wpng_info.infile );
        if ( pnmline[0] != 'P' || ( ( pnmchar = pnmline[1] ) != '5' &&
                                    pnmchar != '6' && pnmchar != '8' ) ) {
            fprintf ( stderr, PROGNAME
                      ":  input file [%s] is not a binary PGM, PPM or PAM file\n",
                      inname );
            ++error;
        } else {
            wpng_info.pnmtype = ( int ) ( pnmchar - '0' );
            if ( wpng_info.pnmtype != 8 )
                wpng_info.have_bg = FALSE;  /* no need for bg if opaque */
            do {
                fgets ( pnmline, 256, wpng_info.infile ); /* lose any comments */
            } while ( pnmline[0] == '#' );
            sscanf ( pnmline, "%ld %ld", &wpng_info.width, &wpng_info.height );
            do {
                fgets ( pnmline, 256, wpng_info.infile ); /* more comment lines */
            } while ( pnmline[0] == '#' );
            sscanf ( pnmline, "%d", &maxval );
            if ( wpng_info.width <= 0L || wpng_info.height <= 0L ||
                    maxval != 255 ) {
                fprintf ( stderr, PROGNAME
                          ":  only positive width/height, maxval == 255 allowed \n" );
                ++error;
            }
            wpng_info.sample_depth = 8;  /* <==> maxval 255 */

            if ( !wpng_info.filter ) {
                /* make outname from inname */
                if ( ( p = strrchr ( inname, '.' ) ) == NULL ||
                        ( p - inname ) != ( len - 4 ) ) {
                    strcpy ( outname, inname );
                    strcpy ( outname+len, ".png" );
                } else {
                    len -= 4;
                    strncpy ( outname, inname, len );
                    strcpy ( outname+len, ".png" );
                }
                /* check if outname already exists; if not, open */
                if ( ( wpng_info.outfile = fopen ( outname, "rb" ) ) != NULL ) {
                    fprintf ( stderr, PROGNAME ":  output file exists [%s]\n",
                              outname );
                    fclose ( wpng_info.outfile );
                    ++error;
                } else if ( ! ( wpng_info.outfile = fopen ( outname, "wb" ) ) ) {
                    fprintf ( stderr, PROGNAME ":  can't open output file [%s]\n",
                              outname );
                    ++error;
                }
            }
        }
        if ( error ) {
            fclose ( wpng_info.infile );
            wpng_info.infile = NULL;
            if ( wpng_info.filter ) {
                fclose ( wpng_info.outfile );
                wpng_info.outfile = NULL;
            }
        }
    }


    /* if we had any errors, print usage and die horrible death...arrr! */

    if ( error ) {
        fprintf ( stderr, "\n%s %s:  %s\n", PROGNAME, VERSION, APPNAME );
        writepng_version_info();
        fprintf ( stderr, "\n"
                  "Usage:  %s [-gamma exp] [-bgcolor bg] [-text] [-time] [-interlace] pnmfile\n"
                  "or: ... | %s [-gamma exp] [-bgcolor bg] [-text] [-time] [-interlace] | ...\n"
                  "    exp \ttransfer-function exponent (``gamma'') of the image in\n"
                  "\t\t  floating-point format (e.g., ``%.5f''); if image looks\n"
                  "\t\t  correct on given display system, image gamma is equal to\n"
                  "\t\t  inverse of display-system exponent, i.e., 1 / (LUT * CRT)\n"
                  "\t\t  (where LUT = lookup-table exponent and CRT = CRT exponent;\n"
                  "\t\t  first varies, second is usually 2.2, all are positive)\n"
                  "    bg  \tdesired background color for alpha-channel images, in\n"
                  "\t\t  7-character hex RGB format (e.g., ``#ff7700'' for orange:\n"
                  "\t\t  same as HTML colors)\n"
                  "    -text\tprompt interactively for text info (tEXt chunks)\n"
                  "    -time\tinclude a tIME chunk (last modification time)\n"
                  "    -interlace\twrite interlaced PNG image\n"
                  "\n"
                  "pnmfile or stdin must be a binary PGM (`P5'), PPM (`P6') or (extremely\n"
                  "unofficial and unsupported!) PAM (`P8') file.  Currently it is required\n"
                  "to have maxval == 255 (i.e., no scaling).  If pnmfile is specified, it\n"
                  "is converted to the corresponding PNG file with the same base name but a\n"
                  "``.png'' extension; files read from stdin are converted and sent to stdout.\n"
                  "The conversion is progressive (low memory usage) unless interlacing is\n"
                  "requested; in that case the whole image will be buffered in memory and\n"
                  "written in one call.\n"
                  "\n", PROGNAME, PROGNAME, default_gamma );
        exit ( 1 );
    }


    /* prepare the text buffers for libpng's use; note that even though
     * PNG's png_text struct includes a length field, we don't have to fill
     * it out */

    if ( text &&
#ifndef DOS_OS2_W32
            ( keybd = fdopen ( fileno ( stderr ), "r" ) ) != NULL &&
#endif
            ( textbuf = ( char * ) malloc ( ( 5 + 9 ) *75 ) ) != NULL ) {
        int i, valid, result;

        fprintf ( stderr,
                  "Enter text info (no more than 72 characters per line);\n" );
        fprintf ( stderr, "to skip a field, hit the <Enter> key.\n" );
        /* note:  just <Enter> leaves len == 1 */

        do {
            valid = TRUE;
            p = textbuf + TEXT_TITLE_OFFSET;
            fprintf ( stderr, "  Title: " );
            fflush ( stderr );
            if ( FGETS ( p, 74, keybd ) && ( len = strlen ( p ) ) > 1 ) {
                if ( p[len-1] == '\n' )
                    p[--len] = '\0';
                wpng_info.title = p;
                wpng_info.have_text |= TEXT_TITLE;
                if ( ( result = wpng_isvalid_latin1 ( ( uch * ) p, len ) ) >= 0 ) {
                    fprintf ( stderr, "    " PROGNAME " warning:  character code"
                              " %u is %sdiscouraged by the PNG\n    specification "
                              "[first occurrence was at character position #%d]\n",
                              ( unsigned ) p[result], ( p[result] == 27 ) ? "strongly " : "",
                              result+1 );
                    fflush ( stderr );
#ifdef FORBID_LATIN1_CTRL
                    wpng_info.have_text &= ~TEXT_TITLE;
                    valid = FALSE;
#else
                    if ( p[result] == 27 ) {  /* escape character */
                        wpng_info.have_text &= ~TEXT_TITLE;
                        valid = FALSE;
                    }
#endif
                }
            }
        } while ( !valid );

        do {
            valid = TRUE;
            p = textbuf + TEXT_AUTHOR_OFFSET;
            fprintf ( stderr, "  Author: " );
            fflush ( stderr );
            if ( FGETS ( p, 74, keybd ) && ( len = strlen ( p ) ) > 1 ) {
                if ( p[len-1] == '\n' )
                    p[--len] = '\0';
                wpng_info.author = p;
                wpng_info.have_text |= TEXT_AUTHOR;
                if ( ( result = wpng_isvalid_latin1 ( ( uch * ) p, len ) ) >= 0 ) {
                    fprintf ( stderr, "    " PROGNAME " warning:  character code"
                              " %u is %sdiscouraged by the PNG\n    specification "
                              "[first occurrence was at character position #%d]\n",
                              ( unsigned ) p[result], ( p[result] == 27 ) ? "strongly " : "",
                              result+1 );
                    fflush ( stderr );
#ifdef FORBID_LATIN1_CTRL
                    wpng_info.have_text &= ~TEXT_AUTHOR;
                    valid = FALSE;
#else
                    if ( p[result] == 27 ) {  /* escape character */
                        wpng_info.have_text &= ~TEXT_AUTHOR;
                        valid = FALSE;
                    }
#endif
                }
            }
        } while ( !valid );

        do {
            valid = TRUE;
            p = textbuf + TEXT_DESC_OFFSET;
            fprintf ( stderr, "  Description (up to 9 lines):\n" );
            for ( i = 1;  i < 10;  ++i ) {
                fprintf ( stderr, "    [%d] ", i );
                fflush ( stderr );
                if ( FGETS ( p, 74, keybd ) && ( len = strlen ( p ) ) > 1 )
                    p += len;   /* now points at NULL; char before is newline */
                else
                    break;
            }
            if ( ( len = p - ( textbuf + TEXT_DESC_OFFSET ) ) > 1 ) {
                if ( p[-1] == '\n' ) {
                    p[-1] = '\0';
                    --len;
                }
                wpng_info.desc = textbuf + TEXT_DESC_OFFSET;
                wpng_info.have_text |= TEXT_DESC;
                p = textbuf + TEXT_DESC_OFFSET;
                if ( ( result = wpng_isvalid_latin1 ( ( uch * ) p, len ) ) >= 0 ) {
                    fprintf ( stderr, "    " PROGNAME " warning:  character code"
                              " %u is %sdiscouraged by the PNG\n    specification "
                              "[first occurrence was at character position #%d]\n",
                              ( unsigned ) p[result], ( p[result] == 27 ) ? "strongly " : "",
                              result+1 );
                    fflush ( stderr );
#ifdef FORBID_LATIN1_CTRL
                    wpng_info.have_text &= ~TEXT_DESC;
                    valid = FALSE;
#else
                    if ( p[result] == 27 ) {  /* escape character */
                        wpng_info.have_text &= ~TEXT_DESC;
                        valid = FALSE;
                    }
#endif
                }
            }
        } while ( !valid );

        do {
            valid = TRUE;
            p = textbuf + TEXT_COPY_OFFSET;
            fprintf ( stderr, "  Copyright: " );
            fflush ( stderr );
            if ( FGETS ( p, 74, keybd ) && ( len = strlen ( p ) ) > 1 ) {
                if ( p[len-1] == '\n' )
                    p[--len] = '\0';
                wpng_info.copyright = p;
                wpng_info.have_text |= TEXT_COPY;
                if ( ( result = wpng_isvalid_latin1 ( ( uch * ) p, len ) ) >= 0 ) {
                    fprintf ( stderr, "    " PROGNAME " warning:  character code"
                              " %u is %sdiscouraged by the PNG\n    specification "
                              "[first occurrence was at character position #%d]\n",
                              ( unsigned ) p[result], ( p[result] == 27 ) ? "strongly " : "",
                              result+1 );
                    fflush ( stderr );
#ifdef FORBID_LATIN1_CTRL
                    wpng_info.have_text &= ~TEXT_COPY;
                    valid = FALSE;
#else
                    if ( p[result] == 27 ) {  /* escape character */
                        wpng_info.have_text &= ~TEXT_COPY;
                        valid = FALSE;
                    }
#endif
                }
            }
        } while ( !valid );

        do {
            valid = TRUE;
            p = textbuf + TEXT_EMAIL_OFFSET;
            fprintf ( stderr, "  E-mail: " );
            fflush ( stderr );
            if ( FGETS ( p, 74, keybd ) && ( len = strlen ( p ) ) > 1 ) {
                if ( p[len-1] == '\n' )
                    p[--len] = '\0';
                wpng_info.email = p;
                wpng_info.have_text |= TEXT_EMAIL;
                if ( ( result = wpng_isvalid_latin1 ( ( uch * ) p, len ) ) >= 0 ) {
                    fprintf ( stderr, "    " PROGNAME " warning:  character code"
                              " %u is %sdiscouraged by the PNG\n    specification "
                              "[first occurrence was at character position #%d]\n",
                              ( unsigned ) p[result], ( p[result] == 27 ) ? "strongly " : "",
                              result+1 );
                    fflush ( stderr );
#ifdef FORBID_LATIN1_CTRL
                    wpng_info.have_text &= ~TEXT_EMAIL;
                    valid = FALSE;
#else
                    if ( p[result] == 27 ) {  /* escape character */
                        wpng_info.have_text &= ~TEXT_EMAIL;
                        valid = FALSE;
                    }
#endif
                }
            }
        } while ( !valid );

        do {
            valid = TRUE;
            p = textbuf + TEXT_URL_OFFSET;
            fprintf ( stderr, "  URL: " );
            fflush ( stderr );
            if ( FGETS ( p, 74, keybd ) && ( len = strlen ( p ) ) > 1 ) {
                if ( p[len-1] == '\n' )
                    p[--len] = '\0';
                wpng_info.url = p;
                wpng_info.have_text |= TEXT_URL;
                if ( ( result = wpng_isvalid_latin1 ( ( uch * ) p, len ) ) >= 0 ) {
                    fprintf ( stderr, "    " PROGNAME " warning:  character code"
                              " %u is %sdiscouraged by the PNG\n    specification "
                              "[first occurrence was at character position #%d]\n",
                              ( unsigned ) p[result], ( p[result] == 27 ) ? "strongly " : "",
                              result+1 );
                    fflush ( stderr );
#ifdef FORBID_LATIN1_CTRL
                    wpng_info.have_text &= ~TEXT_URL;
                    valid = FALSE;
#else
                    if ( p[result] == 27 ) {  /* escape character */
                        wpng_info.have_text &= ~TEXT_URL;
                        valid = FALSE;
                    }
#endif
                }
            }
        } while ( !valid );

#ifndef DOS_OS2_W32
        fclose ( keybd );
#endif

    } else if ( text ) {
        fprintf ( stderr, PROGNAME ":  unable to allocate memory for text\n" );
        text = FALSE;
        wpng_info.have_text = 0;
    }


    /* allocate libpng stuff, initialize transformations, write pre-IDAT data */

    if ( ( rc = writepng_init ( &wpng_info ) ) != 0 ) {
        switch ( rc ) {
        case 2:
            fprintf ( stderr, PROGNAME
                      ":  libpng initialization problem (longjmp)\n" );
            break;
        case 4:
            fprintf ( stderr, PROGNAME ":  insufficient memory\n" );
            break;
        case 11:
            fprintf ( stderr, PROGNAME
                      ":  internal logic error (unexpected PNM type)\n" );
            break;
        default:
            fprintf ( stderr, PROGNAME
                      ":  unknown writepng_init() error\n" );
            break;
        }
        exit ( rc );
    }


    /* free textbuf, since it's a completely local variable and all text info
     * has just been written to the PNG file */

    if ( text && textbuf ) {
        free ( textbuf );
        textbuf = NULL;
    }


    /* calculate rowbytes on basis of image type; note that this becomes much
     * more complicated if we choose to support PBM type, ASCII PNM types, or
     * 16-bit-per-sample binary data [currently not an official NetPBM type] */

    if ( wpng_info.pnmtype == 5 )
        rowbytes = wpng_info.width;
    else if ( wpng_info.pnmtype == 6 )
        rowbytes = wpng_info.width * 3;
    else /* if (wpng_info.pnmtype == 8) */
        rowbytes = wpng_info.width * 4;


    /* read and write the image, either in its entirety (if writing interlaced
     * PNG) or row by row (if non-interlaced) */

    fprintf ( stderr, "Encoding image data...\n" );
    fflush ( stderr );

    if ( wpng_info.interlaced ) {
        long i;
        ulg bytes;
        ulg image_bytes = rowbytes * wpng_info.height;   /* overflow? */

        wpng_info.image_data = ( uch * ) malloc ( image_bytes );
        wpng_info.row_pointers = ( uch ** ) malloc ( wpng_info.height*sizeof ( uch * ) );
        if ( wpng_info.image_data == NULL || wpng_info.row_pointers == NULL ) {
            fprintf ( stderr, PROGNAME ":  insufficient memory for image data\n" );
            writepng_cleanup ( &wpng_info );
            wpng_cleanup();
            exit ( 5 );
        }
        for ( i = 0;  i < wpng_info.height;  ++i )
            wpng_info.row_pointers[i] = wpng_info.image_data + i*rowbytes;
        bytes = fread ( wpng_info.image_data, 1, image_bytes, wpng_info.infile );
        if ( bytes != image_bytes ) {
            fprintf ( stderr, PROGNAME ":  expected %lu bytes, got %lu bytes\n",
                      image_bytes, bytes );
            fprintf ( stderr, "  (continuing anyway)\n" );
        }
        if ( writepng_encode_image ( &wpng_info ) != 0 ) {
            fprintf ( stderr, PROGNAME
                      ":  libpng problem (longjmp) while writing image data\n" );
            writepng_cleanup ( &wpng_info );
            wpng_cleanup();
            exit ( 2 );
        }

    } else { /* not interlaced:  write progressively (row by row) */
        long j;
        ulg bytes;

        wpng_info.image_data = ( uch * ) malloc ( rowbytes );
        if ( wpng_info.image_data == NULL ) {
            fprintf ( stderr, PROGNAME ":  insufficient memory for row data\n" );
            writepng_cleanup ( &wpng_info );
            wpng_cleanup();
            exit ( 5 );
        }
        error = 0;
        for ( j = wpng_info.height;  j > 0L;  --j ) {
            bytes = fread ( wpng_info.image_data, 1, rowbytes, wpng_info.infile );
            if ( bytes != rowbytes ) {
                fprintf ( stderr, PROGNAME
                          ":  expected %lu bytes, got %lu bytes (row %ld)\n", rowbytes,
                          bytes, wpng_info.height-j );
                ++error;
                break;
            }
            if ( writepng_encode_row ( &wpng_info ) != 0 ) {
                fprintf ( stderr, PROGNAME
                          ":  libpng problem (longjmp) while writing row %ld\n",
                          wpng_info.height-j );
                ++error;
                break;
            }
        }
        if ( error ) {
            writepng_cleanup ( &wpng_info );
            wpng_cleanup();
            exit ( 2 );
        }
        if ( writepng_encode_finish ( &wpng_info ) != 0 ) {
            fprintf ( stderr, PROGNAME ":  error on final libpng call\n" );
            writepng_cleanup ( &wpng_info );
            wpng_cleanup();
            exit ( 2 );
        }
    }


    /* OK, we're done (successfully):  clean up all resources and quit */

    fprintf ( stderr, "Done.\n" );
    fflush ( stderr );

    writepng_cleanup ( &wpng_info );
    wpng_cleanup();

    return 0;
}
示例#11
0
/*
  Function: peerRead2
  
  Reads a matrix in PEER format.
  
  first file contain :
  > NumberOfFiles
  > file1
  > file2
  > ...

  each file contains:
  > %ld %ld %ld  (globaln localn  localnnzeros)
  six elements from col by line, localnnzeros elements in total,
  six elements from row by line, localnnzeros elements in total,
  six elements from val by line, localnnzeros elements in total,
  six elements from rhs by line, localnnzeros elements in total,

  for each part, last line can be with 1, 2 ,3 , 4, 5 or 6 elements.

  Parameters:
    filename - Path to file to read from
    Nrow     - Number of rows						 
    Ncol     - Number of columns					 
    Nnzero,  - Number of non zeros					 
    col      - Index of first element of each column in *row* and *val* 
    row      - Row of eah element				       	 
    val      - Value of each element				       	 
    Type     - Type of the matrix				       	 
    RhsType  - Type of the right-hand-side.			         
    rhs      - right-hand-side term(s)
*/
void peerRead2(char const      *filename, 
	       pastix_int_t    *Nrow, 
	       pastix_int_t    *Ncol, 
	       pastix_int_t    *Nnzero, 
	       pastix_int_t   **col, 
	       pastix_int_t   **row, 
	       pastix_float_t **val, 
	       char           **Type, 
	       char           **RhsType, 
	       pastix_float_t **rhs)
{
  FILE *infile;
  pastix_int_t iterfile;
  char line[BUFSIZ];
  pastix_int_t rowlocal,rowglobal;
  pastix_int_t nzlocal,nzglobal;
  long filenamenumber;
  char **filenametab;
  pastix_int_t nbreltperline=6; /* nbr of elt per line */
  long tempint1,tempint2,tempint3,tempint4,tempint5,tempint6;
  long double tempfloat1, tempfloat2, tempfloat3;

  *Nnzero=0;
  *Type = (char *) malloc(4*sizeof(char));
  *RhsType = (char *) malloc(4*sizeof(char));

  (*Type)[0] = 'R';
  (*Type)[1] = 'U';
  (*Type)[2] = 'A';
  (*Type)[3] = '\0';
  (*RhsType)[0] = 'A';
  (*RhsType)[2] = 'A';
  (*RhsType)[3] = '\0';

  /* Read rsaname */
  infile = fopen(filename, "r");
  if (infile==NULL)
    {
      fprintf(stderr,"cannot load %s\n", filename);
      EXIT(MOD_SI,FILE_ERR);
    }
  FGETS(line, BUFSIZ, infile);
  sscanf(line, "%ld", &filenamenumber); /* Read number of filename */
  filenametab = (char **) malloc(filenamenumber*sizeof(char *));
  for (iterfile=0; iterfile<filenamenumber; iterfile++)
    {
      filenametab[iterfile] = (char *) malloc(64*sizeof(char));
      FGETS(line, BUFSIZ, infile);
      sscanf(line, "%s", filenametab[iterfile]);
    }
  fclose(infile);
  
  /* Calcul nnz global */
  for (iterfile=0; iterfile<filenamenumber; iterfile++)
    {
      infile = fopen(filenametab[iterfile], "r");
      if (infile==NULL)
	{
	  fprintf(stderr,"cannot load %s\n", filenametab[iterfile]);
	  EXIT(MOD_SI,FILE_ERR);
	}
      FGETS(line, BUFSIZ, infile);
      sscanf(line, "%ld %ld %ld",&tempint1,&tempint2,&tempint3);
      *Nrow = (pastix_int_t)tempint1;
      rowlocal = (pastix_int_t)tempint2;
      nzlocal = (pastix_int_t)tempint3;
      printf("Nrow %ld rowlocal %ld nzlocal %ld\n", (long) *Nrow, (long) rowlocal, (long) nzlocal);
      fclose(infile);
      *Nnzero += nzlocal;
    }
  *Ncol = *Nrow;
  printf("Nnzero global %ld\n", (long)*Nnzero);

  /* memory alloc */
  (*col) = (pastix_int_t *) malloc((*Nrow+1)*sizeof(pastix_int_t));
  if ((*col) == NULL)
    fprintf(stderr, "peerRead : Not enough memory for *col\n");
  (*row) = (pastix_int_t *) malloc(*Nnzero*sizeof(pastix_int_t));
  if ((*row) == NULL)
    fprintf(stderr, "peerRead : Not enough memory for *row\n");
  (*val) = (pastix_float_t *) malloc(*Nnzero*sizeof(pastix_float_t));
  if ((*val) == NULL)
    fprintf(stderr, "peerRead : Not enough memory for *val\n");
  (*rhs) = (pastix_float_t *) malloc(*Nrow*sizeof(pastix_float_t));
  if ((*rhs) == NULL)
    fprintf(stderr, "peerRead : Not enough memory for *rhs\n");

  rowglobal=0;
  nzglobal=0;
  for (iterfile=0; iterfile<filenamenumber; iterfile++)
    {
      pastix_int_t iterelt;
      
      infile = fopen(filenametab[iterfile], "r");
      if (infile==NULL)
	{
	  fprintf(stderr,"cannot load %s\n", filenametab[iterfile]);
	  EXIT(MOD_SI,FILE_ERR);
	}
      FGETS(line,BUFSIZ,infile);
      sscanf(line, "%ld %ld %ld", &tempint1, &tempint2, &tempint3);
      *Nrow = (pastix_int_t)tempint1;
      rowlocal = (pastix_int_t)tempint2;
      nzlocal = (pastix_int_t)tempint3;

      /* read col */
      for (iterelt=0; iterelt<rowlocal+1+1-nbreltperline;iterelt++)
	{
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%ld %ld %ld %ld %ld %ld", &tempint1, &tempint2, &tempint3, &tempint4, &tempint5, &tempint6);
	  (*col)[iterelt+rowglobal] = tempint1+nzglobal;
	  (*col)[iterelt+rowglobal+1] = tempint2+nzglobal;
	  (*col)[iterelt+rowglobal+2] = tempint3+nzglobal;
	  (*col)[iterelt+rowglobal+3] = tempint4+nzglobal;
	  (*col)[iterelt+rowglobal+4] = tempint5+nzglobal;
	  (*col)[iterelt+rowglobal+5] = tempint6+nzglobal;
	  iterelt+=5;
	}
      switch (rowlocal-iterelt+1)
	{
	case 1:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%ld",&tempint1);
	  (*col)[iterelt+rowglobal] += tempint1+nzglobal;
	  iterelt++;
	  break;
	case 2:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%ld %ld", &tempint1, &tempint2);
	  (*col)[iterelt+rowglobal] = tempint1+nzglobal;
	  (*col)[iterelt+rowglobal+1] = tempint2+nzglobal;
	  iterelt+=2;
	  break;
	case 3:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%ld %ld %ld", &tempint1, &tempint2, &tempint3);
	  (*col)[iterelt+rowglobal] = tempint1+nzglobal;
	  (*col)[iterelt+rowglobal+1] = tempint2+nzglobal;
	  (*col)[iterelt+rowglobal+2] = tempint3+nzglobal;
	  iterelt+=3;
	  break;
	case 4:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%ld %ld %ld %ld", &tempint1, &tempint2, &tempint3, &tempint4);
	  (*col)[iterelt+rowglobal] = tempint1+nzglobal;
	  (*col)[iterelt+rowglobal+1] = tempint2+nzglobal;
	  (*col)[iterelt+rowglobal+2] = tempint3+nzglobal;
	  (*col)[iterelt+rowglobal+3] = tempint4+nzglobal;
	  iterelt+=4;
	  break;
	case 5:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%ld %ld %ld %ld %ld", &tempint1, &tempint2, &tempint3, &tempint4, &tempint5);
	  (*col)[iterelt+rowglobal] = tempint1+nzglobal;
	  (*col)[iterelt+rowglobal+1] = tempint2+nzglobal;
	  (*col)[iterelt+rowglobal+2] = tempint3+nzglobal;
	  (*col)[iterelt+rowglobal+3] = tempint4+nzglobal;
	  (*col)[iterelt+rowglobal+4] = tempint5+nzglobal;
	  iterelt+=5;
	  break;
	}

      /* read row */
      for (iterelt=0; iterelt<nzlocal+1-nbreltperline; iterelt++)
	{
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%ld %ld %ld %ld %ld %ld", &tempint1,&tempint2,&tempint3,&tempint4,&tempint5,&tempint6);
	  (*row)[iterelt+nzglobal]   = (pastix_int_t)tempint1;
	  (*row)[iterelt+nzglobal+1] = (pastix_int_t)tempint2;
	  (*row)[iterelt+nzglobal+2] = (pastix_int_t)tempint3;
	  (*row)[iterelt+nzglobal+3] = (pastix_int_t)tempint4;
	  (*row)[iterelt+nzglobal+4] = (pastix_int_t)tempint5;
	  (*row)[iterelt+nzglobal+5] = (pastix_int_t)tempint6;
	  iterelt+=5;
	}
      switch (nzlocal-iterelt)
	{
	case 1:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%ld",&tempint1);
	  (*row)[iterelt+nzglobal] = (pastix_int_t)tempint1;
	  iterelt++;
	  break;
	case 2:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%ld %ld", &tempint1, &tempint2);
	  (*row)[iterelt+nzglobal]   = (pastix_int_t)tempint1;
	  (*row)[iterelt+nzglobal+1] = (pastix_int_t)tempint2;
	  iterelt+=2;
	  break;
	case 3:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%ld %ld %ld", &tempint1, &tempint2, &tempint3);
	  (*row)[iterelt+nzglobal]   = (pastix_int_t)tempint1;
	  (*row)[iterelt+nzglobal+1] = (pastix_int_t)tempint2;
	  (*row)[iterelt+nzglobal+2] = (pastix_int_t)tempint3;
	  iterelt+=3;
	  break;
	case 4:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%ld %ld %ld %ld", &tempint1, &tempint2, &tempint3, &tempint4);
	  (*row)[iterelt+nzglobal]   = (pastix_int_t)tempint1;
	  (*row)[iterelt+nzglobal+1] = (pastix_int_t)tempint2;
	  (*row)[iterelt+nzglobal+2] = (pastix_int_t)tempint3;
	  (*row)[iterelt+nzglobal+3] = (pastix_int_t)tempint4;
	  iterelt+=4;
	  break;
	case 5:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%ld %ld %ld %ld %ld", &tempint1, &tempint2, &tempint3, &tempint4, &tempint5);
	  (*row)[iterelt+nzglobal]   = (pastix_int_t)tempint1;
	  (*row)[iterelt+nzglobal+1] = (pastix_int_t)tempint2;
	  (*row)[iterelt+nzglobal+2] = (pastix_int_t)tempint3;
	  (*row)[iterelt+nzglobal+3] = (pastix_int_t)tempint4;
	  (*row)[iterelt+nzglobal+4] = (pastix_int_t)tempint5;
	  iterelt+=5;
	  break;
	}
      
      nbreltperline=3; /* nbr of elt per line */
      
      /* read val */
      for (iterelt=0; iterelt<nzlocal+1-nbreltperline; iterelt++)
	{
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%Lf %Lf %Lf",&tempfloat1,&tempfloat2,&tempfloat3);
	  (*val)[iterelt+nzglobal]   = (pastix_float_t)tempfloat1;
	  (*val)[iterelt+nzglobal+1] = (pastix_float_t)tempfloat2;
	  (*val)[iterelt+nzglobal+2] = (pastix_float_t)tempfloat3;
	  iterelt+=2;
	}
      switch (nzlocal-iterelt)
	{
	case 1:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%Lf",&tempfloat1);
	  (*val)[iterelt+nzglobal] = (pastix_float_t)tempfloat1;
	  iterelt++;
	  break;
	case 2:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%Lf %Lf",&tempfloat1,&tempfloat2);
	  (*val)[iterelt+nzglobal]   = (pastix_float_t)tempfloat1;
	  (*val)[iterelt+nzglobal+1] = (pastix_float_t)tempfloat2;
	  iterelt+=2;
	  break;
	case 3:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%Lf %Lf %Lf",&tempfloat1,&tempfloat2,&tempfloat3);
	  (*val)[iterelt+nzglobal]   = (pastix_float_t)tempfloat1;
	  (*val)[iterelt+nzglobal+1] = (pastix_float_t)tempfloat2;
	  (*val)[iterelt+nzglobal+2] = (pastix_float_t)tempfloat3;
	  iterelt+=3;
	  break;
	}
      nzglobal += nzlocal;

      /* read rhs */
      for (iterelt=0; iterelt<rowlocal+1-nbreltperline; iterelt++)
	{
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%Lf %Lf %Lf",&tempfloat1,&tempfloat2,&tempfloat3);
	  (*rhs)[iterelt+rowglobal]   = (pastix_float_t)tempfloat1;
	  (*rhs)[iterelt+rowglobal+1] = (pastix_float_t)tempfloat2;
	  (*rhs)[iterelt+rowglobal+2] = (pastix_float_t)tempfloat3;
	  iterelt+=2;
	}

      switch (rowlocal-iterelt)
	{
	case 1:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%Lf",&tempfloat1);
	  (*rhs)[iterelt+rowglobal] = (pastix_float_t)tempfloat1;
	  iterelt++;
	  break;
	case 2:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%Lf %Lf",&tempfloat1,&tempfloat2);
	  (*rhs)[iterelt+rowglobal]   = (pastix_float_t)tempfloat1;
	  (*rhs)[iterelt+rowglobal+1] = (pastix_float_t)tempfloat2;
	  iterelt++;
	  break;
	case 3:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%Lf %Lf %Lf",&tempfloat1,&tempfloat2,&tempfloat3);
	  (*rhs)[iterelt+rowglobal]   = (pastix_float_t)tempfloat1;
	  (*rhs)[iterelt+rowglobal+1] = (pastix_float_t)tempfloat2;
	  (*rhs)[iterelt+rowglobal+2] = (pastix_float_t)tempfloat3;
	  iterelt++;
	  break;
	}
      rowglobal += rowlocal;

      fclose(infile);
    }
}
示例#12
0
/*
  Function: peerRead
  
  Reads a matrix in PEER format.
  
  first file contain :
  > NumberOfFiles
  > file1
  > file2
  > ...

  each file contains:
  > %ld%ld          (globaln localn)
  > %ld             (local nnzeros)
  four elements from col by line, nnzeros local elements in total,
  four elements from row by line, nnzeros local elements in total,
  four elements from val by line, nnzeros local elements in total,
  four elements from rhs by line, nnzeros local elements in total,

  for each part, last line can be with 1, 2 ,3 or 4 elements.

  Parameters:
    filename - Path to file to read from
    Nrow     - Number of rows						 
    Ncol     - Number of columns					 
    Nnzero,  - Number of non zeros					 
    col      - Index of first element of each column in *row* and *val* 
    row      - Row of eah element				       	 
    val      - Value of each element				       	 
    Type     - Type of the matrix				       	 
    RhsType  - Type of the right-hand-side.			         
    rhs      - right-hand-side term(s)
*/
void peerRead(char const      *filename, 
	      pastix_int_t    *Nrow, 
	      pastix_int_t    *Ncol, 
	      pastix_int_t    *Nnzero, 
	      pastix_int_t   **col, 
	      pastix_int_t   **row, 
	      pastix_float_t **val, 
	      char           **Type, 
	      char           **RhsType, 
	      pastix_float_t **rhs)
{
  FILE *infile;
  pastix_int_t iterfile;
  char line[BUFSIZ];
  pastix_int_t rowlocal,rowglobal;
  pastix_int_t nzlocal,nzglobal;
  long filenamenumber;
  char **filenametab;
  const pastix_int_t nbreltperline=4; /* nbr of elt per line */
  long tempint1,tempint2,tempint3,tempint4;
  long double tempfloat1, tempfloat2, tempfloat3, tempfloat4;

  *Nnzero=0;
  *Type = (char *) malloc(4*sizeof(char));
  *RhsType = (char *) malloc(4*sizeof(char));

  (*Type)[0] = 'R';
  (*Type)[1] = 'U';
  (*Type)[2] = 'A';
  (*Type)[3] = '\0';
  (*RhsType)[0] = 'A';
  (*RhsType)[2] = 'A';
  (*RhsType)[3] = '\0';


#ifdef TYPE_COMPLEX 
  fprintf(stderr, "\nWARNING: This drivers reads non complex matrices, imaginary part will be 0\n\n");
#endif

  /* Read rsaname */
  infile = fopen(filename, "r");
  if (infile==NULL)
    {
      fprintf(stderr,"cannot load %s\n", filename);
      exit(EXIT_FAILURE);
    }
  FGETS(line, BUFSIZ, infile);
  sscanf(line, "%ld", &filenamenumber); /* Read number of filename */
  filenametab = (char **) malloc(filenamenumber*sizeof(char *));
  for (iterfile=0; iterfile<filenamenumber; iterfile++)
    {
      filenametab[iterfile] = (char *) malloc(64*sizeof(char));
      FGETS(line, BUFSIZ, infile);
      sscanf(line, "%s", filenametab[iterfile]);
    }
  fclose(infile);
  
  /* Calcul nnz global */
  for (iterfile=0; iterfile<filenamenumber; iterfile++)
    {
      infile = fopen(filenametab[iterfile], "r");
      if (infile==NULL)
	{
	  fprintf(stderr,"cannot load %s\n", filenametab[iterfile]);
	  exit(EXIT_FAILURE);
	}
      FGETS(line, BUFSIZ, infile);
      sscanf(line, "%ld%ld", &tempint1,&tempint2);
      *Nrow = tempint1;
      rowlocal = tempint2;
      fprintf(stderr, "Nrow %ld rowlocal %ld\n", (long) *Nrow, (long) rowlocal);
      FGETS(line, BUFSIZ, infile);
      sscanf(line, "%ld", &tempint1);
      nzlocal = tempint1;
      fprintf(stderr, "nzlocal %ld\n", (long) nzlocal);
      fclose(infile);
    
      *Nnzero += nzlocal;
    }
  *Ncol = *Nrow;
  fprintf(stderr, "Nnzero global %ld\n", (long int) *Nnzero);

  /* memory alloc */
  (*col) = (pastix_int_t *) malloc((*Nrow+1)*sizeof(pastix_int_t));
  if ((*col) == NULL)
    fprintf(stderr, "peerRead : Not enough memory for *col\n");
  (*row) = (pastix_int_t *) malloc(*Nnzero*sizeof(pastix_int_t));
  if ((*row) == NULL)
    fprintf(stderr, "peerRead : Not enough memory for *row\n");
  (*val) = (pastix_float_t *) malloc(*Nnzero*sizeof(pastix_float_t));
  if ((*val) == NULL)
    fprintf(stderr, "peerRead : Not enough memory for *val\n");
  (*rhs) = (pastix_float_t *) malloc(*Nrow*sizeof(pastix_float_t));
  if ((*rhs) == NULL)
    fprintf(stderr, "peerRead : Not enough memory for *rhs\n");

  rowglobal=0;
  nzglobal=0;
  for (iterfile=0; iterfile<filenamenumber; iterfile++)
    {
      pastix_int_t iterelt;
      
      infile = fopen(filenametab[iterfile], "r");
      if (infile==NULL)
	{
	  fprintf(stderr,"cannot load %s\n", filenametab[iterfile]);
	  exit(EXIT_FAILURE);
	}
      FGETS(line,BUFSIZ,infile);
      sscanf(line, "%ld%ld", &tempint1, &tempint2);
      *Nrow = tempint1;
      rowlocal = tempint2;
      FGETS(line,BUFSIZ,infile);
      sscanf(line, "%ld", &tempint1);
      nzlocal = tempint1;

      /* read col */
      for (iterelt=0; iterelt<rowlocal+1+1-nbreltperline;iterelt++)
	{
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%ld %ld %ld %ld", &tempint1, &tempint2, &tempint3, &tempint4);
	  (*col)[iterelt+rowglobal]   = (pastix_int_t)tempint1+nzglobal;
	  (*col)[iterelt+rowglobal+1] = (pastix_int_t)tempint2+nzglobal;
	  (*col)[iterelt+rowglobal+2] = (pastix_int_t)tempint3+nzglobal;
	  (*col)[iterelt+rowglobal+3] = (pastix_int_t)tempint4+nzglobal;
	  iterelt+=3;
	}
      
      switch (rowlocal-iterelt+1)
	{
	case 1:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%ld",&tempint1);
	  (*col)[iterelt+rowglobal] += (pastix_int_t)tempint1+nzglobal;
	  iterelt++;
	  break;
	case 2:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%ld %ld", &tempint1, &tempint2);
	  (*col)[iterelt+rowglobal]   = (pastix_int_t)tempint1+nzglobal;
	  (*col)[iterelt+rowglobal+1] = (pastix_int_t)tempint2+nzglobal;
	  iterelt+=2;
	  break;
	case 3:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%ld %ld %ld", &tempint1, &tempint2, &tempint3);
	  (*col)[iterelt+rowglobal]   = (pastix_int_t)tempint1+nzglobal;
	  (*col)[iterelt+rowglobal+1] = (pastix_int_t)tempint2+nzglobal;
	  (*col)[iterelt+rowglobal+2] = (pastix_int_t)tempint3+nzglobal;
	  iterelt+=3;
	  break;
	}


      /* read row */
      for (iterelt=0; iterelt<nzlocal+1-nbreltperline; iterelt++)
	{
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%ld %ld %ld %ld", &tempint1,&tempint2,&tempint3,&tempint4);
	  (*row)[iterelt+nzglobal]   = (pastix_int_t)tempint1;
	  (*row)[iterelt+nzglobal+1] = (pastix_int_t)tempint2;
	  (*row)[iterelt+nzglobal+2] = (pastix_int_t)tempint3;
	  (*row)[iterelt+nzglobal+3] = (pastix_int_t)tempint4;
	  iterelt+=3;
	}
      switch (nzlocal-iterelt)
	{
	case 1:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%ld",&tempint1);
	  (*row)[iterelt+nzglobal] = (pastix_int_t)tempint1;
	  iterelt++;
	  break;
	case 2:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%ld %ld", &tempint1, &tempint2);
	  (*row)[iterelt+nzglobal]   = (pastix_int_t)tempint1;
	  (*row)[iterelt+nzglobal+1] = (pastix_int_t)tempint2;
	  iterelt+=2;
	  break;
	case 3:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%ld %ld %ld", &tempint1, &tempint2, &tempint3);
	  (*row)[iterelt+nzglobal]   = (pastix_int_t)tempint1;
	  (*row)[iterelt+nzglobal+1] = (pastix_int_t)tempint2;
	  (*row)[iterelt+nzglobal+2] = (pastix_int_t)tempint3;
	  iterelt+=3;
	  break;
	}

      /* read val */
      for (iterelt=0; iterelt<nzlocal+1-nbreltperline; iterelt++)
	{
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%Lf %Lf %Lf %Lf",&tempfloat1,&tempfloat2,&tempfloat3,&tempfloat4);
	  (*val)[iterelt+nzglobal]   = (pastix_float_t)tempfloat1;
	  (*val)[iterelt+nzglobal+1] = (pastix_float_t)tempfloat2;
	  (*val)[iterelt+nzglobal+2] = (pastix_float_t)tempfloat3;
	  (*val)[iterelt+nzglobal+3] = (pastix_float_t)tempfloat4;
	  iterelt+=3;
	}
      switch (nzlocal-iterelt)
	{
	case 1:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%Lf",&tempfloat1);
	  (*val)[iterelt+nzglobal] = (pastix_float_t)tempfloat1;
	  iterelt++;
	  break;
	case 2:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%Lf %Lf",&tempfloat1,&tempfloat2);
	  (*val)[iterelt+nzglobal]   = (pastix_float_t)tempfloat1;
	  (*val)[iterelt+nzglobal+1] = (pastix_float_t)tempfloat2;
	  iterelt+=2;
	  break;
	case 3:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%Lf %Lf %Lf",&tempfloat1,&tempfloat2,&tempfloat3);
	  (*val)[iterelt+nzglobal]   = (pastix_float_t)tempfloat1;
	  (*val)[iterelt+nzglobal+1] = (pastix_float_t)tempfloat2;
	  (*val)[iterelt+nzglobal+2] = (pastix_float_t)tempfloat3;
	  iterelt+=3;
	  break;
	}
      nzglobal += nzlocal;

      /* read rhs */
      for (iterelt=0; iterelt<rowlocal+1-nbreltperline; iterelt++)
	{
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%Lf %Lf %Lf %Lf",&tempfloat1,&tempfloat2,&tempfloat3,&tempfloat4);
	  (*rhs)[iterelt+rowglobal]   = (pastix_float_t)tempfloat1;
	  (*rhs)[iterelt+rowglobal+1] = (pastix_float_t)tempfloat2;
	  (*rhs)[iterelt+rowglobal+2] = (pastix_float_t)tempfloat3;
	  (*rhs)[iterelt+rowglobal+3] = (pastix_float_t)tempfloat4;
	  iterelt+=3;
	}

      switch (rowlocal-iterelt)
	{
	case 1:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%Lf",&tempfloat1);
	  (*rhs)[iterelt+rowglobal] = (pastix_float_t)tempfloat1;
	  iterelt++;
	  break;
	case 2:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%Lf %Lf",&tempfloat1,&tempfloat2);
	  (*rhs)[iterelt+rowglobal]   = (pastix_float_t)tempfloat1;
	  (*rhs)[iterelt+rowglobal+1] = (pastix_float_t)tempfloat2;
	  iterelt++;
	  break;
	case 3:
	  FGETS(line,BUFSIZ,infile);
	  sscanf(line,"%Lf %Lf %Lf",&tempfloat1,&tempfloat2,&tempfloat3);
	  (*rhs)[iterelt+rowglobal]   = (pastix_float_t)tempfloat1;
	  (*rhs)[iterelt+rowglobal+1] = (pastix_float_t)tempfloat2;
	  (*rhs)[iterelt+rowglobal+2] = (pastix_float_t)tempfloat3;
	  iterelt++;
	  break;
	}
      rowglobal += rowlocal;

      fclose(infile);
    }
}
示例#13
0
vx_status vxFReadImage(vx_array file, vx_image output)
{
    vx_char *filename = NULL;
    vx_size filename_stride = 0;
    vx_uint8 *src = NULL;
    vx_uint32 p = 0u, y = 0u;
    vx_size planes = 0u;
    vx_imagepatch_addressing_t addr = {0};
    vx_df_image format = VX_DF_IMAGE_VIRT;
    FILE *fp = NULL;
    vx_char tmp[VX_MAX_FILE_NAME] = {0};
    vx_char *ext = NULL;
    vx_rectangle_t rect;
    vx_uint32 width = 0, height = 0;

    vx_status status = vxAccessArrayRange(file, 0, VX_MAX_FILE_NAME, &filename_stride, (void **)&filename, VX_READ_ONLY);
    if (status != VX_SUCCESS || filename_stride != sizeof(vx_char))
    {
        vxAddLogEntry((vx_reference)file, VX_FAILURE, "Incorrect array "VX_FMT_REF"\n", file);
        return VX_FAILURE;
    }
    fp = fopen(filename, "rb");
    if (fp == NULL) {
        vxAddLogEntry((vx_reference)file, VX_FAILURE, "Failed to open file %s\n",filename);
        return VX_FAILURE;
    }

    vxQueryImage(output, VX_IMAGE_PLANES, &planes, sizeof(planes));
    vxQueryImage(output, VX_IMAGE_FORMAT, &format, sizeof(format));

    ext = strrchr(filename, '.');
    if (ext && (strcmp(ext, ".pgm") == 0 || strcmp(ext, ".PGM") == 0))
    {
        FGETS(tmp, fp); // PX
        FGETS(tmp, fp); // comment
        FGETS(tmp, fp); // W H
        sscanf(tmp, "%u %u", &width, &height);
        FGETS(tmp, fp); // BPP
        // ! \todo double check image size?
    }
    else if (ext && (strcmp(ext, ".yuv") == 0 ||
                     strcmp(ext, ".rgb") == 0 ||
                     strcmp(ext, ".bw") == 0))
    {
        sscanf(filename, "%*[^_]_%ux%u_%*s", &width, &height);
    }

    rect.start_x = rect.start_y = 0;
    rect.end_x = width;
    rect.end_y = height;

    for (p = 0; p < planes; p++)
    {
        status = vxAccessImagePatch(output, &rect, p, &addr, (void **)&src, VX_WRITE_ONLY);
        if (status == VX_SUCCESS)
        {
            for (y = 0; y < addr.dim_y; y+=addr.step_y)
            {
                vx_uint8 *srcp = vxFormatImagePatchAddress2d(src, 0, y, &addr);
                vx_size len = ((addr.dim_x * addr.scale_x)/VX_SCALE_UNITY);
                vx_size rlen = fread(srcp, addr.stride_x, len, fp);
                if (rlen != len)
                {
                    status = VX_FAILURE;
                    break;
                }
            }
            if (status == VX_SUCCESS)
            {
                status = vxCommitImagePatch(output, &rect, p, &addr, src);
            }
            if (status != VX_SUCCESS)
            {
                break;
            }
        }
        /* src pointer should be made NULL , otherwise the first plane data gets over written. */
        src = NULL;
    }
    fclose(fp);
    vxCommitArrayRange(file, 0, 0, filename);

    return status;
}
示例#14
0
/*
 *  Function: DistributedMatrixMarketRead
 *
 *  Reads a matrix in distributed matrix market format
 *
 *  For more information about matrix market format see mmio.c/mmio.h
 *
 * Parameters:
 *   dirname - Path to the directory containing matrix
 *   Ncol    - Number of columns
 *   Nrow    - Number of rows
 *   Nnzero  - Number of non zeros
 *   col     - Index of first element of each column in *row* and *val*
 *   row     - Row of eah element
 *   val     - Value of each element
 *   Type    - Type of the matrix
 *   RhsType - Type of the right-hand-side.
 *
 */
void DistributedMatrixMarketRead(char const      *filename,
                                 pastix_int_t    *Ncol,
                                 pastix_int_t    *Nrow,
                                 pastix_int_t    *Nnzero,
                                 pastix_int_t   **col,
                                 pastix_int_t   **row,
                                 pastix_float_t **val,
                                 pastix_int_t   **l2g,
                                 char           **Type,
                                 char           **RhsType)
{

  FILE * file;
  pastix_int_t * tempcol, *g2l, *templ2g;
  pastix_int_t iter, iter2, baseval, mincol, maxcol;
  pastix_int_t * temprow;
  pastix_float_t * tempval;
  pastix_int_t total;
  pastix_int_t tmp;
  pastix_int_t pos;
  pastix_int_t limit;
  MM_typecode matcode;
  int tmpncol,tmpnrow,tmpnnzero;
  int rank, size;
  int tmpint;
  pastix_int_t i, global_n_col;
  char line[BUFSIZ], my_filename_s[BUFSIZ];
  char * my_filename;
  pastix_int_t column;

  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  MPI_Comm_size(MPI_COMM_WORLD, &size);

  file = fopen (filename,"r");
  if (file==NULL)
  {
    fprintf(stderr,"cannot load %s\n", filename);
    EXIT(MOD_SI,FILE_ERR);
  }

  FGETS(line, BUFSIZ, file);
  sscanf(line, "%d", &tmpint); /* Read number of filename */
  fprintf(stdout, "%d files\n", tmpint);

  if (size != tmpint)
    {
      /* pour l'instant rien. au choix : recreer un nouveau comm mpi, refusionner la csc et la redecouper */
      if (rank == 0)
        fprintf(stderr, "Please rerun with %d processors\n", tmpint);
      exit(EXIT_FAILURE);
    }

  for (i = 0; i < rank+1; i++)
    {
      FGETS(line, BUFSIZ, file);
      sscanf(line, "%s", my_filename_s);
    }
  my_filename = (char*)malloc(sizeof(char)*(strlen(filename)+BUFSIZ));
  strcpy(my_filename, filename);
  sprintf(my_filename,"%s/%s", dirname(my_filename), my_filename_s);
  fclose(file);

  *Type = (char *) malloc(4*sizeof(char));
  *RhsType = (char *) malloc(1*sizeof(char));
  (*RhsType)[0] = '\0';

  file = fopen (my_filename,"r");
  if (file==NULL)
  {
    fprintf(stderr,"cannot load %s\n", my_filename);
    EXIT(MOD_SI,FILE_ERR);
  }

  if (mm_read_banner(file, &matcode) != 0)
  {
    fprintf(stderr,"Could not process Matrix Market banner.\n");
    exit(1);
  }

#ifdef    TYPE_COMPLEX
  (*Type)[0] = 'C';
  if (!mm_is_complex(matcode))
  {
    fprintf(stderr, "\nWARNING : Matrix should be complex. Imaginary part will be 0.\n\n");
  }
#else  /* TYPE_COMPLEX */
  (*Type)[0] = 'R';
  if (mm_is_complex(matcode))
  {
    fprintf(stderr, "\nWARNING : Matrix should not be complex. Only real part will be taken.\n\n");
  }
#endif /* TYPE_COMPLEX */

  (*Type)[1] = 'U';
  if (mm_is_symmetric(matcode))
  {
    (*Type)[1] = 'S';
  }
  else {
    if (mm_is_hermitian(matcode))
    {
      (*Type)[1] = 'H';
    }
  }
  (*Type)[2] = 'A';
  (*Type)[3] = '\0';
  /* find out size of sparse matrix .... */

  if (mm_read_mtx_crd_size(file, &tmpnrow, &tmpncol, &tmpnnzero) !=0)
    exit(1);

  *Ncol = 0;
  *Nrow = 0;
  *Nnzero = tmpnnzero;
  
  /* Allocation memoire */
  tempcol = (pastix_int_t *) malloc((*Nnzero)*sizeof(pastix_int_t));
  templ2g = (pastix_int_t *) malloc((*Nnzero)*sizeof(pastix_int_t));
  temprow = (pastix_int_t *) malloc((*Nnzero)*sizeof(pastix_int_t));
  tempval = (pastix_float_t *) malloc((*Nnzero)*sizeof(pastix_float_t));

  if ((tempcol==NULL) || (temprow == NULL) || (tempval == NULL))
  {
    fprintf(stderr, "MatrixMarketRead : Not enough memory (Nnzero %ld)\n",(long)*Nnzero);
    EXIT(MOD_SI,OUTOFMEMORY_ERR);
  }

  /* Remplissage */
  {
    long temp1,temp2;
    double re,im;
    im = 0.0;

    if (mm_is_complex(matcode))
    {
      for (iter=0; iter<(*Nnzero); iter++)
      {
        if (4 != fscanf(file,"%ld %ld %lg %lg\n", &temp1, &temp2, &re, &im))
        {
          fprintf(stderr, "ERROR: reading matrix (line %ld)\n",
                  (long int)iter);
          exit(1);
        }

        iter2 = 0;
        column = temp2;
        while ( iter2 < *Ncol &&
                templ2g[iter2] < column)
          iter2++;
        if ( !(iter2 < *Ncol && (templ2g)[iter2] == column) )
          {
            while ( iter2 < *Ncol )
              {
                tmp = column;
                column = (templ2g)[iter2];
                (templ2g)[iter2] = tmp;
                iter2++;
              }
            (templ2g)[iter2] = column;
            (*Ncol)++;
          }

        temprow[iter]=(pastix_int_t)temp1;
        tempcol[iter]=(pastix_int_t)temp2;
#ifdef    TYPE_COMPLEX
        tempval[iter]=(pastix_float_t)(re+im*I);
#else  /* TYPE_COMPLEX */
        tempval[iter]=(pastix_float_t)(re);
#endif /* TYPE_COMPLEX */
      }
    }
    else
    {
      for (iter=0; iter<(*Nnzero); iter++)
      {
        if (3 != fscanf(file,"%ld %ld %lg\n", &temp1, &temp2, &re))
        {
          fprintf(stderr, "ERROR: reading matrix (line %ld)\n",
                  (long int)iter);
          exit(1);
        }

        iter2 = 0;
        column = temp2;
        while ( iter2 < *Ncol &&
                (templ2g)[iter2] < column)
          iter2++;
        if ( iter2 == *Ncol || (templ2g)[iter2] != column )
          {
            while ( iter2 < *Ncol )
              {
                tmp = (templ2g)[iter2];
                (templ2g)[iter2] = column;
                column = tmp;
                iter2++;
              }
            (templ2g)[iter2] = column;
            (*Ncol)++;
          }

        temprow[iter]=(pastix_int_t)temp1;
        tempcol[iter]=(pastix_int_t)temp2;
#ifdef    TYPE_COMPLEX
        tempval[iter]=(pastix_float_t)(re+im*I);
#else  /* TYPE_COMPLEX */
        tempval[iter]=(pastix_float_t)(re);
#endif /* TYPE_COMPLEX */
      }
    }
  }

  *Nrow = *Ncol;
  (*l2g) = (pastix_int_t *) malloc((*Ncol)*sizeof(pastix_int_t));
  memcpy(*l2g, templ2g, *Ncol*sizeof(pastix_int_t));
  free(templ2g);
  (*col) = (pastix_int_t *) malloc((*Ncol+1)*sizeof(pastix_int_t));
  memset(*col,0,(*Ncol+1)*sizeof(pastix_int_t));
  (*row) = (pastix_int_t *) malloc((*Nnzero)*sizeof(pastix_int_t));
  memset(*row,0,(*Nnzero)*sizeof(pastix_int_t));
  (*val) = (pastix_float_t *) malloc((*Nnzero)*sizeof(pastix_float_t));

  if (((*col)==NULL) || ((*row) == NULL) || ((*val) == NULL))
    {
      fprintf(stderr, "MatrixMarketRead : Not enough memory (Nnzero %ld)\n",
              (long)*Nnzero);
      EXIT(MOD_SI,OUTOFMEMORY_ERR);
    }

  /* Detection de la base */
  mincol = (*l2g)[0];
  maxcol = (*l2g)[*Ncol-1];
  if (sizeof(int) == sizeof(pastix_int_t))
    {
      MPI_Allreduce(&mincol, &baseval, 1,      MPI_INT, MPI_MIN, MPI_COMM_WORLD);
      MPI_Allreduce(&maxcol, &global_n_col, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
    }
  else
    {
      MPI_Allreduce(&mincol, &baseval, 1,      MPI_LONG, MPI_MIN, MPI_COMM_WORLD);
      MPI_Allreduce(&maxcol, &global_n_col, 1, MPI_LONG, MPI_MAX, MPI_COMM_WORLD);
    }
  *Nrow = global_n_col;
  if (baseval == 0)
    {
      for(iter=0; iter<(*Nnzero); iter++)
        {
          tempcol[iter]++;
          temprow[iter]++;
        }
      for (iter = 0; iter < *Ncol; iter++)
        {
          (*l2g)[iter]++;
        }
    }

  if (baseval > 1 || baseval < 0)
    {
      fprintf(stderr, "Baseval > 1 || baseval < 0\n");
      exit(1);
    }
  baseval = 1;
  {
    /* Build loc2gloab */
    int inserted_column = 0;
    int iter2;
    int column;
    for (iter = 0; iter < (*Nnzero); iter ++)
      {
        iter2 = 0;
        column = tempcol[iter];
        while ( iter2 < inserted_column &&
                (*l2g)[iter2] < column)
          iter2++;
        if (iter2 < inserted_column && (*l2g)[iter2] == column)
          continue;

        while ( iter2 < inserted_column )
          {
            tmp = column;
            column = (*l2g)[iter2];
            (*l2g)[iter2] = tmp;
            iter2++;
          }
        (*l2g)[iter2] = column;
        inserted_column++;
        if (inserted_column == *Ncol)
          break;
      }
    assert(inserted_column == *Ncol);
  }

  {
    /* Build loc2glob */
    g2l = malloc(global_n_col*sizeof(pastix_int_t));
    for (iter = 0; iter < global_n_col; iter++)
      g2l[iter] = -1;
    for (iter = 0; iter < (*Ncol); iter ++)
      g2l[(*l2g)[iter]-1] = iter+1;
  }

  for (iter = 0; iter < (*Nnzero); iter ++)
    {
      (*col)[g2l[tempcol[iter]-1]-1]++;
    }

  total = baseval;
  for (iter = 0; iter < (*Ncol)+1; iter ++)
    {
      tmp = (*col)[iter];
      (*col)[iter]=total;
      total+=tmp;
    }

  for (iter = 0; iter < (*Nnzero); iter ++)
    {

      pos = (*col)[g2l[tempcol[iter]-1]-1]-1;
      limit = (*col)[g2l[tempcol[iter]-1]]-1;
      while((*row)[pos] != 0 && pos < limit)
        {
          pos++;
        }
      if (pos == limit)
        fprintf(stderr, "Erreur de lecture %ld %ld %ld\n",
                (long int)(*col)[g2l[tempcol[iter]-1]-1]-1,
                (long int)pos, (long int)limit);

      (*row)[pos] = temprow[iter];
      (*val)[pos] = tempval[iter];
    }

  memFree_null(tempval);
  memFree_null(temprow);
  memFree_null(tempcol);

  free(my_filename);
}
示例#15
0
unsigned char mu_cre_file(void)
{
	char		*cc = NULL, path[MAX_FBUFF + 1], errbuff[512];
	unsigned char	buff[DISK_BLOCK_SIZE];
	int		fd = -1, i, lower, upper, status, padded_len, padded_vbn, norm_vbn;
	uint4		raw_dev_size;		/* size of a raw device, in bytes */
	int4		blocks_for_create, blocks_for_extension, save_errno;
	GTM_BAVAIL_TYPE	avail_blocks;
	file_control	fc;
	mstr		file;
	parse_blk	pblk;
	unix_db_info	udi_struct, *udi;
	char		*fgets_res;
	gd_segment	*seg;

	error_def(ERR_NOSPACECRE);
	error_def(ERR_LOWSPACECRE);

	assert((-(sizeof(uint4) * 2) & sizeof(sgmnt_data)) == sizeof(sgmnt_data));
	cs_addrs = &udi_struct.s_addrs;
	cs_data = (sgmnt_data_ptr_t)NULL;	/* for CLEANUP */
	memset(&pblk, 0, sizeof(pblk));
	pblk.fop = (F_SYNTAXO | F_PARNODE);
	pblk.buffer = path;
	pblk.buff_size = MAX_FBUFF;
	file.addr = (char*)gv_cur_region->dyn.addr->fname;
	file.len = gv_cur_region->dyn.addr->fname_len;
	strncpy(path,file.addr,file.len);
	*(path+file.len) = '\0';
	if (is_raw_dev(path))
	{	/* do not use a default extension for raw device files */
		pblk.def1_buf = DEF_NODBEXT;
		pblk.def1_size = sizeof(DEF_NODBEXT) - 1;
	} else
	{
		pblk.def1_buf = DEF_DBEXT;
		pblk.def1_size = sizeof(DEF_DBEXT) - 1;
	}
	if (1 != (parse_file(&file, &pblk) & 1))
	{
		PRINTF("Error translating filename %s.\n", gv_cur_region->dyn.addr->fname);
		return EXIT_ERR;
	}
	path[pblk.b_esl] = 0;
	if (pblk.fnb & F_HAS_NODE)
	{	/* Remote node specification given */
		assert(pblk.b_node);
		PRINTF("Database file for region %s not created; cannot create across network.\n", path);
		return EXIT_WRN;
	}
	udi = &udi_struct;
	udi->raw = is_raw_dev(pblk.l_dir);
	if (udi->raw)
	{
		fd = OPEN(pblk.l_dir,O_EXCL | O_RDWR);
		if (-1 == fd)
		{
			SPRINTF_AND_PERROR("Error opening file %s\n");
			return EXIT_ERR;
		}
		if (-1 != (status = (ssize_t)lseek(fd, 0, SEEK_SET)))
		{
			DOREADRC(fd, buff, sizeof(buff), status);
		} else
			status = errno;
		if (0 != status)
		{
			SPRINTF_AND_PERROR("Error reading header for file %s\n");
			return EXIT_ERR;
		}
		if (!memcmp(buff, GDS_LABEL, STR_LIT_LEN(GDS_LABEL)))
		{
			char rsp[80];
			PRINTF("Database already exists on device %s\n", path);
			PRINTF("Do you wish to re-initialize (all current data will be lost) [y/n] ? ");
			FGETS(rsp, 79, stdin, fgets_res);
			if ('y' != *rsp)
				return EXIT_NRM;
		}
		PRINTF("Determining size of raw device...\n");
		for(i = 1; read(fd, buff, sizeof(buff)) == sizeof(buff);)
		{
			i *= 2;
			lseek(fd, (off_t)i * BUFSIZ, SEEK_SET);
		}

		lower = i / 2;
		upper = i;
		while ((lower + upper) / 2 != lower)
		{
			i = (lower + upper) / 2;
			lseek(fd, (off_t)i * BUFSIZ, SEEK_SET);
 			if (read(fd, buff, sizeof(buff)) == sizeof(buff))
				lower = i;
			else
				upper = i;
		}
		raw_dev_size = i * BUFSIZ;
	} else
	{
		fd = OPEN3(pblk.l_dir, O_CREAT | O_EXCL | O_RDWR, 0600);
		if (-1 == fd)
		{
			SPRINTF_AND_PERROR("Error opening file %s\n");
			return EXIT_ERR;
		}
		if (0 != (save_errno = disk_block_available(fd, &avail_blocks, FALSE)))
		{
			errno = save_errno;
			SPRINTF_AND_PERROR("Error checking available disk space for %s\n");
			CLEANUP(EXIT_ERR);
			return EXIT_ERR;
		}
		seg = gv_cur_region->dyn.addr;

		/* blocks_for_create is in the unit of DISK_BLOCK_SIZE */
		blocks_for_create = DIVIDE_ROUND_UP(sizeof(sgmnt_data), DISK_BLOCK_SIZE) + 1 +
					(seg->blk_size / DISK_BLOCK_SIZE *
					 ((DIVIDE_ROUND_UP(seg->allocation, BLKS_PER_LMAP - 1)) + seg->allocation));
		if ((uint4)avail_blocks < blocks_for_create)
		{
			gtm_putmsg(VARLSTCNT(6) ERR_NOSPACECRE, 4, LEN_AND_STR(path), blocks_for_create, (uint4)avail_blocks);
			send_msg(VARLSTCNT(6) ERR_NOSPACECRE, 4, LEN_AND_STR(path), blocks_for_create, (uint4)avail_blocks);
			CLEANUP(EXIT_ERR);
			return EXIT_ERR;
		}
		blocks_for_extension = (seg->blk_size / DISK_BLOCK_SIZE *
					((DIVIDE_ROUND_UP(EXTEND_WARNING_FACTOR * seg->ext_blk_count, BLKS_PER_LMAP - 1))
				 	  + EXTEND_WARNING_FACTOR * seg->ext_blk_count));
		if ((uint4)(avail_blocks - blocks_for_create) < blocks_for_extension)
		{
			gtm_putmsg(VARLSTCNT(8) ERR_LOWSPACECRE, 6, LEN_AND_STR(path), EXTEND_WARNING_FACTOR, blocks_for_extension,
					DISK_BLOCK_SIZE, (uint4)(avail_blocks - blocks_for_create));
			send_msg(VARLSTCNT(8) ERR_LOWSPACECRE, 6, LEN_AND_STR(path), EXTEND_WARNING_FACTOR, blocks_for_extension,
					DISK_BLOCK_SIZE, (uint4)(avail_blocks - blocks_for_create));
		}
	}
	gv_cur_region->dyn.addr->file_cntl = &fc;
	fc.file_info = (void*)&udi_struct;
	udi->fd = fd;
	cs_data = (sgmnt_data_ptr_t)malloc(sizeof(sgmnt_data));
	memset(cs_data, 0, sizeof(*cs_data));
	cs_data->createinprogress = TRUE;
	cs_data->semid = INVALID_SEMID;
	cs_data->shmid = INVALID_SHMID;
	/* We want our datablocks to start on what would be a block boundary within the file so pad the fileheader
	   if necessary to make this happen.
	*/
	padded_len = ROUND_UP(sizeof(sgmnt_data), BLK_SIZE);
	padded_vbn = DIVIDE_ROUND_UP(padded_len, DISK_BLOCK_SIZE) + 1;
	norm_vbn = DIVIDE_ROUND_UP(sizeof(sgmnt_data), DISK_BLOCK_SIZE) + 1;
	cs_data->start_vbn = padded_vbn;
	cs_data->free_space += (padded_vbn - norm_vbn) * DISK_BLOCK_SIZE;
	cs_data->acc_meth = gv_cur_region->dyn.addr->acc_meth;
	if (udi->raw)
	{
		/* calculate total blocks, reduce to make room for the
		 * database header (size rounded up to a block), then
		 * make into a multiple of BLKS_PER_LMAP to have a complete bitmap
		 * for each set of blocks.
		 */
		cs_data->trans_hist.total_blks = raw_dev_size - ROUND_UP(sizeof(sgmnt_data), DISK_BLOCK_SIZE);
		cs_data->trans_hist.total_blks /= (uint4)(((gd_segment *)gv_cur_region->dyn.addr)->blk_size);
		if (0 == (cs_data->trans_hist.total_blks - DIVIDE_ROUND_UP(cs_data->trans_hist.total_blks, BLKS_PER_LMAP - 1)
			  % (BLKS_PER_LMAP - 1)))
			cs_data->trans_hist.total_blks -= 1;	/* don't create a bitmap with no data blocks */
		cs_data->extension_size = 0;
		PRINTF("Raw device size is %dK, %d GDS blocks\n",
		raw_dev_size / 1000,
		cs_data->trans_hist.total_blks);
	} else
	{
		cs_data->trans_hist.total_blks = gv_cur_region->dyn.addr->allocation;
		/* There are (bplmap - 1) non-bitmap blocks per bitmap, so add (bplmap - 2) to number of non-bitmap blocks
		 * and divide by (bplmap - 1) to get total number of bitmaps for expanded database. (must round up in this
		 * manner as every non-bitmap block must have an associated bitmap)
		 */
		cs_data->trans_hist.total_blks += DIVIDE_ROUND_UP(cs_data->trans_hist.total_blks, BLKS_PER_LMAP - 1);
		cs_data->extension_size = gv_cur_region->dyn.addr->ext_blk_count;
	}
	mucregini(cs_data->trans_hist.total_blks);
	cs_data->createinprogress = FALSE;
	LSEEKWRITE(udi->fd, 0, cs_data, sizeof(sgmnt_data), status);
	if (0 != status)
	{
		SPRINTF_AND_PERROR("Error writing out header for file %s\n");
		CLEANUP(EXIT_ERR);
		return EXIT_ERR;
	}
	cc = (char*)malloc(DISK_BLOCK_SIZE);
	memset(cc, 0, DISK_BLOCK_SIZE);
	LSEEKWRITE(udi->fd,
		   (cs_data->start_vbn - 1) * DISK_BLOCK_SIZE + ((off_t)(cs_data->trans_hist.total_blks) * cs_data->blk_size),
		   cc,
		   DISK_BLOCK_SIZE,
		   status);
	if (0 != status)
	{
		SPRINTF_AND_PERROR("Error writing out end of file %s\n");
		CLEANUP(EXIT_ERR);
		return EXIT_ERR;
	}
	if ((!udi->raw) && (-1 == CHMOD(pblk.l_dir, 0666)))
	{
		SPRINTF_AND_PERROR("Error changing file mode on file %s\n");
		CLEANUP(EXIT_WRN);
		return EXIT_WRN;
	}
	CLEANUP(EXIT_NRM);
	PRINTF("Created file %s\n", path);
	return EXIT_NRM;
}