|
| 1 | +// |
| 2 | +// File: getConfigFilename.cpp |
| 3 | +// |
| 4 | +// Author: fatray |
| 5 | +// |
| 6 | +// Created on 05 December 2007, 23:39 |
| 7 | +// |
| 8 | +// FIXME: portability |
| 9 | + |
| 10 | + |
| 11 | +// I hacked include<string> on to silence my compiler, is it valid? |
| 12 | +#include <string> |
| 13 | +#include <cstring> |
| 14 | +#include <cstdlib> |
| 15 | +#include "getConfigFilename.h" |
| 16 | +#include <fcntl.h> |
| 17 | +#include <sys/stat.h> |
| 18 | +#include <cstdio> |
| 19 | + |
| 20 | +#define PROJECTM_PREFIX "" |
| 21 | + |
| 22 | +// get the full pathname of a configfile |
| 23 | +std::string getConfigFilename() |
| 24 | +{ |
| 25 | + char num[512]; |
| 26 | + FILE *in; |
| 27 | + FILE *out; |
| 28 | + |
| 29 | + char* home; |
| 30 | + // FIXME: fixed length buffers are not ideal. |
| 31 | + char projectM_home[1024]; |
| 32 | + char projectM_config[1024]; |
| 33 | + |
| 34 | + strcpy(projectM_config, PROJECTM_PREFIX); |
| 35 | + strcpy(projectM_config + strlen(PROJECTM_PREFIX), CONFIG_FILE); |
| 36 | + projectM_config[strlen(PROJECTM_PREFIX) + strlen(CONFIG_FILE)] = '\0'; |
| 37 | + printf("dir:%s \n", projectM_config); |
| 38 | + home = getenv("HOME"); |
| 39 | + strcpy(projectM_home, home); |
| 40 | + strcpy(projectM_home + strlen(home), "/.projectM/config.inp"); |
| 41 | + projectM_home[strlen(home) + strlen("/.projectM/config.inp")] = '\0'; |
| 42 | + |
| 43 | + if ((in = fopen(projectM_home, "r"))) |
| 44 | + { |
| 45 | + printf("reading ~/.projectM/config.inp \n"); |
| 46 | + fclose(in); |
| 47 | + return std::string(projectM_home); |
| 48 | + } |
| 49 | + |
| 50 | + printf("trying to create ~/.projectM/config.inp \n"); |
| 51 | + |
| 52 | + projectM_home[strlen(home) + strlen("/.projectM")] = '\0'; |
| 53 | + mkdir(projectM_home, 0755); |
| 54 | + |
| 55 | + strcpy(projectM_home + strlen(home), "/.projectM/config.inp"); |
| 56 | + projectM_home[strlen(home) + strlen("/.projectM/config.inp")] = '\0'; |
| 57 | + |
| 58 | + if((out = fopen(projectM_home, "w"))) |
| 59 | + { |
| 60 | + if ((in = fopen(projectM_config, "r"))) |
| 61 | + { |
| 62 | + while(fgets(num, 80, in)!=NULL) |
| 63 | + { |
| 64 | + fputs(num, out); |
| 65 | + } |
| 66 | + fclose(in); |
| 67 | + fclose(out); |
| 68 | + |
| 69 | + |
| 70 | + if ((in = fopen(projectM_home, "r"))) |
| 71 | + { |
| 72 | + printf("created ~/.projectM/config.inp successfully\n"); |
| 73 | + fclose(in); |
| 74 | + return std::string(projectM_home); |
| 75 | + } |
| 76 | + |
| 77 | + printf("This shouldn't happen, using implementation defaults\n"); |
| 78 | + abort(); |
| 79 | + } |
| 80 | + printf("Cannot find projectM default config, using implementation defaults\n"); |
| 81 | + abort(); |
| 82 | + } |
| 83 | + |
| 84 | + printf("Cannot create ~/.projectM/config.inp, using default config file\n"); |
| 85 | + if ((in = fopen(projectM_config, "r"))) |
| 86 | + { |
| 87 | + printf("Successfully opened default config file\n"); |
| 88 | + fclose(in); |
| 89 | + return std::string(projectM_config); |
| 90 | + } |
| 91 | + |
| 92 | + printf("Using implementation defaults, your system is really messed up, I'm suprised we even got this far\n"); |
| 93 | + abort(); |
| 94 | +} |
| 95 | + |
0 commit comments