[895] | 1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
| 2 | //
|
---|
[3572] | 3 | // $Id: utils.cc,v 1.3 2009-02-07 21:49:59 cmv Exp $
|
---|
[895] | 4 | //
|
---|
| 5 |
|
---|
[2615] | 6 | #include "sopnamsp.h"
|
---|
[895] | 7 | #include "machdefs.h"
|
---|
| 8 | #include "utils.h"
|
---|
| 9 | #include "pexceptions.h"
|
---|
| 10 | #include <string.h>
|
---|
| 11 |
|
---|
| 12 | // MemCpy marche meme en cas de recouvrement.
|
---|
| 13 | void* MemCpy(void* dest, const void* source, size_t n)
|
---|
| 14 | {
|
---|
| 15 | int i; // THINK C ne veut pas du int i dans chaque for...
|
---|
| 16 | if (dest < source)
|
---|
[3572] | 17 | for (i=0; i<(int)n; i++)
|
---|
[895] | 18 | ((char*)dest)[i] = ((char*)source)[i];
|
---|
| 19 | else
|
---|
| 20 | for (i=n-1; i>=0; i--)
|
---|
| 21 | ((char*)dest)[i] = ((char*)source)[i];
|
---|
| 22 |
|
---|
| 23 | return dest;
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | // Dans parmst.cc
|
---|
| 27 | //int PrtLevel = 0;
|
---|
| 28 | //int DebugLevel = 0;
|
---|
| 29 |
|
---|
| 30 | char const* PeidaWorkPath()
|
---|
| 31 | {
|
---|
| 32 | char const* envWP = getenv("PEIDAWORKPATH");
|
---|
| 33 | if (envWP) return envWP;
|
---|
| 34 | #ifdef __mac__
|
---|
| 35 | return "WORK";
|
---|
| 36 | #endif
|
---|
| 37 | return ".";
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | #ifndef __mac__
|
---|
| 41 | void buildPath(char* dst, const char* dir, const char* file)
|
---|
| 42 | {
|
---|
| 43 | strcpy(dst, dir);
|
---|
| 44 | char *p = dst; while (*p) p++; p--;
|
---|
| 45 | if (*dir && *p != '/') strcat(dst, "/");
|
---|
| 46 | strcat(dst, file);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | void buildPath(string& dst, const string& dir, const string& file)
|
---|
| 50 | {
|
---|
| 51 | dst = dir;
|
---|
| 52 | if (dir != "" && dir[dir.length()-1] != '/') dst += "/";
|
---|
| 53 | dst += file;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | #else
|
---|
| 57 | void buildPath(char* dst, const char* dir, const char* file)
|
---|
| 58 | {
|
---|
| 59 | if (*dir) {
|
---|
| 60 | strcpy(dst, ":");
|
---|
| 61 | strcat(dst, dir);
|
---|
| 62 | char *p = dst; while (*p) p++; p--;
|
---|
| 63 | strcat(dst, ":");
|
---|
| 64 | strcat(dst, file);
|
---|
| 65 | } else
|
---|
| 66 | strcpy(dst, file);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | void buildPath(string& dst, const string& dir, const string& file)
|
---|
| 70 | {
|
---|
| 71 | if (dir != "") {
|
---|
| 72 | dst = ":" + dir + ":" + file;
|
---|
| 73 | } else
|
---|
| 74 | dst = file;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | #endif
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 | void changeSuffix(char* file, const char* suffix)
|
---|
| 81 | {
|
---|
| 82 | char *p = file;
|
---|
| 83 | while (*p) p++;
|
---|
| 84 | while (*p != '.' && p > file) p--;
|
---|
| 85 | if (*p != '.')
|
---|
| 86 | strcat(file, suffix);
|
---|
| 87 | else
|
---|
| 88 | strcpy(p,suffix);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | void changeSuffix(string& file, const string& suffix)
|
---|
| 92 | {
|
---|
| 93 | size_t pos = file.rfind('.');
|
---|
| 94 | // if (pos != NPOS) file.remove(pos);
|
---|
| 95 | if (pos != NPOS) file = file.substr(0,pos);
|
---|
| 96 | file += suffix;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | string itos(int i)
|
---|
| 100 | {
|
---|
| 101 | char s[20];
|
---|
| 102 | sprintf(s, "%d", i);
|
---|
| 103 | return s;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | string ftos(double f)
|
---|
| 107 | {
|
---|
| 108 | char s[20];
|
---|
| 109 | sprintf(s, "%g", f);
|
---|
| 110 | return s;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 |
|
---|