Skip to content
Snippets Groups Projects
winjunk.c 2.05 KiB
Newer Older
  • Learn to ignore specific revisions
  • teuben's avatar
    teuben committed
    #ifdef WIN32
    
    /*
     * Assorted functions needed for Windows port.
     * Stuart Levy, slevy@ncsa.uiuc.edu
     * National Center for Supercomputing Applications,
     * University of Illinois 2001.
    
     * This file is part of partiview, released under the
     * Illinois Open Source License; see the file LICENSE.partiview for details.
    
    teuben's avatar
    teuben committed
    
    
    slevy's avatar
    slevy committed
    #include "config.h"
    
    
    teuben's avatar
    teuben committed
    #include <stdio.h>
    #include <stdlib.h>
    
    #include <stdarg.h>
    
    teuben's avatar
    teuben committed
    #include <ctype.h>
    
    
    #ifndef HAVE_STRCASECMP
    
    teuben's avatar
    teuben committed
    int strcasecmp(const char *s1, const char *s2) {
      int c1, c2;
      if(s1 == NULL) return 1;
      if(s2 == NULL) return -1;
      for(;;) {
        c1 = *s1++;
        c2 = *s2++;
        if(c1 == '\0')
    	return (c2 == '\0' ? 0 : 1);
        if(c2 == '\0')
    	return -1;
        if(c1 >= 'A' && c1 <= 'Z') c1 += 'a'-'A';
        if(c2 >= 'A' && c2 <= 'Z') c2 += 'a'-'A';
        if(c1 < c2) return -1;
        if(c1 > c2) return 1;
      }
    }
    
    
    int strncasecmp(const char *s1, const char *s2, int maxlen) {
      int c1, c2;
      int i;
      if(s1 == NULL) return 1;
      if(s2 == NULL) return -1;
      for(i = 0; i < maxlen; i++) {
        c1 = *s1++;
        c2 = *s2++;
        if(c1 == '\0')
    	return (c2 == '\0' ? 0 : 1);
        if(c2 == '\0')
    	return -1;
        if(c1 >= 'A' && c1 <= 'Z') c1 += 'a'-'A';
        if(c2 >= 'A' && c2 <= 'Z') c2 += 'a'-'A';
        if(c1 < c2) return -1;
        if(c1 > c2) return 1;
      }
      return 0;
    }
    
    #endif /*!HAVE_STRCASECMP*/
    
    teuben's avatar
    teuben committed
    
    void srandom(int seed) {
      srand(seed);
    }
    
    int random() {
      return rand() << 15 | rand();
    }
    
      /* Assuming x86 Windows, i.e. little-endian */
    
    unsigned int htonl(unsigned int v) {
        return (v&0xFF)<<24 | (v&0xFF00)<<8 | (v>>8)&0xFF00 | (v>>24)&0xFF;
    }
    
    
    slevy's avatar
     
    slevy committed
    #include <math.h>
    
    #include <float.h>
    
    
    slevy's avatar
     
    slevy committed
    double rint(double v) {
        return floor(v + 0.5);
    }
    
    #endif /*!HAVE_RINT*/
    
    slevy's avatar
     
    slevy committed
    
    
    #ifndef HAVE_SNPRINTF
    
    /* Use FLTK's substitute vsnprintf since Win32 lacks any of its own */
    extern int fl_vsnprintf(char* str, size_t size, const char* fmt, va_list ap);
    
    int snprintf(char* str, size_t size, const char* fmt, ...) {
      int ret;
      va_list ap;
      va_start(ap, fmt);
      ret = fl_vsnprintf(str, size, fmt, ap);
      va_end(ap);
      return ret;
    }
    
    #endif /*!HAVE_SNPRINTF*/
    
    teuben's avatar
    teuben committed
    #endif /*WIN32*/